elasticsearch,Json,elasticsearch" /> elasticsearch,Json,elasticsearch" />

Json Elasticsearch |过滤不相关的嵌套数据

Json Elasticsearch |过滤不相关的嵌套数据,json,elasticsearch,Json,elasticsearch,好的,今天我遇到了一个问题,无法过滤带有两个嵌套的不相关字段的elasticsearch查询specs.value.text和specs.spec.text 这些字段的映射: ... "specs": { "type": "nested", "properties": { "spec": { "type": "nested", "properties": { "text": { "type": "string", "fields": { "

好的,今天我遇到了一个问题,无法过滤带有两个嵌套的不相关字段的elasticsearch查询
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.value.text中的
10
的文档

结果示例: 第一个目标:

  ...
  "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"

                                                }
                                            }]
                                        }
                                    }
                                }
                            }]
                        }
                    }
                }
            }]
        }
    }
}
由于您在等级库级别运行两个嵌套查询,因此两个文档都匹配,因为两个文档都匹配每个过滤器,但位于等级库下的不同嵌套文档中。要确定同一嵌套文档的范围和条件,请在一个嵌套查询下启动筛选器

希望这有帮助 阿杰