Elasticsearch OR operation between phrases

2019-05-29 01:04发布

Is it possible in elasticsearch to get all documents that contain phrase1 OR phrase2 in a single query? I know to match a phrase, one can use the following query:

"query" : {
        "match_phrase" : {
            "title" : "rock climbing"
        }
    }

But how about a case when there are multiple phrases and the goal is to retrieve documents containing either of these phrases.

2条回答
萌系小妹纸
2楼-- · 2019-05-29 01:12

You can wrap the two match_phrase queries in the should clause of a bool query :

{
  "query" : {
    "bool": {
      "should": [
        {
          "match_phrase" : {
            "title" : "phrase1"
          }
        },
        {
          "match_phrase": {
            "title": "phrase2"
          }
        }
      ],
      "minimum_number_should_match": 1
    } 
  }
}
查看更多
爷的心禁止访问
3楼-- · 2019-05-29 01:26

You can perform Query String search

{
    "query_string" : {
        "default_field" : "content",
        "query" : "this AND that OR thus"
    }
}

You can specify the AND / OR clause in query or use default_operator and just write keywords to query parameter. If you want exact phare, just wrap the words to quotes.

查看更多
登录 后发表回答