【问题标题】:Elastic Search Fliter on two fields within same sub documentElasticsearch过滤同一子文档中的两个字段
【发布时间】:2017-01-27 20:20:07
【问题描述】:

我正在尝试编写一个弹性搜索查询,其中我有一组具有完全相同结构的子文档,并且我必须检查同一个子文档中的两个条件。

我的数据如下所示:

{
    "_id": "5672ba7c0d71c93d159c408e",
    "productId": "1723913526",
    "title": "Digitab Tablet",
    "instock": true,
    "specifications": [{
        "category": "In the box",
            "name": "Test1",
            "value": "No"
        }, {
            "category": "Warranty",
            "name": "Test2",
            "value": "Yes"
        }, {
            "category": "General",
            "name": "Test3",
            "value": "Abcd"
        }],
    }
}

我正在尝试的是:我应该得到规格名称为“Test1”且同一子文档的规格说明值为“是”的产品

这是我为此编写的查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "wildcard": {
            "specifications.name": "Test1*"
          }
        },
        {
          "term": {
            "instock": "true"
          }
        },
        {
          "term": {
            "specifications.value": "yes"
          }
        }
      ],
      "must_not": [],
      "should": []
    }
  }
}

问题是 - 它不应该返回 id 为 1723913526 的产品,因为 "value": "Yes" 对于 "name": "Test2" 是 true 而不是 "name": "Test1 ”。

我希望问题很清楚,如果需要更多信息,请发表评论。

提前致谢。

【问题讨论】:

    标签: elasticsearch elasticsearch-query


    【解决方案1】:

    您需要修改您的映射以表明specificationsnested 类型。

    映射如下:

    "mappings": {
      "product": {
        "properties": {
          ...
          "specifications": {
            "type": "nested" 
          }
        }
      }
    }
    

    然后像这样查询:

    "bool": {
      "must": [{
        "nested": {
          "path": "specifications",
          "query": {
            "bool": {
              "must": [
                { "wildcard": { "specifications.first": "Test1*" }},
                { "term": { "specifications.value":  "yes" }} 
              ]
            }
          }
        },
        {
          "term": { "instock": "true" }
        }
      }]
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-17
      • 2022-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多