solr 之 实时更新/添加--文档

1. vod例子,当然一下是中文,需要进行utf-8编码即可:

http://172.16.132.159:6602/sse/vod_media/update?wt=json&stream.body=<update><add><doc><field name="media_id">11010546488</field><field name="media_online" update="set">0</field></doc></add><add><doc><field name="media_id">11010546488</field><field name="vender_id" update="set">1001</field></doc></add><add><doc><field name="media_id">11010546488</field><field name="vender_id" update="add">1002</field></doc></add><add><doc><field name="media_id">11010546488</field><field name="vender_id" update="add">1003</field></doc></add></update>

注意:这个例子中,主要是说list的修改和添加!!!

 

2. 添加如下:

http://172.16.132.159:6602/sse/vod_media/update?wt=json&stream.body=<update><add><doc><field name="media_id">1111</field><field name="category_name">uuuu</field></doc></add></update>

如果1111已经存在,那么修改category_name,结果就是修改的,新添加字段的话,那么就再添加个字段

 

3. 以上都会有一点点延迟,如果想立即生效,加上commit即可

http://172.16.132.159:6602/sse/vod_media/update?wt=json&stream.body=<update><add><doc><field name="media_id">1111</field><field name="category_name">ufffu</field></doc></add></update>&commit=true

 

4. 详解:

Solr是一个拥有象WebService一样接口的独立运行的搜索服务器。它基于restful风格,并且扩展了Lucene.能够通过HTTP协议以XML格式将文档放入搜索服务器(索引),你能够通过HTTP协议的GET来查询搜索服务器并且得到XML格式的结果。

REST并不是一个协议或技术;它是一种体系结构风格。

REST 是 SOAP 的轻量型替代品,它是面向资源的,而不是面向操作的。它常常被归结为远程过程使用 HTTP 调用 GET、POST、PUT 和 DELETE 语句。

REST比较简单朴素,在安全性,可靠消息传输,或标准化的业务过程自动化上还没有定义。

一、通过http传递xml格式数据

 

[html] view plaincopy
 
  1. <add>                   
  2.   <doc>                     
  3.     <field name="employeeId">05991</field>                    
  4.     <field name="office">Bridgewater</field>                      
  5.     <field name="skills">Perl</field>                     
  6.     <field name="skills">Java</field>                     
  7.   </doc>                    
  8.   [<doc> ... </doc>[<doc> ... </doc>]]                    
  9. </add>                      
  10.                       
  11. <delete><id>05991</id></delete>                   
  12. <delete><query>office:Bridgewater</query></delete>                    
  13.                       


http://localhost:8983/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<add><doc><field name="id">testdoc</field></doc></add>'

 

http://localhost:8081/solr/update/?stream.body=<add><doc><field name="id">1</field><field name="author">bbb</field></doc></add>&commit=true

 

二、通过http传递json格式数据

 

[html] view plaincopy
 
  1. {                                         
  2. add: {                                        
  3.   "doc": {                                        
  4.     "id": "DOC1",                                         
  5.     "my_boosted_field": {        /* use a map with boost/value for a boosted field */                                         
  6.       "boost": 2.3,                                       
  7.       "value": "test"                                         
  8.     },                                        
  9.     "my_multivalued_field": [ "aaa", "bbb" ]   /* use an array for a multi-valued field */                                        
  10.   }                                       
  11. },                                        
  12. add: {                                        
  13.   "commitWithin": 5000,          /* commit this document within 5 seconds */                                          
  14.   "overwrite": false,            /* don't check for existing documents with the same uniqueKey */                                         
  15.   "boost": 3.45,                 /* a document boost */                                       
  16.   "doc": {                                        
  17.     "f1": "v1",                                       
  18.     "f1": "v2"                                        
  19.   }                                       
  20. },                                        
  21.                                           
  22. delete: { "id":"ID" },                               /* delete by ID */                                       
  23. delete: { "query":"QUERY" }                          /* delete by query */                                        
  24. delete: { "query":"QUERY", 'commitWithin':'500' }    /* delete by query, commit within 500ms */                                       
  25. }                                         


http://localhost:8983/solr/update -H 'Content-type:application/json' -d '

 

 {

  "id"        : "TestDoc1",

  "title"     : {"set":"test1"},

  "revision"  : {"inc":3},

  "publisher" : {"add":"TestPublisher"}

 }'

 

http://localhost:8081/solr/update/?stream.body={"add": {"doc":{"id":1, "author":"ccc"}}}&commit=true

 

Solr对外提供标准的http接口来实现对数据的索引的增加、删除、修改、查询。在Solr中,用户通过向部署在servlet 容器中的Solr Web应用程序发送 HTTP请求来启动索引和搜索。Solr接受请求,确定要使用的适当SolrRequestHandler,然后处理请求。通过 HTTP 以同样的方式返回响应。默认配置返回Solr的标准 XML 响应,也可以配置Solr的备用响应格式。

1.3.1     索引 

可以向Solr索引servlet传递四个不同的索引请求:

1)     add/update允许向Solr添加文档或更新文档。直到提交后才能搜索到这些添加和更新。

2)     commit 告诉Solr,应该使上次提交以来所做的所有更改都可以搜索到。

3)     optimize 重构 Lucene 的文件以改进搜索性能。索引完成后执行一下优化通常比较好。如果更新比较频繁,则应该在使用率较低的时候安排优化。一个索引无需优化也可以正常地运行。优化是一个耗时较多的过程。

4)     delete 可以通过 id 或查询来指定。按 id 删除将删除具有指定 id 的文档;按查询删除将删除查询返回的所有文档。

 

要实现添加文档索引则只需要调用搜索接口以HTTP POST的方式提交XML报文。(索引接口:http://192.168.10.85:18080/solr/update),下面是添加文档的示例报文:

注:多核心时为这个地址http://192.168.10.85:18080/solr/core0/update/

使用xml更新solr索引

Solr接受xml格式数据更新、提交、修改索引。

 

更新的Schema

不要混淆SCHEMA.XML

add/replace documents

 

Simple Example:

<!--[if !supportLists]-->·         <!--[endif]--><add>

<!--[if !supportLists]-->·         <!--[endif]-->  <doc>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="employeeId">05991</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="office">Bridgewater</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="skills">Perl</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="skills">Java</field>

<!--[if !supportLists]-->·         <!--[endif]-->  </doc>

<!--[if !supportLists]-->·         <!--[endif]-->  [<doc> ... </doc>[<doc> ... </doc>]]

</add>

注:多个文件,可以在一个单一的<add>命令指定。

optional属性为“add”

overwrite =“true”|“false” 默认是“true”,意味着新的文件具有相同的uniquekey取代以前添加的文件。

 

可选的属性“DOC”

boost = <float> — default is 1.0

这是一个方便的机制◦等同的刺激属性指定各个领域,支持规范(见下文)

optional属性为“field”

update = "add" | "set" | "inc"

boost = <float> — default is 1.0

◦NOTE: 确保规范启用 (omitNorms="false" in the schema.xml)

添加各种可选的属性文件的例子

Example of "add" with optional boost attribute:

<!--[if !supportLists]-->·         <!--[endif]--><add>

<!--[if !supportLists]-->·         <!--[endif]-->  <doc boost="2.5">

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="employeeId">05991</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="office" boost="2.0">Bridgewater</field>

<!--[if !supportLists]-->·         <!--[endif]-->  </doc>

</add>

 

 

Example of "add" with optional update attribute:

 

<!--[if !supportLists]-->·         <!--[endif]--><add>

<!--[if !supportLists]-->·         <!--[endif]-->  <doc>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="employeeId">05991</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="office" update="set">Walla Walla</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="skills" update="add">Python</field>

<!--[if !supportLists]-->·         <!--[endif]-->  </doc>

</add>

 

 

Example of "add" with optional update attribute to set multiple values on a multi-valued field:

<!--[if !supportLists]-->·         <!--[endif]--><add>

<!--[if !supportLists]-->·         <!--[endif]-->  <doc>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="employeeId">05991</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="skills" update="set">Python</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="skills" update="set">Java</field>

<!--[if !supportLists]-->·         <!--[endif]-->    <field name="skills" update="set">Jython</field>

<!--[if !supportLists]-->·         <!--[endif]-->  </doc>

</add>

 

Example of "add" with optional update attribute to set a field to null (i.e. delete a field):

 

<!--[if !supportLists]-->·         <!--[endif]--><add>
<!--[if !supportLists]-->·         <!--[endif]-->  <doc>
<!--[if !supportLists]-->·         <!--[endif]-->    <field name="employeeId">05991</field>
<!--[if !supportLists]-->·         <!--[endif]-->    <field name="skills" update="set" null="true" />
<!--[if !supportLists]-->·         <!--[endif]-->  </doc>
<!--[if !supportLists]-->·         <!--[endif]--></add>

 

"commit" and "optimize"

 

提交操作将会在新的查询请求中提现。

 

Optional attributes for "commit" and "optimize"

waitFlush = "true" | "false" — default is true,等到索引更改刷新到磁盘

waitSearcher = "true" | "false" — default is true 等到新搜索打开注册主查询搜索,使变化可见

softCommit = "true" | "false" — default is false 执行一个软的提交,这将在一个更高效的方式刷新索引的“视图”,但是没有“磁盘”的保证。

 

Optional attributes for "commit"

expungeDeletes = "true" | "false" — default is false 合并段删除掉

 

Optional attributes for "optimize"

<!--[if !supportLists]-->·         <!--[endif]--><commit waitSearcher="false"/>

<!--[if !supportLists]-->·         <!--[endif]--><commit waitSearcher="false" expungeDeletes="true"/>

<!--[if !supportLists]-->·         <!--[endif]--><optimize waitSearcher="false"/>

<!--[if !supportLists]-->·         <!--[endif]--> 

 

通过提交和commitwithin参数作为URL的一部分

curl http://localhost:8983/solr/update?commit=true -H "Content-Type: text/xml" --data-binary '<add><doc><field name="id">testdoc</field></doc></add>'

 

"delete" documents by ID and by Query

<!--[if !supportLists]-->·         <!--[endif]--><delete><id>05991</id></delete>

<delete><query>office:Bridgewater</query></delete>

 

<!--[if !supportLists]-->·         <!--[endif]--><delete>

<!--[if !supportLists]-->·         <!--[endif]-->  <id>05991</id><id>06000</id>

<!--[if !supportLists]-->·         <!--[endif]-->  <query>office:Bridgewater</query>

<!--[if !supportLists]-->·         <!--[endif]-->  <query>office:Osaka</query>

</delete>

 

posted @ 2014-12-11 11:08  清晰-模块-组合-优化  阅读(1325)  评论(0)    收藏  举报