且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

弹性搜索聚合和复杂查询

更新时间:2023-02-08 21:08:10

要了解有关布尔查询的更多信息,请参考此官方

To know more about Bool queries refer to this official 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
                                }
                            }
                        ]
                    }
                }
            }
        }
    ]

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

To know more about inner hits refer to this documentation

上面的查询仅返回第三个文档,从而满足了搜索查询的条件.在内部匹配中,仅返回第三份文档的一部分,而与 blt603fe91adbdcff66 匹配的部分被丢弃.

The above query returns only the third document, thus satisfying the conditions of the search query. In the Inner Hits, only one part of the third document is returning, and the part which is matching blt603fe91adbdcff66 is discarded.