Elasticsearch匹配与过滤条件 [英] Elasticsearch match vs. term in filter

查看:303
本文介绍了Elasticsearch匹配与过滤条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在过滤器中,术语和匹配项之间没有任何区别:

I don't see any difference between term and match in filter:

POST /admin/_search
{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "partnumber": "j1knd"
                    }
                }
            ]
        }
    }
}

结果也包含不完全匹配的部件号,例如:"52527.J1KND-H"

And the result contains not exactly matched partnumbers too, e.g.: "52527.J1KND-H"

为什么?

推荐答案

不分析术语查询,这意味着将使用您发送的任何内容来匹配倒排索引中的标记,同时对匹配查询进行分析且相同分析器应用于字段,该索引在索引时间使用,并因此与文档匹配.

Term queries are not analyzed and mean whatever you send will be used as it is to match the tokens in the inverted index, while match queries are analyzed and the same analyzer applied on the fields, which is used at index time and accordingly matches the document.

详细了解术语查询匹配查询.如匹配查询中所述:

Read more about term query and match query. As mentioned in the match query:

返回与提供的文本,数字,日期或布尔值匹配的文档 价值.匹配之前对提供的文本进行分析.

Returns documents that match a provided text, number, date or boolean value. The provided text is analyzed before matching.

您还可以使用分析API 查看为特定字段生成的令牌.

You can also use the analyze API to see the tokens generated for a particular field.

POST /_analyze
{
    "text": "52527.J1KND-H",
    "analyzer" : "standard"
}

{
    "tokens": [
        {
            "token": "52527",
            "start_offset": 0,
            "end_offset": 5,
            "type": "<NUM>",
            "position": 0
        },
        {
            "token": "j1knd",
            "start_offset": 6,
            "end_offset": 11,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "h",
            "start_offset": 12,
            "end_offset": 13,
            "type": "<ALPHANUM>",
            "position": 2
        }
    ]
}

上面向您解释了为什么您也得到了不完全匹配的partnumbers,例如:"52527.J1KND-H",我将以您为例,以及如何使它工作.

Above explain to you why you are getting the not exactly matched partnumbers too, e.g.: "52527.J1KND-H", I would take your example and how you can make it work.

{
  "mappings": {
    "properties": {
      "partnumber": {
        "type": "text",
        "fields": {
          "raw": { 
            "type":  "keyword" --> note this
          }
        }
      }
    }
  }
}

索引文档

{
  "partnumber" : "j1knd"
}

{
  "partnumber" : "52527.J1KND-H"
}

搜索查询仅返回完全匹配

{
    "query": {
        "bool": {
            "filter": [
                {
                    "term": {
                        "partnumber.raw": "j1knd" --> note `.raw` in field
                    }
                }
            ]
        }
    }

结果

 "hits": [
      {
        "_index": "so_match_term",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.0,
        "_source": {
          "partnumber": "j1knd"
        }
      }
    ]

    }

这篇关于Elasticsearch匹配与过滤条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆