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 ...
随机推荐
- java线性表学习笔记(二)
链表中的每一个元素都包含一个称为节点的结构,每向链表中增加一个元素,就会产生一个与之相关的节点,每个节点与它相邻的节点相连接(这是基础吧,不过在看c的时候没认真看,呼). 定义节点类如下(使用了泛型, ...
- POJ 3449 Geometric Shapes(判断几个不同图形的相交,线段相交判断)
Geometric Shapes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 1243 Accepted: 524 D ...
- app之间的互相跳转
第一次写博客,给大家带来的是:iOS开发中不同app之间的跳转,相信很多人也有用过友盟的SDK或者其他的第三方的分享工具,原理都是一样的. 跳转的实现分为四步: 第一步:建立两个工程,模仿两个App的 ...
- Labview中的属性节点
获取(读取)和/或设置(写入)引用的属性.通过属性节点对本地或远程应用程序实例.VI或对象获取或设置属性和方法也可通过属性节点访问LabVIEW类的私有数据. 属性节点可自动调整为用户所引用的对象的类 ...
- Eclipse 安装对 Java 8 的支持
Java 8 正式版今天已经发布了(详情),但最常用的 Java 开发工具 Eclipse 还没有正式发布对 Java 8 的支持.不过目前可以通过更新 JDT 来支持 Java 8.步骤如下: 菜单 ...
- Android LIstView初次创建getview方法执行多次问题
写listview优化的时候,发现Listview初次创建的时候会多次执行getView方法. <?xml version="1.0" encoding="utf- ...
- WinForm设置窗体默认控件焦点
winform窗口打开后文本框的默认焦点设置,进入窗口后默认聚焦到某个文本框,两种方法: ①设置tabindex 把该文本框属性里的tabIndex设为0,焦点就默认在这个文本框里了. ②Winfor ...
- uva193 - Graph Coloring
Graph Coloring You are to write a program that tries to find an optimal coloring for a given graph. ...
- python中使用list作为默认参数且调用时不给其赋值的问题
最近在写代码时发现一个有趣的地方,当python中的函数使用list作为默认参数且调用时不给其赋值时,无法通过在函数中将其赋值为[]来达到清空此默认参数的目的.按照道理来说,函数f1中的list为局部 ...
- jQuery + jQuery Mobile 实现省市二级下拉列表页面
一.需求: 提供省.市下拉列表,当用户选择省一级下拉列表项后,市下拉列表的各个选项自动变为该省对应的城市列表. 二.效果: 三.实现: 1.省市json数据,来自: http://www.cnblog ...