Apache Solr - 删除文档

删除文档

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

<delete>   
   <id>003</id>   
   <id>005</id> 
   <id>004</id> 
   <id>002</id> 
</delete>

此处,此XML代码用于删除ID为 003 005 的文档.将此代码保存在名为 delete.xml 的文件中.

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

[Hadoop@localhost bin]$ ./post -c my_core delete.xml

执行上述命令时,将获得以下输出.

/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core
6.2.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.179

验证

访问Apache Solr Web界面的主页,选择核心 my_core .尝试通过在文本区域 q 中传递查询":"来检索所有文档并执行查询.执行时,您可以观察到指定的文档被删除.

删除文档

删除字段

有时我们需要根据ID以外的字段删除文档.例如,我们可能必须删除城市为Chennai的文档.

在这种情况下,您需要在< query><< query><<<>内指定字段的名称和值./查询&GT;标签对.

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

将其另存为 delete_field.xml ,并使用以下内容对名为 my_core 的核心执行删除操作Solr的 post 工具.

[Hadoop@localhost bin]$ ./post -c my_core delete_field.xml

在执行上述命令时,它会产生以下输出.

/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core
6.2.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.084

验证

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

值对

删除所有文档

就像删除特定字段一样,如果要删除索引中的所有文档,只需传递符号": "标签之间< query></query>,如下所示.

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

将其另存为 delete_all.xml 并使用以下内容对名为 my_core 的核心执行删除操作Solr的 post 工具.

[Hadoop@localhost bin]$ ./post -c my_core delete_all.xml

在执行上述命令时,它会产生以下输出.

/home/Hadoop/java/bin/java -classpath /home/Hadoop/Solr/dist/Solr-core
6.2.0.jar -Dauto = yes -Dc = my_core -Ddata = files 
org.apache.Solr.util.SimplePostTool deleteAll.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 deleteAll.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.138

验证

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

已删除的值对

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

以下是将文档添加到Apache Solr索引的Java程序.将此代码保存在名为 UpdatingDocument.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"); 
   } 
}

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

[Hadoop@localhost bin]$ javac DeletingAllDocuments 
[Hadoop@localhost bin]$ java DeletingAllDocuments

在执行上述命令时,您将获得以下输出.

Documents deleted