使用json更新solr索引

使用json更新solr索引

Solr接受通过json的格式提交索引。

 

要求

Solr3.1是最早支持json格式提交索引的。

Json请求要求必须配置solrconfig.xml,在example例子中solrconfig.xml已经存在配置了。

 

  <requestHandler name="/update/json" class="solr.JsonUpdateRequestHandler"/>

solr4.0,JSON支持包括标准updaterequesthandler

 

  <requestHandler name="/update" class="solr.UpdateRequestHandler"/>

说明:request请求应该包括Content-type:application/json或者Content-type:text/json

 

发送JSON方法

JSON格式的更新请求可以通过/solr/update/json被发送到Sol。所有的正常方法上传内容的支持。

 

例子

有一个示例JSON文件的例子/ exampledocs / json,可用于添加文件到Solr服务器实例。

例子使用HTTP-POST 的方式提交JSON格式数据。

 

cd example/exampledocs
curl 'http://localhost:8983/solr/update/json?commit=true' --data-binary @books.json -H 'Content-type:application/json'

注意,我们添加了“commit =trueURL,文件将立即搜索。

你现在应该能够查询到新添加的文件,http://localhost:8983/solr/select?q=name:monsters&wt=json&indent=true

 

{
  "responseHeader":{
    "status":0,
    "QTime":2,
    "params":{
      "indent":"true",
      "wt":"json",
      "q":"title:monsters"}},
  "response":{"numFound":1,"start":0,"docs":[
      {
        "id":"978-1423103349",
        "author":"Rick Riordan",
        "series_t":"Percy Jackson and the Olympians",
        "sequence_i":2,
        "genre_s":"fantasy",
        "inStock":true,
        "price":6.49,
        "pages_i":304,
        "name":[
          "The Sea of Monsters"],
        "cat":["book","paperback"]}]
  }}

 

它从测试的目的和脚本命令行指定JSON文件也容易(假设一个UNIX环境):

 

URL=http://localhost:8983/solr/update/json
curl $URL -H 'Content-type:application/json' -d '
[
  {
    "id" : "MyTestDocument",
    "title" : "This is just a test"
  }
]'
curl "$URL?commit=true"

这是一个简单的添加一次多个文件的例子:

curl http://localhost:8983/solr/update/json -H 'Content-type:application/json' -d '
[
 {"id" : "TestDoc1", "title" : "test1"},
 {"id" : "TestDoc2", "title" : "another test"}
]'

 

更新命令

JSON更新处理程序接受所有的更新命令,XML更新处理程序支持的类型,通过一个简单的映射。请在命令的详细描述XML文档的更新。【XML格式更新索引.doc

多个命令可以被包含在一个message。下面是一个示例JSON更新消息显示多个更新命令(注:评论是不合法的JSON,但重复的名字是合法的)

 

{ 
"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 */
"delete": { "query":"QUERY", 'commitWithin':'500' }    /* delete by query, commit within 500ms */
}

 

正如在其他更新的处理程序,如提交的,commitwithin,优化参数,并覆盖可不是消息的正文中指定的URL

 

Solr 3.1 Example

Solr 3.2是支持JSONObject语法数组的第一个版本,所以在Solr 3.1需要使用重复的名称(添加标签)添加一次多个文件。有重复的名称在JSON是合法的。的例子

curl http://localhost:8983/solr/update/json -H 'Content-type:application/json' -d '
{
 "add": {"doc": {"id" : "TestDoc1", "title" : "test1"} },
 "add": {"doc": {"id" : "TestDoc2", "title" : "another test"} }
}'

 

Solr 4.0 Example

原子更新

Solr4.0支持原子更新

curl http://localhost:8983/solr/update -H 'Content-type:application/json' -d '
[
 {
  "id"        : "TestDoc1",
  "title"     : {"set":"test1"},
  "revision"  : {"inc":3},
  "publisher" : {"add":"TestPublisher"}
 },
 {
  "id"        : "TestDoc2",
  "publisher" : {"add":"TestPublisher"}
 }
]'

 

乐观并发原子更新

 

Solr 4支持带有一个建设中的_version_场是由Solr自动添加,可以让你在执行原子更新optimistic_concurrency。的例子:

curl http://localhost:8983/solr/update -H 'Content-type:application/json' -d '
[
 {
  "id"        : "TestDoc1",
  "title"     : {"set":"test1"},
  "revision"  : {"inc":3},
  "publisher" : {"add":"TestPublisher"}
  "_version_" : {12345}
 }
]'

 参考网址:http://wiki.apache.org/solr/UpdateJSON

 

 

猜你喜欢

转载自501565246-qq-com.iteye.com/blog/2075278