Fyher
Fyher

Reputation: 31

Elastic filter on min aggregation

hi i want to make a filter on aggregation for example I want all keyword is first date > '2020-10-20'

i have try more solution but is not working

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

Example data hits, you can site I have one date by element

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

i want the keyword who have the first date after 2020-10-12 for example , you see ?

for this example I want the keyword with the min date >2019-09-23, te response is keyword 3 and keyword 4 , not the keyword 2 because this keyword have the date < 2019-09-23 , you see ? Keyword 1 is not a new keyword because is existing before the 2019-09-23 . I wan the new keyword existing after the date

thanks for your help

Upvotes: 0

Views: 174

Answers (2)

Fyher
Fyher

Reputation: 31

I think, i'am found the good answer :)

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

Upvotes: 1

Val
Val

Reputation: 217294

This is how you need to do it:

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

Upvotes: 0

Related Questions