【问题标题】:ElasticSearch query not returns exact match of an arrayElasticSearch 查询不返回数组的完全匹配
【发布时间】:2020-03-25 20:08:42
【问题描述】:

我有一个关于 Elasticsearch 数组查询的问题。在我的例子中,自定义属性的结构是一个对象数组,每个对象包含inner_namevalue,值的类型是混合的(可以是字符串、数字、数组、日期等),其中一个类型是多复选框,它应该将数组作为输入。如下映射custom_attributes

"attributes" : {
              "properties" : {
                "_id" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "inner_name" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                },
                "value" : {
                  "type" : "text",
                  "fields" : {
                    "keyword" : {
                      "type" : "keyword",
                      "ignore_above" : 256
                    }
                  }
                }
              }
            },

我使用mongoosastic 将我的 MongoDB 索引到 ES,所以自定义属性的结构是这样的:

[
  {
    customer_name: "customerX",
    "custom_attributes" : [
      {
        "group" : "xyz",
        "attributes" : [
          {
            "inner_name" : "attr1",
            "value" : 123,
          },       
          {
            "inner_name" : "attr2",
            "value" : [
              "Val1",
              "Val2",
              "Val3",
              "Val4"
            ]
          }
        ]
      }
    ]
  },
  {
    customer_name: "customerY",
    "custom_attributes" : [
      {
        "group" : "xyz",
        "attributes" : [
          {
          "inner_name" : "attr2",
            "value" : [
              "Val1",
              "Val2"
            ]
          }
        ]
      }
    ]
  }
]

我想执行一个查询,其中所有值都必须在数组中。但是,以下查询的问题在于,只要它包含数组中的任何值,它就会返回文档。这是查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "custom_attributes.attributes.inner_name": "attr2"
          }
        },
        {
          "terms": {
            "custom_attributes.attributes.value": [
              "val1",
              "val2",
              "val3",
              "val4"
            ]
          }
        }
      ]
    }
  }
}

例如,它返回两个文档,它应该只返回第一个!我的查询有什么问题?有没有另一种方法来编写查询?

【问题讨论】:

  • 有人可以帮忙吗?!

标签: elasticsearch elastic-stack mongoosastic


【解决方案1】:

如果您的任何值出现在文档中,elasticsearch 术语查询会尝试匹配任何值,请考虑运算符为 OR 而不是 AND,这是您想要的。有两种解决方案

  1. 在 bool 必须查询中使用多个 term 查询,这将提供所需的 AND 功能
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "custom_attributes.attributes.inner_name": "attr2"
          }
        },
        {
          "term": {
            "custom_attributes.attributes.value": "val1"
          }
        },
        {
          "term": {
            "custom_attributes.attributes.value": "val2"
          }
        },
        {
          "term": {
            "custom_attributes.attributes.value": "val3"
          }
        },
        {
          "term": {
            "custom_attributes.attributes.value": "val4"
          }
        }
      ]
    }
  }
}
  1. 将匹配查询与运算符ANDwhitespace 分析器一起使用。如果您的条款包含空格,这不会工作
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "custom_attributes.attributes.inner_name": "attr2"
          }
        },
        {
          "match": {
            "custom_attributes.attributes.value": {
              "query": "val1 val2 val3 val4",
              "operator": "and",
              "analyzer": "whitespace"
            }
          }
        }
      ]
    }
  }
}

【讨论】:

  • 感谢您的帮助,我尝试了两种方法,但都不适合我。我必须在值路径之后添加.keyword 才能获得结果,但结果是我应用OR 而不是AND 运算符。我没有找到其他方法来解决这个问题,您还有其他想法可以帮助我吗?提前致谢。
猜你喜欢
  • 2021-06-28
  • 2023-04-09
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多