elasticsearch bool query combine must with OR

后端 未结 6 755
小蘑菇
小蘑菇 2020-11-28 00:51

I am currently trying to migrate a solr-based application to elasticsearch.

I have this lucene query

(( 
    name:(+foo +bar) 
    OR info:(+foo +bar         


        
6条回答
  •  离开以前
    2020-11-28 01:16

    • OR is spelled should
    • AND is spelled must
    • NOR is spelled should_not

    Example:

    You want to see all the items that are (round AND (red OR blue)):

    {
        "query": {
            "bool": {
                "must": [
                    {
                        "term": {"shape": "round"}
                    },
                    {
                        "bool": {
                            "should": [
                                {"term": {"color": "red"}},
                                {"term": {"color": "blue"}}
                            ]
                        }
                    }
                ]
            }
        }
    }
    

    You can also do more complex versions of OR, for example if you want to match at least 3 out of 5, you can specify 5 options under "should" and set a "minimum_should" of 3.

    Thanks to Glen Thompson and Sebastialonso for finding where my nesting wasn't quite right before.

    Thanks also to Fatmajk for pointing out that "term" becomes "match" in ElasticSearch 6.

提交回复
热议问题