4

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.

1
  • Your query looks correct. mgm's answer (lower case) could be it. So, check your analyzers. Commented Dec 23, 2014 at 7:36

4 Answers 4

9

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)"
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Well, yes. You can build a filtered quesry based on a match_all query with the filters that interest you, combinig and or or filters. Here, you want to return docs that have one of thoses 3 phrases. I assume you do not want "I love my cake, and I have a iphone" to return.

So, something like that (do not know if it is efficient enough, tough).

{
"filtered" : {
    "query" : {
        "match_all" : {}
    },
    "filter" : {
        "or" : {
            "filters" : [{
                    "fquery" : {
                        "query" : {
                            "query_string" : {
                                "query" : "\"I love my iphone\"",
                                "default_field" : "searchContent",
                                "default_operator" : "and"
                            }
                        }
                    }
                },{
                    "fquery" : {
                        "query" : {
                            "query_string" : {
                                "query" : "\"I love my htc\"",
                                "default_field" : "searchContent",
                                "default_operator" : "and"
                            }
                        }
                    }
                },
                {
                    "fquery" : {
                        "query" : {
                            "query_string" : {
                                "query" : "\"I love my samsung\"",
                                "default_field" : "searchContent",
                                "default_operator" : "and"
                            }
                        }
                    }
                }
            ]
        }
    }
}

}

1 Comment

Thanks for the Reply . But i don't have an Fixed values as I mentioned . Query may be Anything like : "\"I have my own\" AND (Laptop OR Bike OR Chopper)" As i gone through various Posts , they Suggest to use "Query String Query" which converts it into boolean Logic that we passed, But here i didn't get that Output. Thanks, Aniruddha
0

Try this...

{
  "query": {
            "query_string": {
                "default_field": "SearchContent",
               "query": "I Love my *"

            }
        }
}

Wild card doesn't scale well if the data is too large. But, this can be of some help if performance is not a major concern (less amount of data).

1 Comment

That doesn't really answer the question, does it?
-2

You can use the below query string to search for the same.

{
    "query_string" : {
        "fields" : ["content", "my_field"],
        "query" : "\"I love my \" HTC Iphone Samsung"

    }
}

Try this it may workout for you. or check : http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html

1 Comment

By default, all terms are OR-connected. Therefore, this search would not necessarily contain "I love my".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.