Elasticsearch的javaAPI之percolator

Elasticsearch的javaAPI之percolator

 

percolator同意一个在index中注冊queries,然后发送包括doc的请求,返回得到在index中注冊过的而且匹配doc的query

//This is the query we're registering in the percolator
QueryBuilder qb = termQuery("content", "amazing");

//Index the query = register it in the percolator
client.prepareIndex("myIndexName", ".percolator", "myDesignatedQueryName")
    .setSource(jsonBuilder()
        .startObject()
            .field("query", qb) // Register the query
        .endObject())
    .setRefresh(true) // Needed when the query shall be available immediately
    .execute().actionGet();

在上面的index中query名为myDesignatedQueryName

为了检查文档注冊查询,使用这个 代码:

//Build a document to check against the percolator
XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
docBuilder.field("doc").startObject(); //This is needed to designate the document
docBuilder.field("content", "This is amazing!");
docBuilder.endObject(); //End of the doc field
docBuilder.endObject(); //End of the JSON root object
//Percolate
PercolateResponse response = client.preparePercolate()
                        .setIndices("myIndexName")
                        .setDocumentType("myDocumentType")
                        .setSource(docBuilder).execute().actionGet();
//Iterate over the results
for(PercolateResponse.Match match : response) {
    //Handle the result which is the name of
    //the query in the percolator
}

 

传统设计基于数据的documents,并将它们存储到一个index中,然后通过搜索api定义的查询。获取这些documents。Percolator正好相反,首先你储存到一个查询到index,然后通过percolatorapi以获取这些查询。

查询能够存储的原因来自这样一个事实:在Elasticsearch中document和query都定义为json格式。这同意您通过index api将query嵌入到document中。 Elasticsearch能够依赖percolator,通过document来提取查询。 既然document也定义为json,您能够定义一个percolator在document的请求中。

percolator和它的大部分功能在实时工作,所以percolator query被存入,那么久能够使用percolator

依据mapping,创建一个index, field:message

curl -XPUT 'localhost:9200/my-index' -d '{
  "mappings": {
    "my-type": {
      "properties": {
        "message": {
          "type": "string"
        }
      }
    }
  }
}
注冊一个query到percolator中:
curl -XPUT 'localhost:9200/my-index/.percolator/1' -d '{
    "query" : {
        "match" : {
            "message" : "bonsai tree"
        }
    }
}'

用一个符合注冊的percolator query的document:

curl -XGET 'localhost:9200/my-index/message/_percolate' -d '{
    "doc" : {
        "message" : "A new bonsai tree in the office"
    }
}'

上面的请求将返回以下的信息:

{
    "took" : 19,
    "_shards" : {
        "total" : 5,
        "successful" : 5,
        "failed" : 0
    },
    "total" : 1,
    "matches" : [
        {
          "_index" : "my-index",
          "_id" : "1"
        }
    ]
}

原文地址:

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-percolate.html#search-percolate

http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/percolate.html

翻译欠佳,希望不会对大家造成误解

posted @ 2016-01-25 19:43  blfshiye  阅读(312)  评论(0编辑  收藏  举报