在上一章中,咱们学习解释了如何向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