0

I'm inserting my data as follows:

PUT my_index2/doc/1
{
  "key": "party",
  "str_val": "THE TWITTER INC"
}

PUT my_index2/doc/2
{
  "key": "party",
  "str_val": "twitter inc"
}

This query:

POST my_index2/_search
{
  "query": {
    "query_string": {
      "default_field": "str_val",
      "query": "*twitter*"
    }
  }
}

correctly returns both results. If I slightly alter my query to:

POST my_index2/_search
{
  "query": {
    "query_string": {
      "default_field": "str_val",
      "query": "*the twitter*"
    }
  }
}

I'm expecting only one result since *the twitter* should only match "THE TWITTER INC" and not "twitter inc". But two results are returned.

Why is my search returning too many matches?

1 Answer 1

2

That is because the default_operator for query_string is OR. You can always use explain:true to know how the result was returned. Like below:

POST my_index2/_search
{
  "query": {
    "query_string": {
      "default_field": "str_val",
      "query": "*the twitter*"
    }
  },
  "explain": true
}

For your case, you need to explicity mention AND to return only documents where both terms match i.e. the AND twitter

 POST my_index2/_search
    {
      "query": {
        "query_string": {
          "default_field": "str_val",
          "query": "*the twitter*",
           "default_operator": "AND"
        }
      }
    }
Sign up to request clarification or add additional context in comments.

Comments

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.