Solr记录-solr文档xml
Solr添加文档(XML)
在上一章中,我们学习解释了如何向Solr中添加JSON和.CSV文件格式的数据。在本章中,将演示如何使用XML文档格式在Apache Solr索引中添加数据。
示例数据
假设我们需要使用XML文件格式将以下数据添加到Solr索引。
| 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的文件中。
<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 >。
- add − 这是用于将文档添加到索引的根标记。它包含一个或多个要添加的文档。
- doc − 添加的文档应该包含在
<doc> </ doc>标记中。文档包含字段形式的数据。 - field − 字段标记包含文档的字段的名称和值。
准备好文档后,可以使用上一章中讨论的任何方法将此文档添加到索引。
假设XML文件(sample.xml)存在于Solr的bin目录中,并且它将在名称为my_core的核心中进行索引,那么可以使用post工具将其添加到Solr索引中,如下所示 -
[yiibai@ubuntu:/usr/local/solr-6.4.0/bin]$ ./post -c my_core sample.xml
执行上述命令后,将得到以下输出 -
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索引。
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文档(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中传递查询“:”来检索所有文档,并执行查询。 执行时可以观察到文档已经更新了。如下图所示 -
Solr删除文档数据
删除文档
要从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(客户端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");
}
}
通过在终端中执行以下命令编译上述代码 -
[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
Solr记录-solr文档xml的更多相关文章
- Solr 18 - 通过SolrJ局部更新Solr中的文档 (原子操作、非覆盖操作)
目录 1 需求分析 2 需求实现 2.1 pom.xml依赖 2.2 Java代码示例 3 补充说明 3.1 关于文档中_version_的取值说明 3.2 store=true/false的区别 1 ...
- Solr记录-solr内核与索引
Solr核心(内核) Solr核心(Core)是Lucene索引的运行实例,包含使用它所需的所有Solr配置文件.我们需要创建一个Solr Core来执行索引和分析等操作. Solr应用程序可以包含一 ...
- Solr记录-solr检索和查询数据
Solr检索数据 在本章中,我们将讨论如何使用Java Client API检索数据.假设有一个名为sample.csv的.csv文档,其中包含以下内容. 001,9848022337,Hyderab ...
- Solr开发参考文档(转)
Solr开发文档 Solr 是一种可供企业使用的.基于 Lucene 的搜索服务器,它支持层面搜索.命中醒目显示和多种输出格式.在这篇文章中,将介绍 Solr 并展示如何轻松地将其表现优异的全文本搜索 ...
- Solr记录-solr介绍及配置
Solr是一个开源搜索平台,用于构建搜索应用程序. 它建立在Lucene(全文搜索引擎)之上. Solr是企业级的,快速的和高度可扩展的. 使用Solr构建的应用程序非常复杂,可提供高性能. 为了在C ...
- Solr记录-solr基础内容
Solr架构(体系结构) 在本章中,我们将讨论Apache Solr的架构. 下图显示了Apache Solr的体系结构的框图. Solr架构 - 构件块以下是Apache Solr的主要构建块(组件 ...
- 新增记录txt文档
StringBuilder s = new StringBuilder(); s.Append("[" + dt.Rows[i]["Store"].Conver ...
- Solr 15 - Solr添加和更新索引的过程 (文档的路由细节)
目录 1 添加文档的细节 1.1 注册观察者 - watcher 1.2 文档的路由 - document route 1.2.1 路由算法 1.2.2 Solr路由的实现类 1.2.3 implic ...
- WebAPI使用多个xml文件生成帮助文档
一.前言 上篇有提到在WebAPI项目内,通过在Nuget里安装(Microsoft.AspNet.WebApi.HelpPage)可以根据注释生成帮助文档,查看代码实现会发现是基于解析项目生成的xm ...
随机推荐
- Reflux系列01:异步操作经验小结
写在前面 在实际项目中,应用往往充斥着大量的异步操作,如ajax请求,定时器等.一旦应用涉及异步操作,代码便会变得复杂起来.在flux体系中,让人困惑的往往有几点: 异步操作应该在actions还是s ...
- sass:常用备忘
一.变量 所有变量以$开头 $font_size: 12px; .container{ font-size: $font_size; } 如果变量嵌套在字符串中,需要写在#{}中 $side : le ...
- Selenium+Python自动化测试环境搭建和搭建过程遇到的问题解决
环境搭建: 第一步:安装Python 网址:https://www.python.org/ 按照如图提示安装,并且配置环境变量(安装时候选中pip会自动安装Python的包管理工具 pip,推荐选择 ...
- python+selenium安装方法
一.准备工具: 下载 python[python 开发环境] http://python.org/getit/ 下载 setuptools [python 的基础包工具] http://pypi.py ...
- 新手向:从不同的角度来详细分析Redis
最近对华为云分布式缓存产品Redis做了一些研究,于是整理了一些基本的知识拿出来与大家分享,首先跟大家分享的是,如何从不同的角度来详细使用Redis. 小编将从以下9个角度来进行详细分析,希望可以帮到 ...
- CodeMirror mode编写
Writing CodeMirror Modes Modes typically consist of a single JavaScript file. This file defines, in ...
- PAT甲题题解-1008. Elevator (20)-大么个大水题,这也太小瞧我们做题者的智商了
如题... #include <iostream> #include <cstdio> #include <algorithm> #include <cstr ...
- PAT甲题题解-1105. Spiral Matrix (25)-(模拟顺时针矩阵)
题意:给定N,以及N个数.找出满足m*n=N且m>=n且m-n最小的m.n值,建立大小为m*n矩阵,将N个数从大到下顺时针填入矩阵中. #include <iostream> #in ...
- ctr中的GBDT+LR的优点
1 为什么gbdt+lr优于gbdt? 其实gbdt+lr类似于做了一个stacking.gbdt+lr模型中,把gbdt的叶子节点作为lr的输入,而gbdt的叶子节点相当于它的输出y',用这个y'作 ...
- redis 事务,持久化,日志,主从,VM
redis目前对事务的支持比较简单,只能保证一个客户端连接发起事务中的命令可以连续执行,而中间不会插入其他客户端的命令. 1.事务 一般情况下,redis接收到一个客户端发送的命令,立刻执行并返回结果 ...