簡體   English   中英

如何基於多個嵌套字段在ElasticSearch中搜索

[英]How to search in ElasticSearch based on multiple nested fields

我將產品屬性嵌入產品文檔中,並且要進行匯總,當其值大於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"
              }
            }
          }
        }
      }
    }
  }
}

您將在以下查詢的響應>內部命中中獲得屬性

查詢:

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
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM