Is the query_string query executed in a query or filter context, with custom sorting?

Issue

This Content is from Stack Overflow. Question asked by Jay

I’m confused as to when the query_string query with custom sort order is executed in a query vs filter context. I listed two queries, query 1 deliberately not specifying the filter context and query 2 with the bool expression to “activate” the filter context, both with custom sort orders.

From an ElasticSearch/Lucene point of view, are both queries the same? And do both queries yield the same performance?

Query 1

curl -XGET 'localhost:9200/my-index/_search?pretty' -d'
{
    "query": {
        "query_string": {
            "query": "title:Tesla"
        }
    },
    "sort": { "publishedAt": { "order": "desc"  } }
}`

Query 2

curl -XGET 'localhost:9200/my-index/_search?pretty' -d'
{
    "query": {
        "bool": {
            "filter": {
                "query_string": {
                    "query": "title:Tesla"
                }
            }
        }
    },
    "sort": { "publishedAt": { "order": "desc" } }
}'

Background Info

For my use case, ElasticSearch document scoring is not required. All good with scoring disabled by the custom sort order. I also want to ensure that queries are run in a filter context to ensure shard-based caching for better search performance. At the moment, I’m using query 1, but I’m uncertain as to whether query 1 actually runs in a filter context.



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?