1

Elastic Search 7.9

I'm searching a single field with a textbox exposed to users through a web UI.

    {
        match: {
            body: {
                query: 'beer pretzels',
            }
        }
    }

I'm debating whether to use simple_query_string instead.

    {
        simple_query_string: {
          query: 'beer pretzels',
        }
    }

My initial thought was to switch to simple_query_string if I detect special characters in the keywords. But now I wonder why I'd use match at all.

My questions:

Are there any differences between match and simple_query_string for the simple case where the keywords contains no special characters?

Any reason why I would not use simple_query_string all the time?

Kevin Lawrence
  • 698
  • 7
  • 23

1 Answers1

0

Simple Query string returns documents based on a provided query string, using a parser with a limited but fault-tolerant syntax.

Refer this to get a detailed explanation, which states that :

The simple_query_string query is a version of the query_string query that is more suitable for use in a single search box that is exposed to users because it replaces the use of AND/OR/NOT with +/|/-, respectively, and it discards invalid parts of a query instead of throwing an exception if a user makes a mistake.

It supports Lucene syntax to interpret the text, you can refer this article that gives detailed information about how simple query string works.

Match Query returns documents that match a provided text, number, date or boolean value. The provided text is analyzed before matching.

Refer to this ES documentation part and this blog, to understand how the match query works

I have tried to run this below search query using both simple query string and match query:

Index Data

{
    "content":"foo bar -baz"
}

Search Query using simple query string:

{
  "query": {
    "simple_query_string": {
      "fields": [ "content" ],
      "query": "foo bar -baz"
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "stof_63937563",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.5753642,      <-- note this
        "_source": {
          "content": "foo bar -baz"
        }
      }
    ]

Search Query using match query:

{
  "query": {
    "match": {
      "content": {
        "query": "foo bar -baz"
      }
    }
  }
}

Search Result:

"hits": [
      {
        "_index": "stof_63937563",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.8630463,    <-- note this
        "_source": {
          "content": "foo bar -baz"
        }
      }
    ]

Please refer this SO answer that explains the difference between multi_match and query_string

ESCoder
  • 15,431
  • 2
  • 19
  • 42
  • Thank you. I am interested in the case where the keywords contain no special characters. Is there any difference between match and simple_query_string in that case? – Kevin Lawrence Sep 17 '20 at 12:52
  • @KevinLawrence As far as I know there will be no difference in the score when querying on keywords containing no special characters by using `match` or `simple query string`. I have tested it locally also, when I search for `"content": "foo bar baz"`, the score generated by both the queries is same i.e. `0.8630463`. – ESCoder Sep 17 '20 at 17:31
  • @KevinLawrence please go through my updated answer, and please don't forget to upvote and accept the answer, if it helped you resolve your queries :) – ESCoder Sep 17 '20 at 17:32