percolator類型json
percolator字段類型解析json結構到本地查詢並存儲到索引中。所以能夠用percolate查詢來匹配提供的文檔。這種狀況能夠理解正常搜索的反方向,通常狀況下咱們索引一個文檔,而後經過搜索進行查詢。percolator是先存儲搜索,而後用文檔來進行查詢是否匹配搜索。app
任何含有json對象的列能夠被配置成percolator字段,例以下面的配置是映射percolator字段類型,這種類型適用於percolate查詢:ui
{ "properties": { "query": { "type": "percolator" } } }
那麼下面的JSON代碼段能夠被索引做爲一個本地查詢:對象
{ "query" : { "match" : { "field" : "value" } } }
重要:percolator查詢必須已經存在於與所使用的percolation索引相關聯的映射中,爲了確保這些字段的存在,經過建立索引或者設置映射來增長或者更新。percolator類型能夠存在於任何索引的任何類型中。一個索引中只能有一個percolator字段類型.索引
percolate查詢ci
percolate查詢能夠用於將存儲在索引中的查詢進行匹配。例如建立兩個映射的索引:文檔
PUT /my-index { "mappings": { "doctype": { "properties": { "message": { "type": "text" } } }, "queries": { "properties": { "query": { "type": "percolator" } } } } }
doctype映射是在percolator查詢中被索引到一個臨時索引以前用於預處理文件的映射。it
queries映射用於索引查詢文檔。query字段將產生一個JSON對象表明一個實際的Elasticsearch查詢。query字段配置爲percolator字段類型,此字段類型理解爲以這樣的一種方式進行查詢dsl和存儲這些查詢,在定義了percolate查詢時它能夠用於之後的匹配文檔。io
在percolator中登記一個查詢:ast
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)原創,轉載請標明做者和出處。