Solr删除文档数据

删除文档

要从Apache Solr的索引中删除文档,我们需要在<delete> </ delete>标记之间指定要删除的文档的ID

<delete>   
   <id>003</id>   
   <id>005</id> 
</delete>

这里,此XML代码用于删除ID003005的文档。将此代码保存在名称为delete.xml的文件中。

如果要从属于名称为my_core的核心的索引中删除文档,则可以使用post工具发布delete.xml文件,如下所示。

[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete.xml

执行上述命令后,将得到以下输出 –

yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c my_core delete.xml
/usr/local/jdk1.8.0_65/bin/java -classpath /usr/local/solr-6.4.0/dist/solr-core-6.4.0.jar -Dauto=yes -Dc=my_core -Ddata=files org.apache.solr.util.SimplePostTool delete.xml
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:8983/solr/my_core/update...
Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log
POSTing file delete.xml (application/xml) to [base]
1 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/my_core/update...
Time spent: 0:00:00.124

验证执行结果

访问Apache Solr Web界面的主页,选择核心 – my_core。 尝试通过在文本区域q中传递查询“”来检索所有文档,并执行查询。 执行时可以观察到指定的文档(ID003005)已删除。

《Solr删除文档数据》

删除字段

有时,需要基于除ID以外的字段来删除文档。例如,可能需要删除城市是Chennai的文档。

在这种情况下,需要在<query> </ query>标记对中指定字段的名称和值。

<delete> 
   <query>city:Chennai</query> 
</delete>

将上面代码保存到delete_field.xml文件中,并使用Solr的post工具在核心my_core上执行删除操作。

[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete_field.xml

执行上述命令后,将产生以下输出。

yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c my_core delete_field.xml
/usr/local/jdk1.8.0_65/bin/java -classpath /usr/local/solr-6.4.0/dist/solr-core-6.4.0.jar -Dauto=yes -Dc=my_core -Ddata=files org.apache.solr.util.SimplePostTool delete_field.xml
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:8983/solr/my_core/update...
Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log
POSTing file delete_field.xml (application/xml) to [base]
1 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/my_core/update...
Time spent: 0:00:00.225

验证执行结果

访问Apache Solr Web界面的主页,选择核心 – my_core。 尝试通过在文本区域q中传递查询“”来检索所有文档,并执行查询。 执行时可以观察到包含指定字段值对的文档被删除。
《Solr删除文档数据》

删除所有文档

类似删除一个指定删除某个字段一样,如果想删除索引中的所有文档,只需要在标签<query> </ query>之间传递符号“”,如下所示。

<delete> 
   <query>*:*</query> 
</delete>

将上面代码保存到delete_all.xml文件中,并使用Solr的post工具对核心my_core执行删除操作。

[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete_all.xml

执行上述命令后,将产生以下输出。

yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c my_core delete_all.xml
/usr/local/jdk1.8.0_65/bin/java -classpath /usr/local/solr-6.4.0/dist/solr-core-6.4.0.jar -Dauto=yes -Dc=my_core -Ddata=files org.apache.solr.util.SimplePostTool delete_all.xml
SimplePostTool version 5.0.0
Posting files to [base] url http://localhost:8983/solr/my_core/update...
Entering auto mode. File endings considered are xml,json,jsonl,csv,pdf,doc,docx,ppt,pptx,xls,xlsx,odt,odp,ods,ott,otp,ots,rtf,htm,html,txt,log
POSTing file delete_all.xml (application/xml) to [base]
1 files indexed.
COMMITting Solr index changes to http://localhost:8983/solr/my_core/update...
Time spent: 0:00:00.114

验证执行结果

访问Apache Solr Web界面的主页,选择核心 – my_core。 尝试通过在文本区域q中传递查询“”来检索所有文档,并执行查询。执行时您可以观察到包含指定字段值对的文档全被删除了。

《Solr删除文档数据》

使用Java(客户端API)删除所有文档

以下是使用Java程序向Apache Solr索引删除文档。将此代码保存在名称为DeletingAllDocuments.java的文件中。

import java.io.IOException;  

import org.apache.Solr.client.Solrj.SolrClient; 
import org.apache.Solr.client.Solrj.SolrServerException; 
import org.apache.Solr.client.Solrj.impl.HttpSolrClient; 
import org.apache.Solr.common.SolrInputDocument;  

public class DeletingAllDocuments { 
   public static void main(String args[]) throws SolrServerException, IOException {
      //Preparing the Solr client 
      String urlString = "http://localhost:8983/Solr/my_core"; 
      SolrClient Solr = new HttpSolrClient.Builder(urlString).build();   

      //Preparing the Solr document 
      SolrInputDocument doc = new SolrInputDocument();   

      //Deleting the documents from Solr 
      Solr.deleteByQuery("*");        

      //Saving the document 
      Solr.commit(); 
      System.out.println("Documents deleted"); 
   } 
}

通过在终端中执行以下命令编译上述代码 –

[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ javac DeletingAllDocuments.java
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ java DeletingAllDocuments

执行上述命令后,将得到以下输出。

Documents deleted

        原文作者:Solr教程
        原文地址: https://www.yiibai.com/solr/apache_solr_deleting_documents.html
        本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
    点赞