复合条件查询包含很多种,今天主要介绍
固定分数查询和布尔查询。
固定分数查询
由于我们每查出一个文档,则对于这个文档ES都会赋予一个_score参数,可以这是匹配度的打分。
请求地址(Post) http://localhost:9200/_search
请求参数:
constant_score:固定分数查询关键字(它支持filter,不支持match)
boost:指定固定分数字段。
{
\"query\": {
\"constant_score\":{
\"filter\": {
\"match\": {
\" \": \"陆小凤\"
}
},
\"boost\": 1
}
}
}
返回值:
{
\"took\": 691,
\"timed_out\": false,
\"_shards\": {
\"total\": 6,
\"successful\": 6,
\"skipped\": 0,
\"failed\": 0
},
\"hits\": {
\"total\": 1,
\"max_score\": 1,
\"hits\": [
{
\"_index\": \"book\",
\"_type\": \"noval\",
\"_id\": \"4\",
\"_score\": 1,
\"_source\": {
\" \": \"陆小凤啊\",
\"author\": \"古龙\",
\"word_count\": \"11\",
\"publish_date\": \"1910-09-20\"
}
}
]
}
}
布尔查询(should must must_not)
请求地址(Post) http://localhost:9200/_search
请求参数:
bool:布尔查询关键字
should: 需要满足的条件(或者的关系)
{
\"query\": {
\"bool\":{
\"should\":[
{
\"match\": {
\" \": \"陆小凤\"
}
},
{
\"match\": {
\" \": \"郭靖\"
}
}
]
}
}
}
返回值:
{
\"took\": 248,
\"timed_out\": false,
\"_shards\": {
\"total\": 6,
\"successful\": 6,
\"skipped\": 0,
\"failed\": 0
},
\"hits\": {
\"total\": 2,
\"max_score\": 4.4556303,
\"hits\": [
{
\"_index\": \"book\",
\"_type\": \"noval\",
\"_id\": \"4\",
\"_score\": 4.4556303,
\"_source\": {
\" \": \"陆小凤啊\",
\"author\": \"古龙\",
\"word_count\": \"11\",
\"publish_date\": \"1910-09-20\"
}
},
{
\"_index\": \"book\",
\"_type\": \"noval\",
\"_id\": \"8\",
\"_score\": 1.4723401,
\"_source\": {
\" \": \"郭靖啊\",
\"author\": \"金庸\",
\"word_count\": \"212\",
\"publish_date\": \"1910-09-20\"
}
}
]
}
}
请求地址(Post) http://localhost:9200/_search
请求参数:
bool:布尔查询关键字
must: 需要满足的条件(并且的关系)
请求参数:
{
\"query\": {
\"bool\":{
\"must\":[
{
\"match\": {
\" \": \"陆小凤啊\"
}
},
{
\"match\": {
\"word_count\": 11
}
}
]
}
}
}
返回值:
{
\"took\": 1644,
\"timed_out\": false,
\"_shards\": {
\"total\": 6,
\"successful\": 6,
\"skipped\": 0,
\"failed\": 0
},
\"hits\": {
\"total\": 1,
\"max_score\": 5.688145,
\"hits\": [
{
\"_index\": \"book\",
\"_type\": \"noval\",
\"_id\": \"4\",
\"_score\": 5.688145,
\"_source\": {
\" \": \"陆小凤啊\",
\"author\": \"古龙\",
\"word_count\": \"11\",
\"publish_date\": \"1910-09-20\"
}
}
]
}
}
must + filter 组合查找
请求参数:
filter:筛选条件(和must一样是数组)
{
\"query\": {
\"bool\":{
\"must\":[
{
\"match\": {
\" \": \"陆小凤啊\"
}
},
{
\"match\": {
\"author\": \"古龙\"
}
}
],
\"filter\":[
{
\"term\":{
\"word_count\": 22
}
}
]
}
}
}
返回值:
{
\"took\": 12,
\"timed_out\": false,
\"_shards\": {
\"total\": 6,
\"successful\": 6,
\"skipped\": 0,
\"failed\": 0
},
\"hits\": {
\"total\": 1,
\"max_score\": 0.6743476,
\"hits\": [
{
\"_index\": \"book\",
\"_type\": \"noval\",
\"_id\": \"6\",
\"_score\": 0.6743476,
\"_source\": {
\" \": \"花满楼啊\",
\"author\": \"古龙\",
\"word_count\": \"22\",
\"publish_date\": \"1910-09-20\"
}
}
]
}
}
must_not + filter查找
请求参数:
must_not:一定不查找关键字(否定)
filter:必须满足条件
{
\"query\": {
\"bool\":{
\"must_not\":[
{
\"match\": {
\" \": \"陆小凤\"
}
},
{
\"match\": {
\"author\": \"金庸\"
}
}
],
\"filter\":[
{
\"term\":{
\"word_count\": 22
}
}
]
}
}
}
返回值:
{
\"took\": 440,
\"timed_out\": false,
\"_shards\": {
\"total\": 6,
\"successful\": 6,
\"skipped\": 0,
\"failed\": 0
},
\"hits\": {
\"total\": 1,
\"max_score\": 0,
\"hits\": [
{
\"_index\": \"book\",
\"_type\": \"noval\",
\"_id\": \"6\",
\"_score\": 0,
\"_source\": {
\" \": \"花满楼啊\",
\"author\": \"古龙\",
\"word_count\": \"22\",
\"publish_date\": \"1910-09-20\"
}
}
]
}
}
继续阅读与本文标签相同的文章
-
Cassandra gossip介绍系列之一
2026-05-18栏目: 教程
-
GO学习笔记 - 数据校验
2026-05-18栏目: 教程
-
源码分析 RocketMQ DLedger 多副本之 Leader 选主
2026-05-18栏目: 教程
-
源码分析 RocketMQ DLedger 多副本存储实现
2026-05-18栏目: 教程
-
阿里巴巴小程序繁星计划技术能力支持指南
2026-05-18栏目: 教程
