Why can't I get Elasticsearch's completion suggester to sort based on a field?

Viewed 157

I'm trying to get autocomplete suggestions from Elasticsearch, but sorted by an internal popularity score that I supply in the data, so that the most popular ones show at the top. My POST looks like this:

curl "http://penguin:9200/node/_search?pretty" --silent --show-error \
    --header "Content-Type: application/json" \
    -X POST \
    -d '
{
   "_source" : [
      "name",
      "popular_score"
   ],
   "sort" : [ "popular_score" ],
   "suggest" : {
      "my_suggestion" : {
         "completion" : {
            "field" : "searchbar_suggest",
            "size" : 10,
            "skip_duplicates" : true
         },
         "text" : "f"
      }
   }
}
'

I get back valid autocomplete suggestions, but they aren't sorted by the popular_score field:

{
  ...
  "suggest" : {
    "my_suggestion" : [
      {
        "text" : "f",
        "offset" : 0,
        "length" : 1,
        "options" : [
          {
            "text" : "2020 Fact Longlist",
            "_index" : "node",
            "_type" : "_doc",
            "_id" : "245105",
            "_score" : 1.0,
            "_source" : {
              "popular_score" : "35",
              "name" : "2020 Fact Longlist"
            }
          },
          {
            "text" : "Fable",
            "_index" : "node",
            "_type" : "_doc",
            "_id" : "125903",
            "_score" : 1.0,
            "_source" : {
              "popular_score" : "69.33333333333333333333333333333333333333",
              "name" : "Fable"
            }
          },
          {
            "text" : "Fables",
            "_index" : "node",
            "_type" : "_doc",
            "_id" : "172986",
            "_score" : 1.0,
            "_source" : {
              "popular_score" : "24",
              "name" : "Fables"
            }
          }
          ...
        ]
      }
    ]
  }
}

My mappings are:

{
  "mappings": {
    "properties": {
      "nodeid": {
        "type": "integer"
      },
      "name": {
        "type": "text",
        "copy_to": "searchbar_suggest"
      },
      "popular_score": {
        "type": "float"
      },
      "searchbar_suggest": {
        "type": "completion"
      }
    }
  }
}

What am I doing wrong?

0 Answers
Related