簡體   English   中英

Elasticsearch | 篩選不相關的嵌套數據

[英]Elasticsearch | filter on not related nested data

好的,今天,我遇到了一個問題,該問題是使用雙嵌套無關字段specs.value.textspecs.spec.text來過濾specs.value.text specs.spec.text

這些字段的映射:

...
"specs": {
"type": "nested",
"properties": {
"spec": {
  "type": "nested",
  "properties": {
    "text": {
      "type": "string",
      "fields": {
        "raw": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }
},

"value": {
"type": "nested",
"properties": {
  "text": {
    "type": "string",
    "fields": {
      "raw": {
        "type": "string",
        "index": "not_analyzed"
        }
      }
    }
    }
  }
}
}
....

問題是當我想使用此請求過濾查詢時:

    {
      "query": {
        "filtered": {
          "filter": {
            "and": {
              "filters": [
                {
                  "nested": {
                    "filter": {
                      "nested": {
                        "filter": {
                          "match": {
                            "specs.value.text": "10"
                          }
                        },
                        "path": "specs.value"
                      }
                    },
                    "path": "specs"
                  }
                },
                {
                  "nested": {
                    "filter": {
                      "nested": {
                        "filter": {
                          "match": {
                            "specs.spec.text": "Délai de livraison"
                          }
                        },
                        "path": "specs.spec"
                      }
                    },
                    "path": "specs"
                  }
                }
              ]
            }
          },
          "query": {
            "match_all": {}
          }
        }
      },
      "_source": [
        "specs"
      ]
    }

Elasticsearch將返回在specs.spec.text中包含單詞Délai de livraison specs.spec.text specs.value.textspecs.spec.text 10specs.value.text

結果的示例:第一個對象:

  ...
  "specs": [
      {
        "value": [
          {
            "text": "10",
            "lang": "fr-FR"
          }
        ],
        "spec": [
          {
            "text": "Délai de livraison",
            "lang": "fr-FR"
          }
        ]
      },


      {

        "value": [
          {
            "text": "10",
            "lang": "fr-FR"
          }
        ],
        "spec": [
          {
            "text": "Volume",
            "lang": "fr-FR"
          }
        ]
      }
  ]
  ...

第二個對象:

    ...
    "specs": [
      {
        "value": [
          {
            "text": "7"
          }
        ]
        "spec": [
          {
            "text": "Délai de livraison"
          }
        ]
      }
    ]
    ...

正確的查詢應為以下內容

{
    "query": {
        "bool": {
            "must": [{
                "nested": {
                    "path": "specs",
                    "query": {
                        "bool": {
                            "must": [{
                                "nested": {
                                    "path": "specs.value",
                                    "query": {
                                        "bool": {
                                            "must": [{
                                                "match": {
                                                    "specs.value.text": "10"
                                                }
                                            }]
                                        }
                                    }
                                }
                            }, {
                                "nested": {
                                    "path": "specs.spec",
                                    "query": {
                                        "bool": {
                                            "must": [{

                                                "match": {
                                                    "specs.spec.text": "Délai de livraison"

                                                }
                                            }]
                                        }
                                    }
                                }
                            }]
                        }
                    }
                }
            }]
        }
    }
}

由於您在規范級別運行兩個嵌套查詢,因此兩個文檔都匹配,因為兩個文檔都與每個過濾器匹配,但在規范下位於不同的嵌套文檔中。 要限制同一嵌套文檔的和條件,請在一個嵌套查詢下觸發過濾器

希望這對阿傑有幫助

暫無
暫無

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

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