Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's difference between simple_query_string and query_string?

I had a nested field source in my index seems like this:

"source": [    
    {
        "name": "source_c","type": "type_a"
    },
    {
        "name": "source_c","type": "type_b"
    }
]

I used query_string query and simple_query_string query to query type_a and got two different result.

query_string

{
  "size" : 3,
  "query" : {
    "bool" : {
      "filter" : {
        "query_string" : {
          "query" : "source:\"source.type:=\"type_a\"\""
        }
      }
    }
  }
}

I got 163459 hits in 294088 docs.

simple_query_string

{
  "size": 3,
  "query": {
    "bool": {
      "filter": {
        "simple_query_string": {
          "query": "source:\"source.type:=\"type_a\"\""
        }
      }
    }
  }
}

I got 163505 hits in 294088 docs.

I only made three different types type_a,type_b,type_c randomly. So I had to say 163459 and 163505 were very little difference in 294088 docs.

I noly got one info in Elasticsearch Reference [2.1]

Unlike the regular query_string query, the simple_query_string query will never throw an exception, and discards invalid parts of the query.

I don't think it's the reason to make the difference.

I want to know what make the little different results between query_string and simple_query_string?

like image 582
fmyblack Avatar asked Jan 05 '16 03:01

fmyblack


2 Answers

Acording to the documentation simple_query_string is meant to be used with unsafe input.

So that users can enter anything and it will not throw exception if input is invalid. Will simply discard invalid input.

like image 151
PeterM Avatar answered Oct 24 '22 10:10

PeterM


As far as I know, nested query syntax is not supported for either query_string or simple_query_string. It is an open issue, and this is the PR regarding that issue.

Then how are you getting the result? Here Explain API will help you understand what is going on. This query

{
  "size": 3,
  "query": {
    "bool": {
      "filter": {
        "simple_query_string": {
          "query": "source:\"source.type:=\"type_a\"\""
        }
      }
    }
  }
}

have a look at the output, you will see

"description": "ConstantScore(QueryWrapperFilter(_all:source _all:source.type _all:type_a)), 

so what is happening here is that ES looking for term source , source.type or type_a, it finds type_a and returns the result. You will also find something similar with query_string using explain api

Also query_string and simple_query_string have different syntax, for e.g field_name:search_text is not supported in simple_query_string.

Correct way to query nested objects is using nested query

EDIT

This query will give you desired results.

{
  "query": {
    "nested": {
      "path": "source",
      "query": {
        "term": {
          "source.type": {
            "value": "type_a"
          }
        }
      }
    }
  }
}

Hope this helps!!

like image 23
ChintanShah25 Avatar answered Oct 24 '22 08:10

ChintanShah25