在上一章中,咱們學習解釋瞭如何向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 |
要將上述數據添加到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
文件包含三個重要的標籤,<add> </add>
, <doc></doc>
, 以及 < field >< /field >
。apache
<doc> </ doc>
標記中。文檔包含字段形式的數據。準備好文檔後,可使用上一章中討論的任何方法將此文檔添加到索引。json
假設XML
文件(sample.xml
)存在於Solr的bin
目錄中,而且它將在名稱爲my_core
的核心中進行索引,那麼可使用post
工具將其添加到Solr索引中,以下所示 -ubuntu
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core sample.xml
執行上述命令後,將獲得如下輸出 -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
訪問Apache Solr Web界面的主頁並選擇核心my_core
。嘗試經過在文本區域q
中傳遞查詢「:」來檢索全部文檔,並執行查詢。執行時應該能夠觀察到所需的數據被添加到Solr索引。app
如下是用於更新現有文檔中的字段的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
文件。 但惟一的區別是這裏使用字段的一個update
屬性。ide
在這個示例中,咱們將使用上述文檔並嘗試更新id
爲001
文檔的字段。
假設XML文檔(update.xml
)存在於Solr的bin目錄中。更新的核心是名稱爲my_core
的索引,可使用post
工具更新以下 -
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core update.xml
執行上述命令後,將獲得如下輸出 -
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
訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試經過在文本區域q
中傳遞查詢「:
」來檢索全部文檔,並執行查詢。 執行時能夠觀察到文檔已經更新了。以下圖所示 -
要從Apache Solr的索引中刪除文檔,咱們須要在<delete> </ delete>
標記之間指定要刪除的文檔的ID
。
<delete> <id>003</id> <id>005</id> </delete>
這裏,此XML代碼用於刪除ID
爲003
和005
的文檔。將此代碼保存在名稱爲delete.xml
的文件中。
若是要從屬於名稱爲my_core
的核心的索引中刪除文檔,則可使用post
工具發佈delete.xml
文件,以下所示。
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete.xml
執行上述命令後,將獲得如下輸出 -
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
訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試經過在文本區域q
中傳遞查詢「:
」來檢索全部文檔,並執行查詢。 執行時能夠觀察到指定的文檔(ID
爲003
和005
)已刪除。
有時,須要基於除ID
之外的字段來刪除文檔。例如,可能須要刪除城市是Chennai
的文檔。
在這種狀況下,須要在<query> </ query>
標記對中指定字段的名稱和值。
<delete> <query>city:Chennai</query> </delete>
將上面代碼保存到delete_field.xml
文件中,並使用Solr的post
工具在覈心my_core
上執行刪除操做。
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete_field.xml
執行上述命令後,將產生如下輸出。
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
訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試經過在文本區域q
中傳遞查詢「:
」來檢索全部文檔,並執行查詢。 執行時能夠觀察到包含指定字段值對的文檔被刪除。
相似刪除一個指定刪除某個字段同樣,若是想刪除索引中的全部文檔,只須要在標籤<query> </ query>
之間傳遞符號「:
」,以下所示。
<delete> <query>*:*</query> </delete>
將上面代碼保存到delete_all.xml
文件中,並使用Solr的post
工具對核心my_core
執行刪除操做。
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core delete_all.xml
執行上述命令後,將產生如下輸出。
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
訪問Apache Solr Web界面的主頁,選擇核心 - my_core。 嘗試經過在文本區域q
中傳遞查詢「:
」來檢索全部文檔,並執行查詢。執行時您能夠觀察到包含指定字段值對的文檔全被刪除了。
如下是使用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"); } }
經過在終端中執行如下命令編譯上述代碼 -
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ javac DeletingAllDocuments.java [yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ java DeletingAllDocuments
執行上述命令後,將獲得如下輸出。
Documents deleted