Rank documents by specific field with the search query result


I have documents that are related to website and ranking of the websites. Each of the document has atleast the following three fields

  • Site name (like google, yahoo etc.)
  • Site URL (like google.com, yahoo.com, etc.)
  • Site rank (which is a pre-determined numeric rank)

I have a search field that the user can search on the "site name". My intention is to allow the search and then order the results based on the site rank. I tried to sort the documents based on the site rank, but then the result has all irrelevant documents, i.e., the query result is not being used for sort

I have tried function_score, but my results are messed up, where-in search for "char" gives results that start with "chas".

Is there a way to achieve this what I am trying?

Here is my query:
> {

                query: {
                  bool: {
                    should: [
                        match: {
                          PRIMARY_LOCALE_ID: {
                            query: "en_US",
                            boost: 2
                        match_phrase: {
                          VALUE: "cha"


(Christoph) #2


sorting the search results on a numeric field after filtering the documents by some search should work. So I'd check first that everything is okay with your search. What is the mapping of the field you allow your users to search on?
Your example suggests you are using two different filter criteria (some locale and some other value, its unclear to me chat it represents). If you combine those in a "should" clause, documents satisfying either of those two this get returned, so maybe this is something to look into first.


Sorting by rank after search does not give me correct results. Here is my mapping

"template": "mysites*",
"settings": {
"index": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": "1",
"max_gram": "20"
"analyzer": {
"autocomplete": {
"filter": [
"type": "custom",
"tokenizer": "standard"
"number_of_shards": "1",
"number_of_replicas": "1"
"sites": {
"properties": {
"VALUE": {
"type": "string",
"analyzer": "autocomplete"

The locale is to boost results specific to region. The reason for should is if the search exhausts the results of that region, then the search should return results in other regions (this works). I can even remove the locale in this case and try only sorting the results after search, but sort just sorts everything with the highest rank and not based on my search results

(Christoph) #4

Why are you using the edge_ngram filter with those settings? This explains

because you are indexing each input token with lots if edge ngrams. At search time the analyzer is also applied to your query, so when searching for "char"m this will also be broken donw as "c", "ch", "cha" and "char", so you will get tons of matches. You should define another search_analyzer that doesn't do the edge ngram analysis.


I wanted to have edge-ngram so that I can enable 'search as user types' and not using it would not help my usecase. Can you please confirm
Can you suggest any other mechanism on how I can enable 'search as user types' but not use edge-ngram


(Christoph) #6

As I already mentioned:

(Christoph) #7

This article explains the details.


Setting "search_analyzer": "standard" helped. This works.

Thanks for your help Christoph

(system) #9

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