Ofir Z
Ofir Z

Reputation: 95

ElasticSearch sort query alphabetically with score

is it possible to choose to have your queries sorted alphabetically via some change in the index settings or mapping?

I know that there is a "sort" query, but that removes the score, and I'd like the score to also be considered in addition to the alphabetical sort of the term.

For example if results "A" & "Z" have scores of 2, and "C" has a score of 1 I want the order to be:

A

Z

C

is this possible?

This is my current index settings and mapping:

{
   "users": {
      "settings": {
         "index": {
            "analysis": {
               "filter": {
                  "shingle_filter": {
                     "max_shingle_size": "2",
                     "min_shingle_size": "2",
                     "output_unigrams": "true",
                     "type": "shingle"
                  },
                  "edgeNGram_filter": {
                     "type": "nGram",
                     "min_gram": "1",
                     "max_gram": "20"
                  }
               },
               "analyzer": {
                  "autocomplete_query_analyzer": {
                     "filter": [
                        "standard",
                        "asciifolding",
                        "lowercase"
                     ],
                     "tokenizer": "standard"
                  },
                  "autocomplete_index_analyzer": {
                     "filter": [
                        "standard",
                        "asciifolding",
                        "lowercase",
                        "shingle_filter",
                        "edgeNGram_filter"
                     ],
                     "tokenizer": "standard"
                  }
               }
            },
            "number_of_shards": "1",
            "number_of_replicas": "1"
         }
      }
   }
}

{
   "users": {
      "mappings": {
         "data": {
            "properties": {
               "name": {
                  "type": "string",
                  "analyzer": "autocomplete_index_analyzer",
                  "search_analyzer": "autocomplete_query_analyzer"
               }
            }
         }
      }
   }
}

Is it possible to add something to the index settings so results are automatically sorted alphabetically?

Upvotes: 0

Views: 2168

Answers (1)

Mihai Ionescu
Mihai Ionescu

Reputation: 978

The example in ElasticSearch's documentation shows that you should be able to sort by multiple values, including _score. Try something like:

"sort": [
    { "date":   { "order": "desc" }},
    { "_score": { "order": "desc" }}
]

I'm not sure which field you're trying to sort by, so you'll need to adjust the example above according to your needs.

Let me know if this works for you :)

Upvotes: 5

Related Questions