percolator類型
percolator字段類型解析json結構到本地查詢並存儲到索引中。因此可以用percolate查詢來匹配提供的文檔。這種情況可以理解正常搜索的反方向,一般情況下我們索引一個文檔,然後通過搜索進行查詢。percolator是先存儲搜索,然後用文檔來進行查詢是否匹配搜索。
任何含有json對象的列可以被配置成percolator字段,例如下面的配置是映射percolator字段類型,這種類型適用於percolate查詢:
{ "properties": { "query": { "type": "percolator" } } }
那麼下面的JSON代碼段可以被索引作爲一個本地查詢:
{ "query" : { "match" : { "field" : "value" } } }
重要:percolator查詢必須已經存在於與所使用的percolation索引相關聯的映射中,爲了確保這些字段的存在,通過創建索引或者設置映射來增加或者更新。percolator類型可以存在於任何索引的任何類型中。一個索引中只能有一個percolator字段類型.
percolate查詢
percolate查詢可以用於將存儲在索引中的查詢進行匹配。例如創建兩個映射的索引:
PUT /my-index { "mappings": { "doctype": { "properties": { "message": { "type": "text" } } }, "queries": { "properties": { "query": { "type": "percolator" } } } } }
doctype映射是在percolator查詢中被索引到一個臨時索引之前用於預處理文件的映射。
queries映射用於索引查詢文檔。query字段將產生一個JSON對象代表一個實際的Elasticsearch查詢。query字段配置爲percolator字段類型,此字段類型理解爲以這樣的一種方式進行查詢dsl和存儲這些查詢,在定義了percolate查詢時它可以用於以後的匹配文檔。
在percolator中登記一個查詢:
PUT /my-index/queries/1?refresh { "query" : { "match" : { "message" : "bonsai tree" } } }
在登記percolator的查詢中匹配文檔:
GET /my-index/_search { "query" : { "percolate" : { "field" : "query", "document_type" : "doctype", "document" : { "message" : "A new bonsai tree in the office" } } } }
響應爲:
{ "took": 13, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 0.5716521, "hits": [ { "_index": "my-index", "_type": "queries", "_id": "1", "_score": 0.5716521, "_source": { "query": { "match": { "message": "bonsai tree" } } } } ] } }
在處的查詢匹配文檔。
查詢的參數:
field:定義percolator字段類型的字段,必填。
document_type:映射的穩定字段,必填。
document:需要匹配的原始文檔。document文檔同時也可以是存儲在索引中的文檔。在這種情況下,文檔參數可以被替換爲以下參數:index,type,id,routing,preference,version。
在前面的例子的基礎上,插入我們要percolate的文件索引:
PUT /my-index/message/1 { "message" : "A new bonsai tree in the office" }
返回值:
{ "_index": "my-index", "_type": "message", "_id": "1", "_version": 1, "_shards": { "total": 2, "successful": 1, "failed": 0 }, "created": true, "result": "created" }
Percolating已經存在的文檔,可以使用搜索索引,類型,id等方式進行替換document參數進行查詢,例如:
GET /my-index/_search { "query" : { "percolate" : { "field": "query", "document_type" : "doctype", "index" : "my-index", "type" : "message", "id" : "1", "version" : 1 } } }
這個時候document參數被替換成了index,type,id,version。
version是可選的,但在某些情況下是有用的。
這個搜索的放回值和之前的返回值是一樣的。
本文由賽克 藍德(secisland)原創,轉載請標明作者和出處。
percolate查詢高亮顯示
percolate查詢同時也支持高亮顯示,例如保存兩查詢:查詢1
PUT /my-index/queries/1?refresh { "query" : { "match" : { "message" : "brown fox" } } }
查詢2
PUT /my-index/queries/2?refresh { "query" : { "match" : { "message" : "lazy dog" } } }
高亮查詢設置:
GET /my-index/_search { "query" : { "percolate" : { "field": "query", "document_type" : "doctype", "document" : { "message" : "The quick brown fox jumps over the lazy dog" } } }, "highlight": { "fields": { "message": {} } } }
返回值如下:
{ "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 0.5446649, "hits": [ { "_index": "my-index", "_type": "queries", "_id": "2", "_score": 0.5446649, "_source": { "query": { "match": { "message": "lazy dog" } } }, "highlight": { "message": [ "The quick brown fox jumps over the <em>lazy</em> <em>dog</em>" ] } }, { "_index": "my-index", "_type": "queries", "_id": "1", "_score": 0.5446649, "_source": { "query": { "match": { "message": "brown fox" } } }, "highlight": { "message": [ "The quick <em>brown</em> <em>fox</em> jumps over the lazy dog" ] } } ] } }
本文由賽克 藍德(secisland)原創,轉載請標明作者和出處。