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://: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://: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://: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 
廣告
© . All rights reserved.