【问题标题】:How to search in ElasticSearch based on multiple nested fields如何基于多个嵌套字段在 ElasticSearch 中进行搜索
【发布时间】:2019-09-22 01:51:38
【问题描述】:

我在产品文档中嵌入产品属性,我想做一个聚合,当属性的值大于 0 时返回属性名称。

我应该如何编写查询?

================================================ ========

ElasticSearch 中存储的数据示例:

从以下数据集中,我预计结果是:["Sugar", "Fat"] 因为没有产品包含水

[{
  name: "Product1",
  price: 12.0,
  properties: [
    {
      group: "Product Composition",
      name: "Sugar",
      value: 1
    },
    {
      group: "Product Composition",
      name: "Fat",
      value: 2
    },
    {
      group: "Product Composition",
      name: "Water",
      value: 0
    },
    {
      group: "Packaging",
      name: "Size",
      value: "Big"
    }
  ]
}, {
  name: "Product2",
  price: 11.0,
  properties: [
    {
      group: "Product Composition",
      name: "Sugar",
      value: 0
    },
    {
      group: "Product Composition",
      name: "Fat",
      value: 2
    },
    {
      group: "Product Composition",
      name: "Water",
      value: 0
    },
    {
      group: "Packaging",
      name: "Size",
      value: "Small"
    }
  ]
}]

======== 索引映射 ========

{
  "test_products": {
    "mappings": {
      "product": {
        "dynamic_templates": [
          {
            "template_1": {
              "path_match": "properties.value",
              "match_mapping_type": "double",
              "mapping": {
                "scaling_factor": 100,
                "type": "scaled_float"
              }
            }
          },
          {
            "template_2": {
              "path_match": "properties.value",
              "match_mapping_type": "string",
              "mapping": {
                "fields": {
                  "raw": {
                    "ignore_above": 100,
                    "type": "keyword"
                  }
                },
                "type": "text"
              }
            }
          }
        ],
        "properties": {
          "name": {
            "type": "text",
            "fields": {
              "raw": {
                "type": "keyword"
              }
            }
          },
          "price": {
            "type": "double"
          },
          "properties": {
            "type": "nested",
            "properties": {
              "group": {
                "type": "text",
                "fields": {
                  "raw": {
                    "type": "keyword"
                  }
                }
              },
              "name": {
                "type": "text",
                "fields": {
                  "raw": {
                    "type": "keyword"
                  }
                }
              },
              "value": {
                "type": "text"
              }
            }
          }
        }
      }
    }
  }
}

【问题讨论】:

  • 请分享您的相关数据映射,以便我可以在我的测试集群中进行模拟。
  • 嗨@apt-get_instal_skill,很抱歉回复晚了,我在问题中添加了索引映射。

标签: elasticsearch nested elasticsearch-aggregation


【解决方案1】:

您将获得属性作为响应> 以下查询的内部点击

查询:

GET test_products/_search
{
  "query": {
    "nested": {
      "path": "properties",
      "query": {
        "bool": {
          "must": [
            {
              "range": {
                "properties.value": {  --> check for value greater than 0
                  "gt": 0
                }
              }
            }
          ],
          "must_not": [
            {
              "term": {
                "properties.group.raw": { ---> remove packaging
                  "value": "Packaging"
                }
              }
            }
          ]
        }
      },
      "inner_hits": {}   ----> in response will give matched nested documents
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    相关资源
    最近更新 更多