Apache Solr - 更新数据



使用 XML 更新文档

以下是用于更新现有文档中的字段的 XML 文件。将其保存到名为 update.xml 的文件中。

<add>   
   <doc>     
      <field name = "id">001</field>     
      <field name = "first name" update = "set">Raj</field>     
      <field name = "last name" update = "add">Malhotra</field>     
      <field name = "phone" update = "add">9000000000</field>    
      <field name = "city" update = "add">Delhi</field>   
   </doc> 
</add>

您可以看到,用于更新数据的 XML 文件与我们用于添加文档的 XML 文件相同。但唯一不同的是,我们使用字段的 update 属性。

在我们的示例中,我们将使用上面的文档并尝试更新文档的字段,其 ID 为 001

假设 XML 文档存在于 Solr 的 bin 目录中。由于我们正在更新名称为 my_core 的核心中的索引,您可以使用 post 工具进行如下更新:

[Hadoop@localhost bin]$ ./post -c my_core update.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 update.xml 
SimplePostTool version 5.0.0 
Posting files to [base] url https://127.0.0.1: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 update.xml (application/xml) to [base] 
1 files indexed. 
COMMITting Solr index changes to https://127.0.0.1:8983/Solr/my_core/update... 
Time spent: 0:00:00.159 

验证

访问 Apache Solr Web 界面主页,并将核心选择为 my_core。尝试通过在文本框 q 中输入查询“:”并执行查询来检索所有文档。在执行过程中,您可以看到文档已更新。

Execute Query

使用 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.client.Solrj.request.UpdateRequest; 
import org.apache.Solr.client.Solrj.response.UpdateResponse;
import org.apache.Solr.common.SolrInputDocument;  

public class UpdatingDocument { 
   public static void main(String args[]) throws SolrServerException, IOException { 
      //Preparing the Solr client 
      String urlString = "https://127.0.0.1:8983/Solr/my_core"; 
      SolrClient Solr = new HttpSolrClient.Builder(urlString).build();   
      
      //Preparing the Solr document 
      SolrInputDocument doc = new SolrInputDocument(); 
   
      UpdateRequest updateRequest = new UpdateRequest();  
      updateRequest.setAction( UpdateRequest.ACTION.COMMIT, false, false);    
      SolrInputDocument myDocumentInstantlycommited = new SolrInputDocument();  
      
      myDocumentInstantlycommited.addField("id", "002"); 
      myDocumentInstantlycommited.addField("name", "Rahman"); 
      myDocumentInstantlycommited.addField("age","27"); 
      myDocumentInstantlycommited.addField("addr","hyderabad"); 
      
      updateRequest.add( myDocumentInstantlycommited);  
      UpdateResponse rsp = updateRequest.process(Solr); 
      System.out.println("Documents Updated"); 
   } 
}

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

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

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

Documents updated 
广告