Solr教程

Solr 删除文档

Apache Solr 中删除文档

如果我们想从 Apache Solr 的索引中删除文档,我们需要为要删除的文档初始化 ID <delete> </delete> 标签。
<delete>   
   <id>101</id>   
   <id>102</id> 
   <id>103</id> 
   <id>104</id> 
</delete>
上面给出的XML代码可以用来删除ID为101和102的文档,我们只需要将该代码保存为delete.xml即可。
以防万一,如果我们想从索引中删除属于名为 my_core 的核心的文档,然后我们可以使用 post 工具发布 delete.xml 文件,如下所示。
[Hadoop@localhost bin]$ ./post-c my_core delete.xml
当我们编译并运行上述命令时,我们将收到以下输出:
/ home/ Hadoop/ java/ bin/ home/ Hadoop/ Solr/ dist/ Solr-core
8.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 file 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 内传递查询": "来获取所有文档并运行查询。运行查询时,可以看到指定的文档被删除了。
Deleting Document in Apache Solr

删除字段

在某些情况下,我们必须根据 ID 以外的字段删除文档。例如。如果我们要删除城市是纽约的文档。在这种情况下,我们必须在 标记对中指定字段的名称和值。
<delete> 
   <query>city:New York</query> 
</delete>
将上述XML文件保存为delete_field.xml,使用Solr的post工具对核心名称my_core执行删除操作。
[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网页界面首页,选择核心为my_core。尝试通过在文本区域 q 中传递查询": "来获取所有文档并执行查询。运行该命令可以观察到包含指定字段值对的文档被删除。
在 Apache Solr 中删除文档

删除所有文档

因为我们删除了一个特定的文件,所以我们可以从索引中删除所有文档,我们只需要在标签 <query></query> 之间传递符号":",如下所示。
<delete>
    <query>*:*</query>
</delete>
将上述xml文件保存为delete_all.xml,使用Solr的post工具对核心名称my_core进行删除操作
[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 中传递查询": "来检索所有文档并执行查询。运行后可以观察到包含指定字段值对的文档被删除了。
在 Apache Solr 中删除文档

使用 Java API 删除文档

下面是 Java 程序,可用于将文档添加到 Apache Solr 索引。重写此代码并将其保存在名为 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
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4