0

e.g., I have two documents like "tasty apple is good" and "tasty banana is good".

Now I want to use the query_string to match the two phrases "tasty apple" and "tasty banana". In general, I can use query like

"\"tasty apple\" OR \"tasty banana\""

to match these documents.

But here I want the query like

"\"tasty (apple OR banana)\""

to match. It seems like the es doesn't support parentheses and bool in the phrase mode.

The reason why I need this above is as the number of the search word increasing, the valid query_string is more complicated.

For example when I want to search the

"\"(tasty OR nasty OR good) (apple OR banana OR grape)\""

I don't want to divide this query_string like

"\"tasty apple\" OR \"tasty banana\" OR \"tasty grape\" OR \"nasty apple\" OR ..."

1 Answer 1

2

Ingest documents

POST test_david_zhao/_doc
{
  "text": "tasty apple is good"
}

POST test_david_zhao/_doc
{
  "text": "tasty grape is good"
}

POST test_david_zhao/_doc
{
  "text": "tasty banana is good"
}

POST test_david_zhao/_doc
{
  "text": "nasty banana is bad"
}

POST test_david_zhao/_doc
{
  "text": "dirty grape is bad"
}

Query

POST test_david_zhao/_search
{
  "query": {
    "query_string": {
      "default_field": "text",
      "query": "+(text:apple text:banana text: grape) +(text:tasty text:nasty)"
    }
  }
}

Response

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 2.261763,
    "hits" : [
      {
        "_index" : "test_david_zhao",
        "_type" : "_doc",
        "_id" : "d6t3_HcBDMyXCx985YKJ",
        "_score" : 2.261763,
        "_source" : {
          "text" : "nasty banana is bad"
        }
      },
      {
        "_index" : "test_david_zhao",
        "_type" : "_doc",
        "_id" : "dKt3_HcBDMyXCx9804Le",
        "_score" : 1.9252907,
        "_source" : {
          "text" : "tasty apple is good"
        }
      },
      {
        "_index" : "test_david_zhao",
        "_type" : "_doc",
        "_id" : "dat3_HcBDMyXCx982oIq",
        "_score" : 1.4144652,
        "_source" : {
          "text" : "tasty grape is good"
        }
      },
      {
        "_index" : "test_david_zhao",
        "_type" : "_doc",
        "_id" : "dqt3_HcBDMyXCx984IJD",
        "_score" : 1.4144652,
        "_source" : {
          "text" : "tasty banana is good"
        }
      }
    ]
  }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer! Here I want to use the phrase slop parameter in the query_string, so I need to use the phrase mode. It seems like this query cannot use slop to define the minimal distance between tasty and apple.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.