(newbie) Query elasticsearch as term query with multiple terms with AND and OR


#1

I have a scenario where I want to get documents from elastic which have any one of the following text:

1- "samsung s8"
2- "silverado"
3- "rims"
4- "ping pong"

As one can see here, "samsung s8/"ping pong" should have AND logic becoz i want both of those terms.

Is there a way to achieve this? Right now I form a elastic query like this which obviously is wrong since it can give result which have "s8" but not "samsung".

"must": [
        {
          "query_string": {
            "query": "samsung s8 silverado rims ping pong",
            "fields": [
              "category^1.0",
              "description^1.0",
              "keywords.*^0.0",
              "title^1.0"
            ],
            "use_dis_max": true,
            "tie_breaker": 0,
            "default_operator": "or",
            "auto_generate_phrase_queries": false,
            "max_determinized_states": 10000,
            "enable_position_increments": true,
            "fuzziness": "AUTO",
            "fuzzy_prefix_length": 0,
            "fuzzy_max_expansions": 50,
            "phrase_slop": 0,
            "escape": false,
            "split_on_whitespace": true,
            "boost": 1
          }
        }
      ],

Some help would be highly appreciated.


(Mayya Sharipova) #2

In query_string query, you can surround your query in double quotes to make it a phrase. Thus, if you are searching for phrases: "samsung s8" or "ping pong", your query could be:
"samsung s8" silverado rims "ping pong"