4

What is the difference between such query:

"query": {
"bool": {
 ...
  "should": [
    {
      "match": {
        "description": {
          "query": "test"              
        }
      }
    },
    {
      "match": {
        "address": {
          "query": "test",              
        }
      }
    },
    {
      "match": {
        "country": {
          "query": "test"              
        }
      }
    },
    {
      "match": {
        "city": {
          "query": "test"
        }
      }
    }        
  ]
}}

and that one:

"query": {
"bool": {
 ...      
  "should": [        
    {
      "query_string": {
        "query": "test",
        "fields": [
          "description",
          "address",
          "country",
          "city"              
        ]
      }
    }
  ]
}}

Performance, relevance?

Thanks in advance!

2 Answers 2

6

The query is analyzed depending on the field analyzer (unless you specify the analyzer in the query itself), thus querying multiple fields with a single query doesn't necessarily mean analyzing the query only once.

Keep in mind that the query_string supports the lucene query syntax: AND and OR operators, querying on specific fields, wildcard, phrase queries etc. therefore it needs to be parsed, which I don't think makes a lot of difference here in terms of performance, but it is error prone and might lead to errors. If you don't need all that power, stick to the match query, and if you want to perform the same query on multiple fields, have a look at the multi_match query, which does what you did with your query_string but translates internally to multiple match queries.

Also, the scores returned if you compare the output of multiple match queries and your query_string might be quite different. Using a bool query you effectively build a lucene boolean query, while the query_string uses by default "use_dis_max":"true", which means it uses internally a dis_max query by default. Same happens using the multi_match query. If you set use_dis_max to false a bool query is going to be used internally instead.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, the multi_match query is really applicable to that case. I need to read manual deeply :)
0

I terms of performance, I would say that the second query will have performance benefits because, the first query requires the query string to be analyzed for all the four match sections, while in the second there is only one query string that needs to be analyzed.

Apart from that, there are some comparisons done over here that you can look at.

I am not quite sure about the relevancy differences, but that you can always fire these two queries and see if there is any difference in relevance from the results fetched.

2 Comments

Actually, the query is analyzed depending on the field analyzer, thus querying multiple fields doesn't necessarily mean analyzing the query only once.
Ok, I think I have missed that, thanks for the update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.