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
相关文章
相关标签/搜索