简体   繁体   English

Elasticsearch | 筛选不相关的嵌套数据

[英]Elasticsearch | filter on not related nested data

Ok, today i have a problem to filter an elasticsearch query with double nested not related fields specs.value.text and specs.spec.text . 好的,今天,我遇到了一个问题,该问题是使用双嵌套无关字段specs.value.textspecs.spec.text来过滤specs.value.text specs.spec.text

The mapping of these fields: 这些字段的映射:

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

The question is when i want to filter the query with this request: 问题是当我想使用此请求过滤查询时:

    {
      "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 will return document that contains the word Délai de livraison in specs.spec.text OR 10 in specs.value.text Elasticsearch将返回在specs.spec.text中包含单词Délai de livraison specs.spec.text specs.value.textspecs.spec.text 10specs.value.text

Exemple of the result: First object: 结果的示例:第一个对象:

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

The Second Object: 第二个对象:

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

The correct query should be the following 正确的查询应为以下内容

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

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

Since you were running two nested queries at specs level, so both the documents match as both the documents match each filter but in different nested documents under specs. 由于您在规范级别运行两个嵌套查询,因此两个文档都匹配,因为两个文档都与每个过滤器匹配,但在规范下位于不同的嵌套文档中。 To scope the and conditions for same nested document fire the filters under one nested query 要限制同一嵌套文档的和条件,请在一个嵌套查询下触发过滤器

Hope this helps Ajay 希望这对阿杰有帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM