1.6.2 Uploading Data with Index Handlers
1.Uploading Data with Index Handlers
索引处理器就是Request Handlers,用于添加,更新,删除索引中的文档.另外,使用Tika抽取富文档数据,使用Data Import Handler抽取结构化数据.solr自然也就支持 XML, CSV 和 JSON格式的结构化文档.
配置并使用request handlers的推荐的方式就是使用基于names的path,在request url中映射path--但是如果request Dispatcher配置合适之后,request handlers也可以指定qt(query type)参数.
2.1 The Combined UpdateRequestHandler
联合的UpdateRequesthandler.
在solr4以后,有一个统一的update request handler的方式支持XML,CVS,JSON和javabin更新请求,授权使用ContentStreamLoader,基于ContentSream的Content-Type.
2.2 UpdateRequestHandler配置
默认的配置文件有更新请求处理器(update request handler)默认的配置
<requestHandler name="/update" class="solr.UpdateRequestHandler" />
2.3 XML Formatted Index Updates
使用 Content-type: application/xml Content-type:text/xml格式可以发送xml格式消息.
添加文档
update handler识别xml结构的数据的方式是很直接的:
- <add>元素引入一个或者多个文档添加到索引中
- <doc>元素引入组成一个文档的字段
- <field>元素代表了一个字段的内容
例子:
<add>
<doc>
<field name="authors">Patrick Eagar</field>
<field name="subject">Sports</field>
<field name="dd">796.35</field>
<field name="numpages">128</field>
<field name="desc"></field>
<field name="price">12.40</field>
<field name="title" boost="2.0">Summer of the all-rounder: Test and
championship
cricket in England 1982</field>
<field name="isbn">0002166313</field>
<field name="yearpub">1982</field>
<field name="publisher">Collins</field>
</doc>
<doc boost="2.5">
...
</doc>
</add>
每一个元素都有一些可以指定的属性:
命令 | 参数 | 参数描述 |
<add> | commitWithin=number | 在指定的毫秒数中添加文档 |
<add> | overwrite=boolean | 默认为ture |
<doc> | boost=float | 默认为1.0 设置文档的权值 |
<field> | boost=float | 默认为1.0 设置字段的权值 |
Commit and Optimize Operations
<commit>操作是将自从上次提交之后的所有文档写入到硬盘上.在提交之前,新的文档对搜素是不可见的,提交操作打开了一个新的searcher,触发任何配置的监听事件.
提交操作可以通过<commit/>来发布.也可以通过solrconfig.xml中的<autocommit>来触发.
<optimize>操作要求solr合并内部数据以提高查询性能.对于大索引,合并将会花费一部分时间.如果使用solr的复制(replication)机制跨多个系统分发搜索,注意在做一个优化操作之后,一个完整的索引被传递.
<commit> 和<optimize>的属性:
属性 | 描述 |
maxSegments | 默认为1,索引优化的片段不能超过这个值 |
waitFlush | 默认为true,阻塞直到索引发生变化,冲刷到磁盘上. |
waitSearcher | 默认为true,阻塞,直到打开一个新的searcher,并注册为主查询searcher.使这个改变可视的. |
expungeDeletes | 默认为false,合并片段,删除删除的文档. |
这里是一个提交和优化的例子:
<commit waitFlush="false" waitSearcher="false"/>
<commit waitFlush="false" waitSearcher="false" expungeDeletes="true"/>
<optimize waitFlush="false" waitSearcher="false"/>
Delete Operations
删除文档有两种方式:按照ID删除和按照query删除.不过如果是通过query删除,commitWithin会被忽略.一个删除消息中可以删除多个文档:
<delete>
<id>0002166313</id>
<id>0031745983</id>
<query>subject:sport</query>
<query>publisher:penguin</query>
</delete>
Rollback Operations
回滚操作恢复从上一次提交之后的所有增加,删除的数据.它既不调用任何监听事件又不创建新的searcher.它的语法简单<rollback/>
Using curl to Perform Updates with the Update Request Handler
可以使用--data-binary 选项来添加xml消息到curl命令中,生成一个HTTP POST请求.例如:
curl http://localhost:8983/update -H "Content-Type: text/xml" --data-binary
'
<add>
<doc>
<field name="authors">Patrick Eagar</field>
<field name="subject">Sports</field>
<field name="dd">796.35</field>
<field name="isbn">0002166313</field>
<field name="yearpub">1982</field>
<field name="publisher">Collins</field>
</doc>
</add>
'
对于发送一个包含XML消息的文件,可以使用:
curl http://localhost:8983/update -H "Content-Type: text/xml" --data-binary
@myfile.xml
简短的请求可以通过HTTP GET命令,URL-编码这个请求,注意"<" 和">"的转义:
curl http://localhost:8983/update?stream.body=%3Ccommit/%3E
solr响应:
<?xml version="1.0" encoding="UTF-8"?>
<response>
<lst name="responseHeader">
<int name="status">0</int>
<int name="QTime">127</int>
</lst>
</response>
如果失败,状态字段status非0.
2.4 Using XSLT to Transform XML Index Updates
UpdateRequestHandler允许索引任意的XML.使用<tr>参数用于XSLT transformation .你必须在solr/conf/xslt目录下有一个样式表.可以转换数据成期望的 format, <add><doc/></add>格式,并且使用tr参数指定样式表的名称.
这是一个XSLT样式表:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<add>
<xsl:apply-templates select="/random/document" />
</add>
</xsl:template>
<xsl:template match="document">
<doc boost="5.5">
<xsl:apply-templates select="*" />
</doc>
</xsl:template>
<xsl:template match="node">
<field name="{@name}">
<xsl:if test="@enhance!=''">
<xsl:attribute name="boost"><xsl:value-of select="@enhance" /></xsl:attribute>
</xsl:if>
<xsl:value-of select="@value" />
</field>
</xsl:template>
</xsl:stylesheet>
这个样式表将solr的XML搜索结果格式转换成Solr的更新XML语法.一个例子就是复制solr1.3的索引(没有CVS响应writer)到一个可以被索引到另外一个solr文件的格式(提供所有字段的存储).
http://localhost:8983/solr/select?q=*:*&wt=xslt&tr=updateXml.xsl&rows=1000
在更新的时候,你也可以使用XsltUpdateRequestHandler的样式表来转换索引:
curl "http://localhost:8983/solr/update?commit=true&tr=updateXml.xsl" -H
"Content-Type: text/xml" --data-binary @myexporteddata.xml
更多关于XML Update Request Handler的信息,参考. https://wiki.apache.org/solr/UpdateXmlMessages.
2.5 JSON Formated Index Updates
JSON格式的更新请求,需要发送/update的句柄,使用Content-Type: application/json或者Content-Type: text/json.
默认配置:
<requestHandler name="/update/json" class="solr.JsonUpdateRequestHandler">
<lst name="defaults">
<str name="stream.contentType">application/json</str>
</lst>
</requestHandler>
例子:
有一个JSON文件在example/exampledocs/books.json,可以添加文档到solr的服务中:
cd example/exampledocs
curl 'http://localhost:8983/solr/update/json?commit=true'
--data-binary @books.json -H 'Content-type:application/json'
URL中添加 commit=true,使文档立即可以搜索.
Update Commands
JSON的更新处理器(handler)接受所有的更新命令.一个消息中可以包含多个命令:
{
"add": {
"doc": {
"id": "DOC1",
"my_boosted_field": { /* use a map with boost/value for a boosted field */
"boost": 2.3,
"value": "test"
},
"my_multivalued_field": [ "aaa", "bbb" ] /* use an array for a multi-valued field */
}
},
"add": {
"commitWithin": 5000, /* commit this document within 5 seconds */
"overwrite": false, /* don't check for existing documents with the same uniqueKey */
"boost": 3.45, /* a document boost */
"doc": {
"f1": "v1",
"f1": "v2"
}
},
"commit": {},
"optimize": {
"waitFlush":false, "waitSearcher":false },
"delete": { "id":"ID" }, /* delete by ID */
"delete": { "query":"QUERY" } /* delete by query */
}
注意:json中是不允许注释的.
正如其他update handler一样, commit, commitWithin, optimize,overwrite也可以在URL中指定.
"delete":"myid"
"delete":["id1","id2"]
更多 JSON Update Request Handler的相关信息,参考 https://wiki.apache.org/solr/UpdateJSON.
2.6 CSV Formated Index Updates
CVS格式请求可以发送/update 句柄,使用Content-Type: application/csv或者Content-Type: text/csv.
默认配置:
<requestHandler name="/update/csv" class="solr.CSVRequestHandler">
<lst name="defaults">
<str name="stream.contentType">application/csv</str>
</lst>
</requestHandler>
1.6.2 Uploading Data with Index Handlers的更多相关文章
- 1.6.3 Uploading Data with Solr Cell using Apache Tika
1. Uploading Data with Solr Cell using Apache Tika solr使用Apache Tika工程的代码提供了一个框架,用于合并所有不同格式的文件解析器为so ...
- $.each(data, function (index, value) { })的用法;json和list<>的互相转换
在json中常常碰到这样的代码: jquery $.each(data, function (index, value) { }) 遍历处理data,可以是数组.DOM.json等,取决于直接给 ...
- Big Spatio temporal Data(R-tree Index and NN & RNN & Skyline)
一.简单介绍大数据技术产物 “大数据”一词首先出现在2008年9月<Nature>杂志发表的一篇名为“Big Data: Wikiomics”的文章上(Mitch,2008).“大数据科学 ...
- [React] Pass Data To Event Handlers with Partial Function Application
In this lesson we’ll see how to pass an item’s id value in an event handler and get the state to ref ...
- 1.6 Indexing and Basic Data Operations--目录
1.6.1 什么是 Indexing 1.6.2 Uploading Data with Index Handlers 1.6.3 Uploading Data with Solr Cell usin ...
- Mac下搭建solr搜索引擎与PHP扩展开发(上)
首先需要安装jdk,前往 https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html 自 ...
- php使用solr全文搜索引擎
前言 本来以为网上已经有了类似博文,不想重复,可是一圈搜下来,都是一些内容不甚明了的文章,或者solr版本太过老,参考价值不高,更有甚者,直接拷贝的别人的内容.一篇博客,各大平台都能看到,也不见转载链 ...
- solr数据操作
本文介绍solr的基本数据操作,基于solr 8.2.solr支持多种数据格式,包括XML,JSON,CSV等,并提供多种脚本和工具来操作数据.本文讲解curl请求和JSON数据格式的处理方式. 本文 ...
- 1.6.4 Uploading Structured Data Store Data with the Data Import Handler
1.使用DIH上传结构化数据 许多搜索应用索引结构化数据,如关系型数据库.DIH提供了一个这样的存储并索引结构化数据的机制.除了关系型数据库,solr可以索引来自HTTP的内容,基于数据源如RSS和A ...
随机推荐
- 借助LVS+Keepalived实现负载均衡(转)
原文:http://www.cnblogs.com/edisonchou/p/4281978.html 一.负载均衡:必不可少的基础手段 1.1 找更多的牛来拉车吧 当前大多数的互联网系统都使用了服务 ...
- Android内存管理之道
相信一步步走过来的Android从业者,每个人都会遇到OOM的情况.如何避免和防范OOM的出现,对于每一个程序员来说确实是一门必不可少的能力.今天我们就谈谈在Android平台下内存的管理之道,开始今 ...
- xml velocity模板
. <?xml version="1.0" encoding="GBK"?> <PACKET type="REQUEST" ...
- How to create custom methods for use in spring security expression language annotations
From:http://stackoverflow.com/questions/6632982/how-to-create-custom-methods-for-use-in-spring-secur ...
- C#核心基础--类(2)
C#核心基础--类的声明 类是使用关键字 class 声明的,如下面的示例所示: 访问修饰符 class 类名 { //类成员: // Methods, properties, fields, eve ...
- Windows 10 Edge浏览器、照片查看程序关闭“平滑滚动”
升级到10后,这两个常用软件的“平滑滚动”功能,个人感觉体验有点不好,特别是图片这个自带程序,看了几十张图后就有点头晕了,所以把它关闭为好: 控制面板\系统和安全\系统\高级系统设置\高级\性能\设置 ...
- Hadoop集群基准测试
hadoop jar ./share/hadoop/mapreduce/hadoop-mapreduce-client-jobclient-2.2.0-tests.jar TestDFSIO -wri ...
- chart.js接口开发:X轴步长和Labels旋转角
一. 当初为什么选择chart.js 当初项目使用库是Zepto,Zepto能支持的chart处理库太少.也是为了使得项目比较轻量化,所以选择了chart.js. 但是最后的显示结果实在太差,放弃了c ...
- 玩转变量、环境变量以及数学运算(shell)
变量和环境变量 var=value 给变量赋值,输出语句:$ echo $var或者是$ echo ${var},记住中间有个空格 例如:name="coffee" age ...
- EasyMock 使用方法与原理剖析
from:http://www.ibm.com/developerworks/cn/opensource/os-cn-easymock/ Mock 方法是单元测试中常见的一种技术,它的主要作用是模拟一 ...