天天看點

elasticsearch 的 Percolator操作

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/percolate.html

https://berlinbuzzwords.de/session/elasticsearch-percolator

Percolator翻譯為過濾器,抽出器的意思。

percolator 操作通過注冊查詢條件,然後通過percolate請求(包含文檔),來查詢符合該請求的查詢條件;可以用來不存儲資料,隻存儲查詢,做為告警使用;

以前我們是通過query來查詢文檔,Percolator則是通過文檔來查詢query.

傳統設計基于資料的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

一、CURL測試

根據mapping,建立一個index, field:message

curl -XPUT 'localhost:/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" : ,
    "_shards" : {
        "total" : ,
        "successful" : ,
        "failed" : 
    },
    "total" : ,
    "matches" : [
        {
          "_index" : "my-index",
          "_id" : "1"
        }
    ]
}
           

二、JAVA API方式:

/**
 * percolator api操作示例
 * @param IndexName 索引名
 * @param indexType 索引類型
 */
public void  percolator(String IndexName,String indexType) {
    //ES操作用戶端
    Client client = getClient();

    //This is the query we're registering in the percolator
    //建立查詢條件,并把該查詢條件注冊到索引中
    QueryBuilder qb = QueryBuilders.termQuery("areaName", "寶安");
    try {
        //Index the query = register it in the percolator
        //把查詢條件添加到索引中,myDesignatedQueryName為定義的查詢名 
        client.prepareIndex(IndexName, ".percolator", "myDesignatedQueryName")
            .setSource( XContentFactory.jsonBuilder()
                .startObject()
                    // Register the query,添加查詢記錄
                    .field("query", qb) 
                .endObject())
            .setRefresh(true) // Needed when the query shall be available immediately
            .execute().actionGet();
        //上面的term查詢定義名為:myDesignatedQueryName       

        //為了驗證一個文檔是否符合該查詢,建立以下代碼:建構一個文檔
        //Build a document to check against the percolator
        XContentBuilder docBuilder = XContentFactory.jsonBuilder().startObject();
        //This is needed to designate the document
        docBuilder.field("doc").startObject(); 
        docBuilder.field("areaName", "寶安");
        docBuilder.endObject(); //End of the doc field
        docBuilder.endObject(); //End of the JSON root object

        //Percolate查詢
        PercolateResponse response = client.preparePercolate()
                                .setIndices(IndexName)
                                //文檔建立時的mapping配置資訊名,
                                //如果未修改的話就是索引的type資訊
                                .setDocumentType(indexType)
                                //docBuilder建構的文檔
                              .setSource(docBuilder).execute().actionGet();

        //Iterate over the results,疊代代碼結果,結果處理
       //擷取查詢query後處理邏輯
        for(PercolateResponse.Match match : response) {
        //建立percolate時指定的ID,根據查詢ID在做相應的操作吧
        System.out.println("percolate ID: "+match.getId());
            System.out.println("percolate Index Name: " +match.getIndex());
            //Handle the result which is the name of
            //the query in the percolator
        }       
    } catch (ElasticsearchException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
           

三、應用場景

https://berlinbuzzwords.de/session/elasticsearch-percolator需要翻牆軟體通路

Percolator的這種特性對于以下的應用是非常好的,如資料分類(classification)、資料路由( data aware routing)、事件監控和預警( even alerting and monitoring of events)

事件監控和報警應用:

存儲和注冊查詢過濾條件,監控資料,終端使用者可以自定義自己的監控條件;

如:價格監控、新聞監控、股票監控、天氣監控

繼續閱讀