Solr記錄-solr文檔xml

Solr添加文檔(XML)

在上一章中,咱們學習解釋瞭如何向Solr中添加JSON.CSV文件格式的數據。在本章中,將演示如何使用XML文檔格式在Apache Solr索引中添加數據。html

示例數據

假設咱們須要使用XML文件格式將如下數據添加到Solr索引。java

Student ID First Name Last Name Phone City
001 Rajiv Reddy 9848022337 Hyderabad
002 Siddharth Bhattacharya 9848022338 Kolkata
003 Rajesh Khanna 9848022339 Delhi
004 Preethi Agarwal 9848022330 Pune
005 Trupthi Mohanty 9848022336 Bhubaneshwar
006 Archana Mishra 9848022335 Chennai


使用XML添加文檔

要將上述數據添加到Solr索引中,咱們須要準備一個XML文檔,以下所示。 將此文檔保存在名稱爲sample.xml的文件中。shell

<add> <doc> <field name = "id">001</field> <field name = "first name">Rajiv</field> <field name = "last name">Reddy</field> <field name = "phone">9848022337</field> <field name = "city">Hyderabad</field> </doc> <doc> <field name = "id">002</field> <field name = "first name">Siddarth</field> <field name = "last name">Battacharya</field> <field name = "phone">9848022338</field> <field name = "city">Kolkata</field> </doc> <doc> <field name = "id">003</field> <field name = "first name">Rajesh</field> <field name = "last name">Khanna</field> <field name = "phone">9848022339</field> <field name = "city">Delhi</field> </doc> <doc> <field name = "id">004</field> <field name = "first name">Preethi</field> <field name = "last name">Agarwal</field> <field name = "phone">9848022330</field> <field name = "city">Pune</field> </doc> <doc> <field name = "id">005</field> <field name = "first name">Trupthi</field> <field name = "last name">Mohanthy</field> <field name = "phone">9848022336</field> <field name = "city">Bhuwaeshwar</field> </doc> <doc> <field name = "id">006</field> <field name = "first name">Archana</field> <field name = "last name">Mishra</field> <field name = "phone">9848022335</field> <field name = "city">Chennai</field> </doc> </add> 
XML

正如所看到的,寫入添加數據到索引的XML文件包含三個重要的標籤,<add> </add><doc></doc>, 以及 < field >< /field >apache

  • add − 這是用於將文檔添加到索引的根標記。它包含一個或多個要添加的文檔。
  • doc − 添加的文檔應該包含在<doc> </ doc>標記中。文檔包含字段形式的數據。
  • field − 字段標記包含文檔的字段的名稱和值。

準備好文檔後,可使用上一章中討論的任何方法將此文檔添加到索引。json

假設XML文件(sample.xml)存在於Solrbin目錄中,而且它將在名稱爲my_core的核心中進行索引,那麼可使用post工具將其添加到Solr索引中,以下所示 -ubuntu

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

執行上述命令後,將獲得如下輸出 -api

yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c my_core sample.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 sample.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 sample.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.756
Shell

驗證上面的操做

訪問Apache Solr Web界面的主頁並選擇核心my_core。嘗試經過在文本區域q中傳遞查詢「:」來檢索全部文檔,並執行查詢。執行時應該能夠觀察到所需的數據被添加到Solr索引。app

 

Solr更新文檔數據

使用XML更新文檔

如下是用於更新現有文檔中的字段的XML文件。將下面的內容保存在名稱爲update.xml的文件中。yii

<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文件就相似以前用來添加文檔的XML文件。 但惟一的區別是這裏使用字段的一個update屬性。ide

在這個示例中,咱們將使用上述文檔並嘗試更新id001文檔的字段。

假設XML文檔(update.xml)存在於Solr的bin目錄中。更新的核心是名稱爲my_core的索引,可使用post工具更新以下 -

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

執行上述命令後,將獲得如下輸出 -

yiibai@ubuntu:/usr/local/solr-6.4.0/bin$ ./post -c my_core update.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 update.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 update.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.246
Shell

驗證修改結果

訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試經過在文本區域q中傳遞查詢「」來檢索全部文檔,並執行查詢。 執行時能夠觀察到文檔已經更新了。以下圖所示 -

Solr刪除文檔數據

刪除文檔

要從Apache Solr的索引中刪除文檔,咱們須要在<delete> </ delete>標記之間指定要刪除的文檔的ID

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

這裏,此XML代碼用於刪除ID003005的文檔。將此代碼保存在名稱爲delete.xml的文件中。

若是要從屬於名稱爲my_core的核心的索引中刪除文檔,則可使用post工具發佈delete.xml文件,以下所示。

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

執行上述命令後,將獲得如下輸出 -

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
XML

驗證執行結果

訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試經過在文本區域q中傳遞查詢「」來檢索全部文檔,並執行查詢。 執行時能夠觀察到指定的文檔(ID003005)已刪除。

刪除字段

有時,須要基於除ID之外的字段來刪除文檔。例如,可能須要刪除城市是Chennai的文檔。

在這種狀況下,須要在<query> </ query>標記對中指定字段的名稱和值。

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

將上面代碼保存到delete_field.xml文件中,並使用Solr的post工具在覈心my_core上執行刪除操做。

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

執行上述命令後,將產生如下輸出。

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
Shell

驗證執行結果

訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試經過在文本區域q中傳遞查詢「」來檢索全部文檔,並執行查詢。 執行時能夠觀察到包含指定字段值對的文檔被刪除。

刪除全部文檔

相似刪除一個指定刪除某個字段同樣,若是想刪除索引中的全部文檔,只須要在標籤<query> </ query>之間傳遞符號「」,以下所示。

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

將上面代碼保存到delete_all.xml文件中,並使用Solr的post工具對核心my_core執行刪除操做。

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

執行上述命令後,將產生如下輸出。

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
Shell

驗證執行結果

訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試經過在文本區域q中傳遞查詢「」來檢索全部文檔,並執行查詢。執行時您能夠觀察到包含指定字段值對的文檔全被刪除了。

使用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"); } } 
Java

經過在終端中執行如下命令編譯上述代碼 -

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

執行上述命令後,將獲得如下輸出。

Documents deleted
相關文章
相關標籤/搜索