如何嵌套聚合匹配术语?

How to nested aggregate matched terms?

提问人:Sandy 提问时间:1/23/2023 最后编辑:PauloSandy 更新时间:1/23/2023 访问量:35

问:

我的索引中有这个映射:

{
  "mappings": {
    "properties": {
      "uuid": {
        "type": "keyword"
      },
      "last_visit": {
        "type": "date"
      },
      "urls": {
        "type": "nested",
        "properties": {
          "url": {
            "type": "keyword"
          },
          "is_visited": {
            "type": "boolean"
          }
        }
      }
    }
  }
}

还有数百个这样的数据:

这是我搜索和搜索时所需的输出:*google.com*facebook.com

[
  {
    "uuid": "afa9ac03-0723-4d66-ae18-08a51e2973bd",
    "urls": [
      {
        "is_visited": true,
        "url": "https://www.google.com",
        "last_visit": "2022-02-31"
      },
      {
        "is_visited": false,
        "url": "https://www.facebook.com",
        "last_visit": "2022-02-03"
      },
      {
        "is_visited": true,
        "url": "https://www.twitter.com",
        "last_visit": "2022-03-30"
      }
    ]
  },
  {
    "uuid": "4a1c695d-756b-4d9d-b3a0-cf524d955884",
    "urls": [
      {
        "is_visited": true,
        "url": "https://www.stackoverflow.com",
        "last_visit": "2022-03-23"
      },
      {
        "is_visited": false,
        "url": "https://www.facebook.com",
        "last_visit": "2022-02-02"
      },
      {
        "is_visited": false,
        "url": "https://drive.google.com",
        "last_visit": "2022-05-01"
      },
      {
        "is_visited": true,
        "url": "https://www.google.com",
        "last_visit": "2022-07-09"
      }
    ]
  }
]

这是我编写的代码(感谢另一个问题,我没有很好地解释自己对所需输出的解释),重点是当我尝试将字段添加到输出时:*google.comlast_visit

{
  "query": {
    "nested": {
      "path": "urls",
      "query": {
        "bool": {
          "should": [
            {
              "wildcard": {
                "urls.url": {
                  "value": "*google.com"
                }
              }
            },
            {
              "wildcard": {
                "urls.url": {
                  "value": "*facebook.com"
                }
              }
            }
          ]
        }
      }
    }
  },
  "aggs": {
    "agg_providers": {
      "nested": {
        "path": "urls"
      },
      "aggs": {
        "google.com": {
          "terms": {
            "field": "urls.url",
            "include": ".*google.com",
            "size": 10
          },
          "aggs": {
            "top_hits": {
              "top_hits": {
                "size": 1,
                "_source": {
                  "includes": ["last_visit"]
                }
              }
            }
          }
        },
        "facebook.com": {
          "terms": {
            "field": "urls.url",
            "include": ".*facebook.com",
            "size": 10
          }
        }
      }
    }
  }
}

上面的代码返回 2 个不同的列表,其中我有字典值而不是所有字段( 、 等)bucketskey,doc_countis_visited,last_visituuid

谢谢。

Elasticsearch elasticsearch-aggregation elasticsearch-dsl

评论


答: 暂无答案