Elasticsearch autocomplete- sorting words by their relevance to a keyword

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?

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

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

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

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

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