elasticsearch5.0.0中的percolator类型和percolate查询

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)原创,转载请标明做者和出处。

相关文章
相关标签/搜索