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

原创
2016/11/20 23:03
阅读数 1.5K

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

展开阅读全文
加载中
点击引领话题📣 发布并加入讨论🔥
0 评论
0 收藏
0
分享
返回顶部
顶部