簡體   English   中英

彈性搜索:嵌套屬性中的布爾查詢

[英]Elastic Search: Bool Query in nested properties

假設我的數據結構如下:

 { "id": "120400871755634330808993320",
                    "name": "Metaalschroef binnenzeskant, DIN 912 RVS A4-80",
                    "description": "m16x70 cilinderschroef bzk a4-80 din912 klasse 80",
                    "fullDescription": "Metaalschroef met een binnenzeskant cilinderkop",
                    "synonyms": [],
                    "properties": [
                        {
                            "name": "draad",
                            "value": "16",
                            "sort": 99
                        },
                        {
                            "name": "lengte",
                            "value": "70",
                            "sort": 99
                        },
                        {
                            "name": "materiaal",
                            "value": "roestvaststaal",
                            "sort": 99
                        },
                        {
                            "name": "kwaliteit (materiaal)",
                            "value": "A4",
                            "sort": 99
                        },
                        {
                            "name": "DIN",
                            "value": "912",
                            "sort": 99
                        },
                        {
                            "name": "AISI",
                            "value": "316",
                            "sort": 99
                        },
                        {
                            "name": "draadsoort",
                            "value": "metrisch",
                            "sort": 99
                        },
                        {
                            "name": "Merk",
                            "value": "Elcee Holland",
                            "sort": 1
                        }
                    ]
}

如何編寫布爾查詢,在其中選擇所有具有名稱為“ draad”,值為“ 16”的屬性和名稱為“ lengte”且值為“ 70”的文檔。

現在我有這個,但它返回0個結果:

"query" : {
    "nested" : {
        "path" : "properties",
        "query" : {
            "bool" : {
                "must" : [{
                        "bool" : {
                            "must" : [{
                                    "term" : {
                                        "properties.name" : "Merk"
                                    }
                                }, {
                                    "term" : {
                                        "properties.value" : "Facom"
                                    }
                                }
                            ]
                        }
                    }, {
                        "bool" : {
                            "must" : [{
                                    "term" : {
                                        "properties.name" : "materiaal"
                                    }
                                }, {
                                    "term" : {
                                        "properties.value" : "kunststof"
                                    }
                                }
                            ]
                        }
                    }
                ]
            }
        }
    }
}

將最高級別的“必須”替換為“應該”會返回太多結果,這很有意義,因為它可以轉換為“或”。

使用must ,引擎將嘗試搜索name:Merkvalue:Facom嵌套文檔。 而且還帶有name:materiaal value:kunststofvalue:kunststof在同一嵌套文檔中不可能一次發生。

正如您所提到的,在使用should將其轉換為or -的確是可能的。

問題是,您還將獲得整個父文檔及其所有嵌套文檔。

在我自己的答案中,我演示了使用nested文檔創建索引的步驟(您應將字段properties標記為嵌套類型`)。

完成這些步驟之后,您將可以通過以下查詢獲取結果:

{
  "_source": [
    "id",
    "name",
    "description"
  ],
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "properties",
            "query": {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "properties.name": "Merk"
                          }
                        },
                        {
                          "term": {
                            "properties.value": "Facom"
                          }
                        }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        {
                          "term": {
                            "properties.name": "materiaal"
                          }
                        },
                        {
                          "term": {
                            "properties.value": "kunststof"
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            },
            "inner_hits":{
              "size": 10
            }
          }
        }
      ]
    }
  }
}

我找到了運行良好的解決方案!

我的屬性對象現在看起來像這樣:

                    {
                        "name": "breedte(mm)",
                        "value": "1000",
                        "unit": "mm",
                        "sort": 99,
                        "nameSlug": "breedte-mm",
                        "slug": "breedte-mm-1000"
                    },

我添加了一個slug(包含用於鍵+值的規范化字符串)和一個nameslug,它是名稱的規范化字符串。

我的索引是這樣映射的:

                "properties": {
                    "type": "nested",
                    "include_in_parent": true,
                    "properties": {
                        "name": {
                            "type": "keyword"
                        },
                        "nameSlug": {
                            "type": "keyword"
                        },
                        "slug": {
                            "type": "keyword"
                        },
                        "sort": {
                            "type": "long"
                        },
                        "unit": {
                            "type": "text",
                            "index": false
                        },
                        "value": {
                            "type": "keyword"
                        }
                    }
                }

“ include_in_parent”在這里很重要。 它允許我執行以下查詢:

"query": {
    "bool": {
      "must": [
        {
          "terms": {
            "properties.slug": [
              "merk-orbis",
              "merk-bahco"
            ]
          }
        },
        {
          "terms": {
            "properties.slug": [
              "materiaal-staal",
              "materiaal-kunststof"
            ]
          }
        }
      ]
    }
  },

該查詢搜索“ merk”為“ Orbis”或“ Bahco”且“ material”為“ staal”或“ kunststof”的所有文檔。

我的匯總如下所示:

"merk_query": {
          "filter": {
            "bool": {
              "must": [
                {
                  "terms": {
                    "properties.slug": [
                      "materiaal-staal",
                      "materiaal-kunststof"
                    ]
                  }
                }
              ]
            }
          },
          "aggs": {
            "merk_facets": {
              "nested": {
                "path": "properties"
              },
              "aggs": {
                "merk_only": {
                  "filter": {
                    "term": {
                      "properties.nameSlug": {
                        "value": "merk"
                      }
                    }
                  },
                  "aggs": {
                    "facets": {
                      "terms": {
                        "field": "properties.name",
                        "size": 1
                      },
                      "aggs": {
                        "facetvalues": {
                          "terms": {
                            "field": "properties.value",
                            "size": 10
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        },

我運行filteraggregate過濾所有與構面匹配的文檔(但不是我正在構建的當前文檔)。

這種聚合的結果是這樣的:

"merk_query": {
                "doc_count": 7686,
                "merk_facets": {
                    "doc_count": 68658,
                    "merk_only": {
                        "doc_count": 7659,
                        "facets": {
                            "doc_count_error_upper_bound": 0,
                            "sum_other_doc_count": 0,
                            "buckets": [
                                {
                                    "key": "Merk",
                                    "doc_count": 7659,
                                    "facetvalues": {
                                        "doc_count_error_upper_bound": 10,
                                        "sum_other_doc_count": 438,
                                        "buckets": [
                                        {
                                            "key": "Orbis",
                                            "doc_count": 6295
                                        },
                                        {
                                            "key": "DX",
                                            "doc_count": 344
                                        },
                                        {
                                            "key": "AXA",
                                            "doc_count": 176
                                        },
                                        {
                                            "key": "Talen Tools",
                                            "doc_count": 127
                                        },
                                        {
                                            "key": "Nemef",
                                            "doc_count": 73
                                        },
                                        {
                                            "key": "bonfix",
                                            "doc_count": 67
                                        },
                                        {
                                            "key": "Bahco",
                                            "doc_count": 64
                                        },
                                        {
                                            "key": "Henderson",
                                            "doc_count": 27
                                        },
                                        {
                                            "key": "Maasland Groep",
                                            "doc_count": 25
                                        },
                                        {
                                            "key": "SYSTEC",
                                            "doc_count": 23
                                        }
                                    ]
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        },

這是瀏覽器的最終結果:

在此處輸入圖片說明

暫無
暫無

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

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