在上一讲我们可以发现,对于multi-value的搜索方式,实现起来可以有多种方式。这里就说明一下,实现的方式虽然很多,但是elasticsearch在查询的时候底层都会转换为bool + term的形式

1、普通的match如何转换为term+should

{    "match": {        "title": "java elasticsearch"    }}

使用类似上面的match query进行多值搜索的时候,elasticsearch会在底层自动将这个match query转换为bool的语法

{    "bool": {        "should": [            {                "term": {                    "title": "java"                }            },            {                "term: {                    "title": "elasticsearch"                }            }        ]    }}

2、and match 如何转换为term+must

{    "match": {        "title": {            "query": "java elasticsearch",            "operator": "and"        }    }}

转换为:

{    "bool": {        "must": [            {                "term": {                    "title": "java"                }            },            {                "term": {                    "title": "elasticsearch"                }            }        ]    }}

3、minimum_should_match如何转换

{    "match": {        "title": {            "query": "java elasticsearch spark hadoop",            "minimum_should_match": 3        }    }}

转换为:

{    "bool": {        "should": [            {                "term": {                    "title": "java"                }            },            {                "term": {                    "title": "elasticsearch"                }            },            {                "term": {                    "title": "spark"                }            },            {                "term": {                    "title": "hadoop"                }            }        ],        "minimum_should_match": 3    }}