最小聚集上的弹性过滤器

aij0ehis  于 2021-06-13  发布在  ElasticSearch
关注(0)|答案(2)|浏览(300)

嗨,我想在聚合上做一个过滤器,例如我想所有关键字都是first date>'2020-10-20'
我尝试了更多的解决方案,但不起作用

get /analityc/_search
{
  "query":{
    "match": {
      "hashDomaine": "test"
    }
  },
  "aggs":{
    "nbkeyword":{
        "terms": {
          "field": "keyword.keyword",
          "size": 10
        },
        "aggs": {
          "minDate": {
            "min": {
              "field": "date"
            }
          }

        }
    }
  }
}

示例数据点击,你可以网站我有一个日期的元素

ex data 

"hits" : [
      {
        "_index" : "analityc",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 8.002881E-5,
        "_source" : {
          "id" : 1,
          "hashDomaine" : "fyher",
          "click" : 0,
          "impression" : null,
          "ctr" : 0.0,
          "position" : 74.0,
          "url" : "urllll",
          "country" : "fra",
          "device" : "DESKTOP",
          "date" : "2019-09-17T00:00:00+02:00",
          "genre" : "",
          "keyword" : "keyword 1 "
        }
      },
      {
        "_index" : "analityc",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 8.002881E-5,
        "_source" : {
          "id" : 2,
          "hashDomaine" : "fyher",
          "click" : 0,
          "impression" : null,
          "ctr" : 0.0,
          "position" : 83.0,
          "url" : "urllll",
          "country" : "esp",
          "device" : "DESKTOP",
          "date" : "2019-09-17T00:00:00+02:00",
          "genre" : "",
          "keyword" : "keyword 2"
        }
      },
      {
        "_index" : "analityc",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 8.002881E-5,
        "_source" : {
          "id" : 3,
          "hashDomaine" : "fyher",
          "click" : 0,
          "impression" : null,
          "ctr" : 0.0,
          "position" : 17.0,
          "url" : "urllll",
          "country" : "vnm",
          "device" : "DESKTOP",
          "date" : "2019-09-22T00:00:00+02:00",
          "genre" : "",
          "keyword" : "keyword2"
        }
      },
      {
        "_index" : "analityc",
        "_type" : "_doc",
        "_id" : "5",
        "_score" : 8.002881E-5,
        "_source" : {
          "id" : 5,
          "hashDomaine" : "fyher",
          "click" : 0,
          "impression" : null,
          "ctr" : 0.0,
          "position" : 51.0,
          "url" : "urllll",
          "country" : "tha",
          "device" : "DESKTOP",
          "date" : "2019-09-27T00:00:00+02:00",
          "genre" : "",
          "keyword" : "keyword2"
        }
      },
      {
        "_index" : "analityc",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 8.002881E-5,
        "_source" : {
          "id" : 6,
          "hashDomaine" : "fyher",
          "click" : 0,
          "impression" : null,
          "ctr" : 0.0,
          "position" : 34.0,
          "url" : "urllll",
          "country" : "usa",
          "device" : "MOBILE",
          "date" : "2019-09-26T00:00:00+02:00",
          "genre" : "",
          "keyword" : "keyword3"
        }
      },
      {
        "_index" : "analityc",
        "_type" : "_doc",
        "_id" : "6",
        "_score" : 8.002881E-5,
        "_source" : {
          "id" : 6,
          "hashDomaine" : "fyher",
          "click" : 0,
          "impression" : null,
          "ctr" : 0.0,
          "position" : 34.0,
          "url" : "urllll",
          "country" : "usa",
          "device" : "MOBILE",
          "date" : "2019-09-25T00:00:00+02:00",
          "genre" : "",
          "keyword" : "keyword4"
        }
      }

我想要一个关键字谁在2020-10-12之后有第一个日期,你看?
在本例中,我希望关键字min date>2019-09-23,te response是关键字3和关键字4,而不是关键字2,因为该关键字的日期<2019-09-23,明白吗?关键字1不是新关键字,因为在2019-09-23之前存在。我希望新关键字在日期之后存在
谢谢你的帮助

2vuwiymt

2vuwiymt1#

我想,我找到了好答案:)

get /analityc/_search
{
  "query": {
    "match": {
      "hashDomaine": "fyher"
    }
  },
  "aggs": {
      "nbkeyword": {
          "terms": {
            "field": "keyword.keyword",
            "size": 50
          },
          "aggs": {
            "minagg": {
              "min": {
                "field": "date",
                "format": "YYYY-mm-dd"
              }
            },
            "selector":
            {
              "bucket_selector": {
                "buckets_path": {
                  "min":"minagg"
                },
                "script": "params.min>1601547846000L"
              }
            }
          }
        }
  }
}
}
fsi0uk1n

fsi0uk1n2#

您需要这样做:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "hashDomaine": "test"
          }
        }
      ],
      "filter": [
        {
          "range": {
            "date": {
              "gte": "2020-10-20"
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "nbkeyword": {
      "terms": {
        "field": "keyword.keyword",
        "size": 10
      }
    }
  }
}

相关问题