【问题标题】:Elastic Search Aggregation and Complex Query弹性搜索聚合和复杂查询
【发布时间】:2020-06-25 11:12:54
【问题描述】:

我已经创建了索引

PUT ten2
{
    "mappings": {
        "documents": {
            "properties": {
                "title": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    }
                },"uid": {
                    "type": "text",
                    "fields": {
                        "raw": {
                            "type": "keyword"
                        }
                    }
                },
                "publish_details": {
                    "type": "nested",
                    "properties": {
                        "environment": {
                            "type": "keyword"
                        },
                        "locale": {
                            "type": "keyword"
                        },
                        "time": {
                            "type": "date"
                        },
                        "version": {
                            "type": "integer"
                        }
                    }
                }
            }
        }
    }
}

并将文档添加到其中。这是文件清单:

   [{
    "_index": "ten2",
    "_type": "documents",
    "_id": "blt69b62b48bbed1fb6_en-us",
    "_source": {
        "publish_details": [{
                "environment": "blt603fe91adbdcff66",
                "time": "2020-06-24T12:11:25.276Z",
                "locale": "en-us",
                "user": "bltaadab2f531206e9d",
                "version": 1
            },
            {
                "environment": "blt603fe91adbdcff66",
                "time": "2020-06-24T12:11:25.276Z",
                "locale": "hi-in",
                "user": "bltaadab2f531206e9d",
                "version": 1
            }
        ],
        "title": "Entry 1",
        "uid": "blt69b62b48bbed1fb6"
    }
},
{
    "_index": "ten2",
    "_type": "documents",
    "_id": "blt69b62b48bbed1fb6_mr-in",
    "_source": {
        "publish_details": [{
            "environment": "blt603fe91adbdcff66",
            "time": "2020-06-24T12:12:35.467Z",
            "locale": "mr-in",
            "user": "bltaadab2f531206e9d",
            "version": 1
        }],
        "title": "Entry 3",
        "uid": "blt69b62b48bbed1fb6"
    }
},
{
    "_index": "ten2",
    "_type": "documents",
    "_id": "blt4044c5198122a3ed_en-us",
    "_source": {
        "publish_details": [{
            "environment": "blt603fe91adbdcff66",
            "time": "2020-06-24T12:10:46.430Z",
            "locale": "en-us",
            "user": "bltaadab2f531206e9d",
            "version": 1
        },{
            "environment": "blt603fe91adbdcff6690",
            "time": "2020-06-24T12:10:46.430Z",
            "locale": "en-us",
            "user": "bltaadab2f531206e9d",
            "version": 1
        }],
        "title": "Entry 10",
        "uid": "blt4044c5198122a3ed"
    }
}

] 我想要以下结果

    [
 {
    "_index": "ten2",
    "_type": "documents",
    "_id": "blt4044c5198122a3ed_en-us",
    "_source": {
        "publish_details": [{
            "environment": "blt603fe91adbdcff66",
            "time": "2020-06-24T12:10:46.430Z",
            "locale": "en-us",
            "user": "bltaadab2f531206e9d",
            "version": 1
        },{
            "environment": "blt603fe91adbdcff6690",
            "time": "2020-06-24T12:10:46.430Z",
            "locale": "en-us",
            "user": "bltaadab2f531206e9d",
            "version": 1
        }],
        "title": "Entry 10",
        "uid": "blt4044c5198122a3ed"
    }
}

]

我正在使用以下查询来获取结果

GET ten2/_search
{
    "query": {
        "bool": {
            "must": [{
                "bool": {
                    "must_not": [{
                        "bool": {
                            "must": [{
                                "nested": {
                                    "path": "publish_details",
                                    "query": {
                                        "term": {
                                        "publish_details.environment": "blt603fe91adbdcff66"
                                        }
                                    }
                                }
                            }, {
                                "nested": {
                                    "path": "publish_details",
                                    "query": {
                                        "term": {
                                            "publish_details.locale": "en-us"
                                        }
                                    }
                                }
                            }, {
                                "nested": {
                                    "path": "publish_details",
                                    "query": {
                                        "term": {
                                            "publish_details.locale": "hi-in"
                                        }
                                    }
                                }
                            }, {
                                "nested": {
                                    "path": "publish_details",
                                    "query": {
                                        "term": {
                                            "publish_details.locale": "mr-in"
                                        }
                                    }
                                }
                            }]
                        }
                    }]
                }
            }
        }
    }
}

请帮我查询以获得预期结果。前两个具有相同 uid 的 dicuemtns 只有 publish_details.locale 不同。我在 must_not 中使用 must 查询来获取结果,目前我正在获取所有三个文档,但我只想要最后一个。我有数百万个文档。

【问题讨论】:

  • 和你的其他问题有关系吗?你能再解释一下逻辑吗?
  • 前两个文档的 uid 相同,这意味着 uid:blt69b62b48bbed1fb6 在所有 3 个语言环境中发布。所以我不希望结果中出现该文档。

标签: elasticsearch elasticsearch-query


【解决方案1】:

要了解更多关于布尔查询的信息,请参考这个官方documentation

为您的映射、索引数据和搜索查询添加一个工作示例

搜索查询:

    {
  "query": {
    "nested": {
      "path": "publish_details",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "publish_details.locale": "en-us"
              }
            }
          ],
          "must_not": [
            {
              "match": {
                "publish_details.environment": "blt603fe91adbdcff66"
              }
            },
            {
              "match": {
                "publish_details.locale": "hi-in"
              }
            },
            {
              "match": {
                "publish_details.locale": "mr-in"
              }
            }
          ]
        }
      },
      "inner_hits": {
        
      }
    }
  }
}

搜索结果:

"hits": [
        {
            "_index": "test",
            "_type": "_doc",
            "_id": "3",
            "_score": 0.53899646,
            "_source": {
                "publish_details": [
                    {
                        "environment": "blt603fe91adbdcff66",
                        "time": "2020-06-24T12:10:46.430Z",
                        "locale": "en-us",
                        "user": "bltaadab2f531206e9d",
                        "version": 1
                    },
                    {
                        "environment": "blt603fe91adbdcff6690",
                        "time": "2020-06-24T12:10:46.430Z",
                        "locale": "en-us",
                        "user": "bltaadab2f531206e9d",
                        "version": 1
                    }
                ],
                "title": "Entry 10",
                "uid": "blt4044c5198122a3ed"
            },
            "inner_hits": {
                "publish_details": {
                    "hits": {
                        "total": {
                            "value": 1,
                            "relation": "eq"
                        },
                        "max_score": 0.53899646,
                        "hits": [
                            {
                                "_index": "test",
                                "_type": "_doc",
                                "_id": "3",
                                "_nested": {
                                    "field": "publish_details",
                                    "offset": 1
                                },
                                "_score": 0.53899646,
                                "_source": {
                                    "environment": "blt603fe91adbdcff6690",
                                    "time": "2020-06-24T12:10:46.430Z",
                                    "locale": "en-us",
                                    "user": "bltaadab2f531206e9d",
                                    "version": 1
                                }
                            }
                        ]
                    }
                }
            }
        }
    ]

要了解更多关于内部点击的信息,请参阅documentation

以上查询只返回第三个文档,从而满足搜索查询的条件。在 Inner Hits 中,只有第三个文档的一部分返回,匹配 blt603fe91adbdcff66 的部分被丢弃。

【讨论】:

  • 感谢您的回复,但我的问题在这里有所不同。首先,两个具有相同 uid(blt69b62b48bbed1fb6) 的文档只有 publish_details.locale 是不同的,所以我希望查询和 uid 一起得到结果,这只是我有数百万个文档的示例。那么请帮我解决这个问题?
  • @Suraj Dalvi 你能解释一下你的意思吗**我希望查询和 uid 一起得到结果**,你想要结果的标准是什么?
  • 正如我告诉你的,前两个文档的 uid 相同,这意味着 uid:blt69b62b48bbed1fb6 在所有 3 个语言环境中发布。所以我不希望结果中出现该文档。
  • 需要针对该@Bhavya 的通用查询
  • 好的,@Suraj Dalvi 我现在明白你想说什么了 :)
猜你喜欢
  • 1970-01-01
  • 2016-03-23
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多