这是比较复杂一点的示例:
{
"from": 0,
"query": {
"bool": {
"minimum_should_match": "1",
"must": [
{
"query_string": {
"query": "status:0"
}
},
{
"query_string": {
"query": "delFlag:0"
}
},
{
"terms": {
"code.keyword": [
"11016400",
"11054600",
"11010300",
"11011400",
"11001500"
]
}
}
],
"should": [
{
"query_string": {
"query": "shortName.keyword:*手机*"
}
},
{
"query_string": {
"query": "code.keyword:*手机*"
}
}
]
}
},
"size": 10,
"sort": [
{
"sortNum": {
"order": "desc"
}
}
]
}
下面拆解一下功能:
"must": [
{
"query_string": {
"query": "status:0"
}
},
{
"query_string": {
"query": "delFlag:0"
}
},
{
"terms": {
"code.keyword": [
"11016400",
"11054600",
"11010300",
"11011400",
"11001500"
]
}
}
]
精准匹配 关键字说明
must 是必选满足该条件才能查询到数据
query_string 指定字段精准匹配
terms 相当于sql中的in查询 【code】是分片中的字段
"minimum_should_match": "1",
"should": [
{
"query_string": {
"query": "shortName.keyword:*手机*"
}
},
{
"query_string": {
"query": "code.keyword:*手机*"
}
}
]
模糊匹配 关键字说明
"minimum_should_match": "1" 表示 至少需要匹配一个 should 子句 才能返回文档。
查询逻辑
文档必须满足:
-
所有
must条件(status:0, delFlag:0, code在指定列表中) -
至少一个
should条件(简称或编码包含"手机")
效果对比
-
如果设为
0:即使不满足任何should条件,文档也会返回 -
如果设为
2:必须同时满足两个should条件 -
如果设为
1(当前):只需满足任意一个should条件