Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Search max on string fields

In SQL, it is possible to use MAX() on string fields to get a distinct value (assuming the group by is correct).

However this is not possible in ElasticSearch, since MAX only works on numeric fields. However I want to retrieve the values of some string fields after my aggregations, so I can display these values.

eg assuming a generic books structure

{
  "aggs" : {
     "group_by_author" : { "terms" : { "field" : "author"},
     "aggs" : { 
      "books_published" : { "sum" : { "field" : "name"}},
      "distinct_title" : { "max" : {"field" : "some_relevant_field_name"}}
    }
  }
  }
}

Here I cannot perform the max on some_relevant_field_name since it is a string. Is there an alternative way to do this apart from more aggregations ?

like image 823
Hav3n Avatar asked Jun 12 '26 23:06

Hav3n


1 Answers

If you want to find the distinct book titles for each author, maybe should your try to use the "terms" aggregation in the "distinct_title" field:

{
    "aggs":{
        "group_by_author":{
            "terms":{
                "field":"author"
            },
            "aggs":{
                "books_published":{
                    "sum":{
                        "field":"name"
                    }
                },
                "distinct_title":{
                    "terms":{
                        "field":"some_relevant_field_name"
                    }
                }
            }
        }
    }   
}

It should create buckets of book titles for each author as described in the documentation.

like image 65
Heschoon Avatar answered Jun 16 '26 12:06

Heschoon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!