版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31617637/article/details/85119903
复合条件查询包含很多种,今天主要介绍
固定分数查询
和布尔查询
。
固定分数查询
由于我们每查出一个文档,则对于这个文档ES都会赋予一个_score
参数,可以这是匹配度的打分。
请求地址(Post
) http://localhost:9200/_search
请求参数:
constant_score
:固定分数查询关键字(它支持filter
,不支持match
)
boost
:指定固定分数字段。
{
"query": {
"constant_score":{
"filter": {
"match": {
"title": "陆小凤"
}
},
"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": {
"title": "陆小凤啊",
"author": "古龙",
"word_count": "11",
"publish_date": "1910-09-20"
}
}
]
}
}
布尔查询(should must must_not)
请求地址(Post
) http://localhost:9200/_search
请求参数:
bool
:布尔查询关键字
should
: 需要满足的条件(或者的关系
)
{
"query": {
"bool":{
"should":[
{
"match": {
"title": "陆小凤"
}
},
{
"match": {
"title": "郭靖"
}
}
]
}
}
}
返回值:
{
"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": {
"title": "陆小凤啊",
"author": "古龙",
"word_count": "11",
"publish_date": "1910-09-20"
}
},
{
"_index": "book",
"_type": "noval",
"_id": "8",
"_score": 1.4723401,
"_source": {
"title": "郭靖啊",
"author": "金庸",
"word_count": "212",
"publish_date": "1910-09-20"
}
}
]
}
}
请求地址(Post
) http://localhost:9200/_search
请求参数:
bool
:布尔查询关键字
must
: 需要满足的条件(并且的关系
)
请求参数:
{
"query": {
"bool":{
"must":[
{
"match": {
"title": "陆小凤啊"
}
},
{
"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": {
"title": "陆小凤啊",
"author": "古龙",
"word_count": "11",
"publish_date": "1910-09-20"
}
}
]
}
}
must + filter 组合查找
请求参数:
filter
:筛选条件(和must一样是数组
)
{
"query": {
"bool":{
"must":[
{
"match": {
"title": "陆小凤啊"
}
},
{
"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": {
"title": "花满楼啊",
"author": "古龙",
"word_count": "22",
"publish_date": "1910-09-20"
}
}
]
}
}
must_not + filter查找
请求参数:
must_not
:一定不查找关键字(否定)
filter
:必须满足
条件
{
"query": {
"bool":{
"must_not":[
{
"match": {
"title": "陆小凤"
}
},
{
"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": {
"title": "花满楼啊",
"author": "古龙",
"word_count": "22",
"publish_date": "1910-09-20"
}
}
]
}
}