【ElasticSearch 6.*】 学习十:高级查询之复合条件查询

复合条件查询包含很多种,今天主要介绍固定分数查询布尔查询

固定分数查询

由于我们每查出一个文档,则对于这个文档ES都会赋予一个_score参数,可以这是匹配度的打分。
请求地址(Posthttp://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)

请求地址(Posthttp://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"
                }
            }
        ]
    }
}

请求地址(Posthttp://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"
                }
            }
        ]
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值