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

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

Elasticsearch的javaAPI之percolator的更多相关文章

  1. elasticsearch的javaAPI之query

    elasticsearch的javaAPI之query API the Search API同意运行一个搜索查询,返回一个与查询匹配的结果(hits). 它能够在跨一个或多个index上运行, 或者一 ...

  2. Elasticsearch的javaAPI之get,delete,bulk

    Elsasticsearch的javaAPI之get get API同意依据其id获得指定index中的基于json document.以下的样例得到一个JSON document(index为twi ...

  3. Elasticsearch的javaAPI之query dsl-queries

    Elasticsearch的javaAPI之query dsl-queries 和rest query dsl一样,elasticsearch提供了一个完整的Java query dsl. 查询建造者 ...

  4. elasticsearch的javaAPI之index

    Index API 原文:http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/index_.html ...

  5. ElasticSearch的javaAPI之Client

    翻译的原文:http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/client.html#node-c ...

  6. Elasticsearch的JavaAPI

    获取客户端对象 public class App { private TransportClient client; //获取客户端对象 @Before public void getClinet() ...

  7. Elasticsearch JavaApi

    官网JavaApi地址:https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-search.html 博 ...

  8. ElasticSearch的基本用法与集群搭建

    一.简介 ElasticSearch和Solr都是基于Lucene的搜索引擎,不过ElasticSearch天生支持分布式,而Solr是4.0版本后的SolrCloud才是分布式版本,Solr的分布式 ...

  9. ElasticSearch Java api 详解_V1.0

    /×××××××××××××××××××××××××××××××××××××××××/ Author:xxx0624 HomePage:http://www.cnblogs.com/xxx0624/ ...

随机推荐

  1. SVN trunk、branch、tag的用法

    Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn://proj/,那么标准的svn布局是svn://proj/|+-trunk+-branches+-tag ...

  2. PHP - 多维数组

    多维数组指的是包含一个或多个数组的数组. PHP 能理解两.三.四或五级甚至更多级的多维数组.不过,超过三级深的数组对于大多数人难于管理. 注释:数组的维度指示您需要选择元素的索引数. 对于二维数组, ...

  3. [转]Traceroute网络排障实用指南(1)

    注:本文是同事的大作,虽是翻译的一篇英文PPT,但内容实在精彩,小小的Traceroute竟包含如此大的信息量,真是让人感慨!内容不涉及公司机密,所以一直想转到自己的Blog上来,自己需要时可以再翻阅 ...

  4. 第一个Spring MVC程序

    最近公司项目要开始使用Spring MVC替代Struts2了,就学习了一下Spring MVC的使用.这是第一个Spring mvc程序,分别使用xml和注解两种方式. 一.使用xml格式进行构建 ...

  5. OutputCache祥解

    当用户訪问页面时,整个页面将会被server保存在内存中,这样就对页面进行了缓存.当用户再次訪问该页,页面不会再次运行数据操作,页面首先会检查server中是否存在缓存,假设缓存存在,则直接从缓存中获 ...

  6. linux下C++对线程的封装

    之前一直是使用C语言,前段时间转做C++.无论使用什么语言,多线程编程都是不可或缺的.最近项目中又用到了线程,现在将线程的封装做出总结: 1.线程类中应该包含线程ID.线程的状态以及线程基本操作等. ...

  7. gdb零散学习心得。

    引用: 记录学习心得.未来将统一整理. 一.基础知识 1:细节整理. 1,启动GDB的前置要求. 1)使用gcc/g++时,添加-g的指令.例如:g++ -g test.cpp -o test 2)如 ...

  8. js获取当期日期累加天数

    本文是转载的,,忘记出处了,我用上了,也给大家分享一下 一.日期减去天数等于第二个日期 function cc(dd,dadd)...{//可以加上错误处理var a = new Date(dd)a ...

  9. [ofbiz]entitymode中类型的对照关系

    在实体数据结构的时候,习惯于数据库的设计模式,int,varchar等各种类型,但是在entitymode中不是直接使用数据库的类型模式,而是自己定义了一套数据类型(type). 如何找到两者之间的对 ...

  10. php递归函数,性能给力

    function arPro($data,$res=array(),$pid='0',$level='0'){ foreach ($data as $k => $v){ if($v['comme ...