Elasticsearch的javaAPI之percolator
2016-01-25 19:43
423 查看
Elasticsearch的javaAPI之percolatorpercolator同意一个在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-percolatehttp://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/percolate.html翻译欠佳,希望不会对大家造成误解
相关文章推荐
- Struts2之文件上传
- The Java™ Tutorials — Generics :Why Use Generics? 为什么使用泛型
- 1,Java的移位操作效果; 2,移位如何实施的; 3, 原码补码反码原理
- 使用Sublime Text 3 编译并运行Java程序
- 使用spring websocket stomp遇到的3个问题总结:路径匹配/自动注入/activemq连接超时
- 开发工具之Git(将本地eclipse工程上传至GitCafe服务器)
- java项目打包后如何读写项目中的文件
- Export导出项目时报错at proguard.ConfigurationParser.parse(ConfigurationParser.java:191)
- Eclipse如何设置CodeTemplates
- java 页面url传值中文乱码的解决方法
- Java 三大特性 —— 多态
- RabbitMQ入门教程 For Java【9】 - 与Spring集成
- java I/O 学习笔记2 ,线程池
- java面向对象之封装
- java单例模式(线程安全,效率高,双重推断)
- Java正則表達式演示样例
- ExtJS5 (一) 超简单整合到eclipse中,搭建简单的开发环境
- Java基础学习10_多态
- Java 异常处理机制
- Java基础学习9_继承