2

Let's say I have the following query string that the user enters - "dark blue shoes and water washable"

Basically I want to search for both "dark blue shoes" and "water washable" in product_description OR product_title field.

And also the same goes for "dark blue shoes or water washable", so instead of "and" operator, use a "or" operator.

Is it possible to provide multiple phrases to Elasticsearch with either "and/or" operator

I am using v7.1 if that matters.

kapso
  • 11,703
  • 16
  • 58
  • 76

1 Answers1

1

You can use Query_string and pass operator in the text itself

Data:

{
        "_index" : "index72",
        "_type" : "_doc",
        "_id" : "p7_JqHABcSMy6UhGDmtS",
        "_score" : 1.0,
        "_source" : {
          "product_description" : "dark blue shoes water washable"
        }
},
{
        "_index" : "index72",
        "_type" : "_doc",
        "_id" : "qL_JqHABcSMy6UhGbWsn",
        "_score" : 1.0,
        "_source" : {
          "product_description" : "dark blue"
        }
}

Query:

{
    "query": {
        "query_string" : {
            "fields" : ["product_description", "product_title"],
            "query" : "dark blue shoes AND water washable"
        }
    }
}

Result:

{
        "_index" : "index72",
        "_type" : "_doc",
        "_id" : "p7_JqHABcSMy6UhGDmtS",
        "_score" : 2.0794973,
        "_source" : {
          "product_description" : "dark blue shoes water washable"
        }
}
jaspreet chahal
  • 8,817
  • 2
  • 11
  • 29