Completion Suggester Sorting

Hi,
I created an index that only contains the suggest property:

PUT /test
{
  "mappings": {
    "properties": {
      "suggest": {
        "type": "completion",
        "analyzer": "standard",
        "preserve_separators": false,
        "preserve_position_increments": false,
        "max_input_length": 30
      }
    }
  }
}
POST _bulk
{"index": {"_index": "test", "_id": "1"}}
{"suggest":{"input":["ab"]}}
{"index": {"_index": "test", "_id": "2"}}
{"suggest":{"input":["a bc"]}}
{"index": {"_index": "test", "_id": "3"}}
{"suggest":{"input":["a bc1"]}}
{"index": {"_index": "test", "_id": "4"}}
{"suggest":{"input":["a b"]}}

My doc data (also in the order returned by suggest => "ab"):

a b
a bc
a bc1
ab
GET /test/_search
{
	"suggest": {
		"suggest": {
			"completion": {
				"field": "suggest",
				"size": 100
			},
			"prefix": "ab"
		}
	}
}

When the weight is the same, the return result of completion is in lexicographical order. Why is the space not removed in this lexicographical order (considering "preserve_separators" : false )?
expected:

ab
a b
a bc
a bc123

or

a b
ab
a bc
a bc123

If it is possible, how should I configure the index?
Elasticsearch Version: 7.17.3
Thanks

so the short answer is preserve_separators is only impacting how the data is indexed. Once retrieved the data is sorted lexically based on what’s actually in the data.

My guess is without having tried this myself is that this is not possible to sort like this for suggesters.

I think if you want custom sorting logic you will need to explore indexing the data differently as in not using the suggest api.

Or just be ok with getting things in the wrong order and sorting them on the client side.

I’m not aware of an open feature request so you could add one here: GitHub · Where software is built

1 Like