在同一搜索字符串上同时处理完全搜索和部分搜索 [英] handling both exact and partial search on the same search string

查看:40
本文介绍了在同一搜索字符串上同时处理完全搜索和部分搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想定义一个模式,该模式可以解决相同搜索值的部分搜索和精确搜索.

I want to define the schema which can tackle the partial as well as the exact search for the same search value.

精确搜索应始终返回完全匹配",在这种情况下,ES不应将搜索字符串分解为标记.

The exact search should always return the "exact match", ES should not break the search string into tokens in this case.

推荐答案

对于部分匹配,属性的数据类型应为 text ,而对于确切的数据类型,应为 keyword .为了能够进行部分搜索和精确搜索,而不必将数据索引到不同的属性,您可以使用

For partial match data type of the property should be text and for exact it should be keyword. For having the feasibility to have both partial and exact search without having to index the data to different properties you can leverage using fields. What it does is that it helps to index same data into different ways.

因此,可以说您要索引人员的姓名,并具有部分和精确搜索的能力.在这种情况下,映射将为:

So, lets say you want to index name of persons, and have the ability for partial and exact search. In such case the mapping would be:

PUT test
{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword"
            }
          }
        }
      }
    }
  }
}

让一些文档编入索引:

PUT test/_doc/1
{
  "name": "Nishant Saini"
}

PUT test/_doc/2
{
  "name": "Nishant Kumar"
}

对于部分搜索,我们必须查询 name 字段,该字段的类型为 text .

For partial search we have to query name field and it is of type text.

GET test/_doc/_search
{
  "query": {
    "query_string": {
      "query": "Nishant Saini",
      "field": [
        "name"
      ]
    }
  }
}

上面的查询将返回两个文档(1和2),因为在两个文档中,字段 name 都出现了一个令牌,即 Nishant .

The above query will return both docs (1 and 2) because one token i.e. Nishant appears in both the document for field name.

要进行精确搜索,我们需要查询 name.keyword .要执行完全匹配,我们可以使用以下术语查询:

For exact search we need to query on name.keyword. To perform exact match we can use term query as below:

{
  "query": {
    "term": {
      "name.keyword": "Nishant Saini"
    }
  }
}

这将仅匹配doc 1.

This would match doc 1 only.

这篇关于在同一搜索字符串上同时处理完全搜索和部分搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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