# Elasticsearch autocomplete- sorting words by their relevance to a keyword

**URL:** https://discuss.elastic.co/t/elasticsearch-autocomplete-sorting-words-by-their-relevance-to-a-keyword/110603
**Category:** Elasticsearch
**Created:** [December 7, 2017, 4:04am UTC](https://discuss.elastic.co/t/elasticsearch-autocomplete-sorting-words-by-their-relevance-to-a-keyword/110603 "2017-12-07T04:04:36Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![arafath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arafath/32/12784_2.png) [@arafath](https://discuss.elastic.co/u/arafath)
#### Post date: [December 7, 2017, 4:04am UTC](https://discuss.elastic.co/t/elasticsearch-autocomplete-sorting-words-by-their-relevance-to-a-keyword/110603/1 "2017-12-07T04:04:36Z")

</div>

I'm currently working on a search using elasticsearch. We have a very large amount of users. Here is the elasticsearch mapping :

```
PUT /example_index/_mapping/users
{
  "properties": {
    "user_autocomplete": {
      "type": "text",
      "fields": {
        "raw": {
          "type": "keyword"
        },
        "completion": {
          "type": "text",
          "analyzer": "user_autocomplete_analyzer",
          "search_analyzer": "standard"
        }
      }
    },
    "firstName": {
      "type": "text"
    },
    "lastName": {
      "type": "text"
    }
  }
}

```

Here is the search query. for example, I get 3 records

```
GET example_index/users/_search
{
  "from": 0,
  "size": 3,
  "query": {
    "query_string": {
      "query": "*ro*",
      "fields": [
        "firstName",
        "lastName"
      ]
    }
  },
  "aggs": {
    "user_suggestions": {
      "terms": {
        "size": 3,
        "field": "user_autocomplete.raw"
      }
    }
  }
}

```

Here is the output of elasticsearch

```
{
  "took": 53,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 13,
    "max_score": 1,
    "hits": [
      {
        "_index": "example_index",
        "_type": "users",
        "_id": "08",
        "_score": 1,
        "_source": {
          "firstName": "Eero",
          "lastName": "Saarinen",
          "user_autocomplete": "Eero Saarinen"
        }
      },
      {
        "_index": "example_index",
        "_type": "users",
        "_id": "16",
        "_score": 1,
        "_source": {
          "firstName": "Aaron",
          "lastName": "Judge",
          "user_autocomplete": "Aaron Judge"
        }
      },
      {
        "_index": "example_index",
        "_type": "users",
        "_id": "20",
        "_score": 1,
        "_source": {
          "firstName": "Robert",
          "lastName": "Langdon",
          "user_autocomplete": "Robert Langdon"
        }
      }
    ]
  },
  "aggregations": {
    "user_suggestions": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 10,
      "buckets": [
        {
          "key": "Eero Saarinen",
          "doc_count": 1
        },
        {
          "key": "Aaron Judge",
          "doc_count": 1
        },
        {
          "key": "Robert Langdon",
          "doc_count": 1
        }
      ]
    }
  }
}

```

I need result like in the following order

1. Robert Langdon
2. Aaron Judge
3. Eero Saarinen

I have tried order method. It won't work. So is there a way?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 7, 2017, 9:24am UTC](https://discuss.elastic.co/t/elasticsearch-autocomplete-sorting-words-by-their-relevance-to-a-keyword/110603/2 "2017-12-07T09:24:56Z")

</div>

Is that descending sort on key that you want to do?

If so there is an example here: [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#search-aggregations-bucket-terms-aggregation-order)

---

<div class="post-metadata">

### Author: ![arafath](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/arafath/32/12784_2.png) [@arafath](https://discuss.elastic.co/u/arafath)
#### Post date: [December 7, 2017, 12:13pm UTC](https://discuss.elastic.co/t/elasticsearch-autocomplete-sorting-words-by-their-relevance-to-a-keyword/110603/3 "2017-12-07T12:13:06Z")

</div>

No. I want to order based on the score. But the score is always 1.  
If i change query from this `"query": "ro",` to `"query": "ro",` it is working fine. But Autoomcpltetion not working. It s

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [December 7, 2017, 4:13pm UTC](https://discuss.elastic.co/t/elasticsearch-autocomplete-sorting-words-by-their-relevance-to-a-keyword/110603/4 "2017-12-07T16:13:07Z")

</div>

> If i change query from this `"query": "ro"`, to `"query": "ro"`,

I don't see the difference between your both examples TBH.

Could you provide a full recreation script as described in

> [@About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21):
>
> The heart of the free and open Elastic Stack Elasticsearch is a distributed, RESTful search and analytics engine capable of addressing a growing number of use cases. As the heart of the Elastic Stack, it centrally stores your data for lightning fast search, fine‑tuned relevancy, and powerful analytics that scale with ease. warning PLEASE READ THIS SECTION IF IT'S YOUR FIRST POST Some useful links: [elasticsearch reference guide](http://www.elastic.co/guide/en/elasticsearch/reference/current/index.html)[elasticsearch user guide](http://www.elastic.co/guide/en/elasticsearch/guide/current/index.html)[elasticsearch plugins](https://www.elastic.co/guide/en/elasticsearch/plugins/current/index.html)[elasticsearch cl…](https://www.elastic.co/guide/en/elasticsearch/client/index.html)

It will help to better understand what you are doing.  
Please, try to keep the example as simple as possible.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [January 4, 2018, 4:13pm UTC](https://discuss.elastic.co/t/elasticsearch-autocomplete-sorting-words-by-their-relevance-to-a-keyword/110603/5 "2018-01-04T16:13:18Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
