Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic search Query String Query

I am trying to get records using the Query String Query .
My scenario for searching is as follows :
I have to search like : "I Love my " AND (HTC OR Iphone OR Samsung)
And I am expecting result like :

  • I Love my HTC
  • I Love my Iphone
  • I Love my Samsung

I just tried some combinations but its not working

{
  "query": {
            "query_string": {
                "default_field": "SearchContent",
               "query": "\"I Love my\" AND (HTC OR Iphone OR Samsung)"
            }
        }
}

How can i do this with Query String Or is there any Other Option , i am stuck. Help me out.

like image 556
AniPol Avatar asked Mar 22 '23 14:03

AniPol


1 Answers

The query depends on your _mapping. If you haven't defined any, then probably the standard analyzer is used. If so then tokens are:

  • I Love my HTC -> "i", "love", "my", "htc"
  • I Love my Iphone -> "i", "love", "my", "iphone"
  • I Love my Samsung -> "i", "love", "my", "samsung"

notice the tokens are lowercase.

The proper format for your query string is:

GET /yourindex/_search
{
  "query": {
    "query_string": {
      "default_field": "SearchContent",
      "query": "\"i love my\" AND (samsung OR htc OR iphone)"
    }
  }
}
like image 101
kgm Avatar answered Mar 31 '23 17:03

kgm