Elasticsearch: completion sorting by the number of matched documents

I am implementing a tag autocompleter with Elasticsearch.
My tags are stored inside my indexed documents in various String arrays:

 {
...
"SKILLS": {
  "INDUSTRIES_SOLUTIONS": [
    "project-management"
  ],
  "SKILLS_COMPETENCIES": [
    "javascript"
  ],
  "HIGHLIGHTED": [],
  "tag_suggest": {
    "input": [
      "javascript",
      "hibernate",
      "project-management"
    ]
  },
  "JOB_ROLES": [
    "hibernate"
  ]
}
}

As you can see, I included them in the tag_suggest object and mapped it in a way to be able to query the _suggest api with this kind of queries:

{
"tag_suggest":{
    "text":"jav",
    "completion": {
        "field" : "SKILLS.tag_suggest",
        "fuzzy" : {
            "fuzziness" : 1
        }
    }
}
}

which correctly returns:

    {
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "tag_suggest": [
    {
      "text": "jav",
      "offset": 0,
      "length": 3,
      "options": [
        {
          "text": "java",
          "score": 1
        },
        {
          "text": "javascript",
          "score": 1
        }
      ]
    }
  ]
}

What I would like to do is to be able to sort the results basing on the number of matched documents.
For example, if the “java” tag is included in 1 document and javascript is included in 10 documents, I would like the latter to appear as the first suggestion result. A nice to have would be the number of matched documents, sort of Stackoverflow-like.
How could I implement this feature? Is completion the correct way to go? Thank you.


Source: json

Leave a Reply