sqlelasticsearchkibanaelasticsearch-dsl-py

select a single field with applying filters in elasticsearch


I would like to select all the filename field values by ACCOUNT and APPLICATION_NAME Assuming as in SQL I need to do this :

select filename.keyword from XXX where ACCOUNT='monitoring' and APPLICATION_NAME='webapp'

this is a screenshot of a log entry sample in the kibana interface

enter image description here


Solution

  • selecting the unique values of a specific field is exactly like running an aggregation query at one of the SQL databases for example

    this query worked for me in case I wanted to selelct filename and POD_ID uniques pairs.

    {
              "size": "0",
              "aggs": {
                "custom_agg_name_whatever_you_want": {
                  "composite": {
                    "sources": [
                      {
                        "FILENAME": {
                          "terms": {
                            "field": "filename.keyword"
                          }
                        }
                      },
                      {
                        "POD_ID":{
                          "terms": {
                            "field": "POD_ID.keyword"
                          }
                        }
                      }
                    ]
                  }
                }
              },
              
              "query": {
                "bool": {
                  "filter": [
                    {
                      "bool": {
                        "filter": [
                          {
                            "bool": {
                              "should": [
                                {
                                  "match_phrase": {
                                    "ACCOUNT.keyword": "searchValue"
                                  }
                                }
                              ],
                              "minimum_should_match": 1
                            }
                          },
                          {
                            "bool": {
                              "should": [
                                {
                                  "match_phrase": {
                                    "APPLICATION_NAME.keyword": "searchValue"
                                  }
                                }
                              ],
                              "minimum_should_match": 1
                            }
                          }
                        ]
                      }
                    },
                    {
                      "range": {
                        "@timestamp": {
                          "format": "strict_date_optional_time",
                          "gte": "2022-03-21T09:09:09.277Z",
                          "lte": "2022-03-25T09:09:09.277Z"
                        }
                      }
                    }
                  ]
                }
              }
            
            }