Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is Elastic Search sorting when no sort option specified and no search query specified

I wonder how Elastic search is sorting (on what field) when no search query is specified (I just filter on documents) and no sort option specified. It looks like sorting is than random ... Default sort order is _score, but score is always 1 when you do not specify a search query ...

like image 276
cyclomarc Avatar asked May 28 '14 18:05

cyclomarc


People also ask

How does Elasticsearch sort work?

In order to sort by relevance, we need to represent relevance as a value. In Elasticsearch, the relevance score is represented by the floating-point number returned in the search results as the _score, so the default sort order is _score descending. In the previous example, we searched for movies from 1962.

What is the default sort order in Elasticsearch?

The order defaults to desc when sorting on the _score , and defaults to asc when sorting on anything else.

How do you search a specific field in Elasticsearch?

To retrieve specific fields in the search response, use the fields parameter. Because it consults the index mappings, the fields parameter provides several advantages over referencing the _source directly. Specifically, the fields parameter: Returns each value in a standardized way that matches its mapping type.


2 Answers

Just in case someone may see this post even it posted over 6 yrs ago..

When you wanna know how elasticsearch calculate its own score known as _score, you can use the explain option.

I suppose that your query(with filter & without search) might like this more or less (but the point is making the explain option true) :

    POST /goods/_search
    {
        "explain": true, 
        "query": {
            "bool": {
                "must": {
                    "match_all": {}
                },
                "filter": {
                    "term": {
                        "maker_name": "nike"
                    }
                }
            }
        }
    }

As running this, you will notice that the _explaination of each hits describes as below :

    "_explanation" : {
        "value" : 1.0,
        "description" : "ConstantScore(maker_name:nike)",
        "details" : [ ]
    }

which means ES gave constant score to all of the hits.

So to answer the question, "yes". The results are sorted kinda randomly because all the filtered results have same (constant) score without any search query.

By the way, enabling an explain option is more helpful when you use search queries. You will see how ES calculates the score and will understand the reason why it returns in that order.

like image 188
HB HONG Avatar answered Sep 19 '22 08:09

HB HONG


You got it right. Its then more or less random with score being 1. You still get consistent results as far as I remember. You have the "same" when you get results in SQL but don't specify ORDER BY.

like image 31
Torsten Engelbrecht Avatar answered Sep 18 '22 08:09

Torsten Engelbrecht