Elasticsearch的javaAPI之percolator
percolator同意一个在index中注冊queries,然后发送包括doc的请求,返回得到在index中注冊过的而且匹配doc的query
//This is the query we're registering in the percolatorQueryBuilder qb = termQuery("content", "amazing");//Index the query = register it in the percolatorclient.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 percolatorXContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();docBuilder.field("doc").startObject(); //This is needed to designate the documentdocBuilder.field("content", "This is amazing!");docBuilder.endObject(); //End of the doc fielddocBuilder.endObject(); //End of the JSON root object//PercolatePercolateResponse response = client.preparePercolate().setIndices("myIndexName").setDocumentType("myDocumentType").setSource(docBuilder).execute().actionGet();//Iterate over the resultsfor(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/client/java-api/current/percolate.html
翻译欠佳,希望不会对大家造成误解
相关文章
- Elasticsearch之文档的增删改查以及ik分词器
- ElasticSearch之Quick.ElasticSearch.Furion组件的使用
- Elasticsearch的javaAPI之percolator
- elasticsearch之mappings的其他设置:index、copy_to、对象属性、settings
- Elasticsearch之java的基本操作一
- ElasticSearch 学习记录之如任何设计可扩容的索引结构
- Elasticsearch学习之ES节点类型以及各种节点的分工
- Elasticsearch从入门到精通之Elasticsearch集群内的原理
- Elasticsearch之几个重要的分词器
- elasticsearch mappings之dynamic的三种状态