【问题标题】:Match query which matches exactly to a field with shuffling匹配与改组的字段完全匹配的查询
【发布时间】:2020-01-09 04:32:33
【问题描述】:

我在Elasticsearch 中有一个字段,其中包含字符串数组。该字段具有标准分析器。

e.g

Document 1
{ field:["a b c d", "a b c x", "b a c y"]}

Document 2
{ field:["a b c"]}

现在我正在使用 100% 匹配的匹配查询来搜索“b c a”。 我得到了两个正确的文件。 但我只想要文档 2。 如何使用匹配查询仅获取 Document2?

【问题讨论】:

  • 你能发布映射吗?

标签: elasticsearch


【解决方案1】:

我正在使用名为 token_count 的数据类型 它将计算并存储每个文本的标记数。 此计数值可用于获取具有确切标记数的文档作为搜索文本

PUT testindex6
{
  "mappings": {
    "properties": {
      "search_text": {
        "type": "nested",
        "properties": {
          "text": {
            "type": "text",
            "fields": {
              "length": {
                "type": "token_count",
                "analyzer": "standard"
              }
            }
          }
        }
      }
    }
  }
}

数据:

[
      {
        "_index" : "testindex6",
        "_type" : "_doc",
        "_id" : "Il1fDm0B27hOMovb2NOC",
        "_score" : 1.0,
        "_source" : {
          "search_text" : [
            {
              "text" : "a b c d"
            },
            {
              "text" : "a b c d e"
            }
          ]
        }
      },
      {
        "_index" : "testindex6",
        "_type" : "_doc",
        "_id" : "I11fDm0B27hOMovb99NK",
        "_score" : 1.0,
        "_source" : {
          "search_text" : [
            {
              "text" : "a b c"
            }
          ]
        }
      }
    ]

查询:

GET testindex6/_search
{
  "query": {
    "nested": {
      "path": "search_text",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "search_text.text": "b c a"
              }
            },
            {
              "term": {
                "search_text.text.length": {
                  "value": 3   ----> pass the number of tokens searched for 
                }
              }
            }
          ]
        }
      }
    }
  }
}

【讨论】:

  • 感谢您的回复。是的,token_count 数据类型在内部计算字符串长度,但为此我必须将字符串包装在一个对象中。我的意思是我必须将字符串数组转换为索引中的包装对象数组。
  • 是的,每个字符串都必须是一个对象(不一定是嵌套字段)才能工作
猜你喜欢
  • 1970-01-01
  • 2017-10-25
  • 2021-04-06
  • 1970-01-01
  • 2014-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多