Elasticsearch组合查询
组合查询–布尔查询
组合查询中的常用的查询方式:布尔查询。
它将多个查询条件组合在一起,并且将查询的结果和结果的评分组合在一起。
布尔查询是把多个子查询组合成一个布尔表达式,所有子查询之间逻辑关系是and,只有当一个文档满足布尔查询中的所有子查询条件时,ES才会认为该文档满足查询条件。
特点:
1、子查询可以任意顺序出现
2、查询语句中可以嵌套多个查询条件。
布尔查询包含的操作符:
- must:必须匹配
- must_not:必须不匹配,过滤子句
- should:选择性匹配,至少满足一条。
- filter:必须匹配,过滤子句
测试:
1# 数据准备
DELETE one_piece_character_index
POST one_piece_character_index/_bulk
{"index":{}}
{"name":"路飞","identity":"船长","age":18,"interests":["美食","探险"]}
{"index":{}}
{"name":"索隆","identity":"赏金猎人","age":20,"interests":["睡觉","修炼","喝酒"]}
{"index":{}}
{"name":"娜美","identity":"测量员","age":19,"interests":["钱","橘子","美食"]}
{"index":{}}
{"name":"乌索普","identity":"狙击手","age":18,"interests":["发明武器"]}
{"index":{}}
{"name":"山治","identity":"厨师","age":20,"interests":["美食制作","抽烟","美女"]}
{"index":{}}
{"name":"乔巴","identity":"船医","age":16,"interests":["甜食","医疗"]}
{"index":{}}
{"name":"罗宾","identity":"考古家","age":25,"interests":["考古","美食"]}
{"index":{}}
{"name":"弗兰奇","identity":"船工","age":33,"interests":["造船","可乐"]}
{"index":{}}
{"name":"布鲁克","identity":"音乐家","age":88,"interests":["音乐","牛奶","剑术"]}
GET one_piece_character_index/_search
{
"query": {
"match_all": {}
}
}
# 组合查询之布尔查询
# 同时满足must,must_not,should
GET one_piece_character_index/_search
{
"query": {
"bool": {
"must_not": [
{
"range": {
"age": {
"gte": 20,
"lte": 88
}
}
}
],
"must": [
{
"term": {
"name.keyword": {
"value": "乔巴"
}
}
}
],
"filter": [
{
"term": {
"identity.keyword": "船医"
}
}
],
"should": [
{
"term": {
"interests.keyword": {
"value": "医疗"
}
}
}
],
"minimum_should_match": 1,
"boost": 1
}
}
}
结果:
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 4.373022,
"hits" : [
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "nwCNyYYBnnKcIM3Dc_j9",
"_score" : 4.373022,
"_source" : {
"name" : "乔巴",
"identity" : "船医",
"age" : 16,
"interests" : [
"甜食",
"医疗"
]
}
}
]
}
}
案例
# 数据准备
DELETE user_info_bool
POST user_info_bool/_doc/_bulk
{"index":{}}
{"name":"张三","address":"中国 北京","age":28,"score":66,"tags":"emp manager love"}
{"index":{}}
{"name":"李四","address":"中国 北京","age":33,"score":77,"tags":"emp love"}
{"index":{}}
{"name":"王五","address":"中国 山东","age":25,"score":88,"tags":"emp manager love"}
{"index":{}}
{"name":"大刀王五","address":"中国 山东","35":28,"score":99,"tags":"manager love"}
# 查询address包含中国,tags包含emp age 不在30到35之间
# "minimum_should_match": 1 表示should中的条件至少命中一个才返回
POST /user_info_bool/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"address": "中国"
}
}
],
"filter": [
{
"term": {
"tags": "emp"
}
}
],
"must_not": [
{
"range": {
"age": {
"gte": 30,
"lte": 35
}
}
}
],
"should": [
{
"term": {
"tags": {
"value": "manager"
}
}
},
{
"term": {
"tags": {
"value": "love"
}
}
}
],
"minimum_should_match": 1,
"boost": 1
}
}
}
查询结果:
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.6378126,
"hits" : [
{
"_index" : "user_info_bool",
"_type" : "_doc",
"_id" : "FV6z94YBIOSTa9Tsa042",
"_score" : 0.6378126,
"_source" : {
"name" : "张三",
"address" : "中国 北京",
"age" : 28,
"score" : 66,
"tags" : "emp manager love"
}
},
{
"_index" : "user_info_bool",
"_type" : "_doc",
"_id" : "F16z94YBIOSTa9Tsa042",
"_score" : 0.6378126,
"_source" : {
"name" : "王五",
"address" : "中国 山东",
"age" : 25,
"score" : 88,
"tags" : "emp manager love"
}
}
]
}
}
组合查询–提高评分查询
提高评分查询就是降低文档评分,对于满足部分条件的文档数据,不是返回,而是降低显示的优先级
降分案例
# 组合查询--提高评分查询
POST up_score_bool/_bulk
{"index":{"_id":1}}
{"content":"Apple Mac"}
{"index":{"_id":2}}
{"content":"Apple Fruit"}
{"index":{"_id":3}}
{"content":"Apple And Pie"}
POST up_score_bool/_search
# 查询content中包含apple,并对包含pie的文档进行降分处理
POST up_score_bool/_search
{
"query": {
"boosting": {
"positive": {
"match": {
"content": "Apple"
}
},
"negative": {
"match": {
"content": "Pie"
}
},
"negative_boost": 0.5 ## 降分
}
}
}
结果:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.14181954,
"hits" : [
{
"_index" : "up_score_bool",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.14181954,
"_source" : {
"content" : "Apple Mac"
}
},
{
"_index" : "up_score_bool",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.14181954,
"_source" : {
"content" : "Apple Fruit"
}
},
{
"_index" : "up_score_bool",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.059778586,
"_source" : {
"content" : "Apple And Pie"
}
}
]
}
}
提分案例:
POST up_score_bool/_search
{
"query": {
"boosting": {
"positive": {
"match": {
"content": "Apple"
}
},
"negative": {
"match": {
"content": "Pie"
}
},
"negative_boost": 1.5 ## 提分
}
}
}
结果:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 0.17933576,
"hits" : [
{
"_index" : "up_score_bool",
"_type" : "_doc",
"_id" : "3",
"_score" : 0.17933576,
"_source" : {
"content" : "Apple And Pie"
}
},
{
"_index" : "up_score_bool",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.14181954,
"_source" : {
"content" : "Apple Mac"
}
},
{
"_index" : "up_score_bool",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.14181954,
"_source" : {
"content" : "Apple Fruit"
}
}
]
}
}
组合查询–固定评分查询
根据某个条件查询时,只需使用filter和constant_score处理,返回固定评分。
案例:
# 组合查询--固定评分查询
# 数据准备
POST constant_score/_bulk
{"index":{"_id":1}}
{"content":"西游记-唐僧"}
{"index":{"_id":2}}
{"content":"西游记-孙悟空"}
# 固定评分查询
GET constant_score/_search
{
"query": {
"constant_score": {
"filter": {
"term": {
"content.keyword": "西游记-唐僧"
}
},
"boost": 1.2
}
}
}
查询结果:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.2,
"hits" : [
{
"_index" : "constant_score",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.2,
"_source" : {
"content" : "西游记-唐僧"
}
}
]
}
}
组合查询–最佳匹配查询
dis_max值的是在文档匹配评分中,只将最佳匹配的评分作为查询的评分结果返回。
# 组合查询--最佳匹配查询
# 数据准备
POST dis_max/_bulk
{"index":{"_id":1}}
{"title":"何为人生之路","content":"人生这条路很长, 未来如星辰大海般璀璨,不必踌躇于过去的半亩方塘。"}
{"index":{"_id":1}}
{"title":"何为人生副本","content":"你要搞清楚自己的人生副本,不是你父母的续集,不是你子女的前传,更不是你朋友的外篇。"}
查询:
GET dis_max/_search
{
"query": {
"dis_max": {
"queries": [
{
"match": {
"title": "副本"
}
},
{
"match": {
"content": "副本"
}
}
]
}
}
}
查询结果:
{
"took" : 16,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.3862942,
"hits" : [
{
"_index" : "dis_max",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.3862942,
"_source" : {
"title" : "何为人生副本",
"content" : "你要搞清楚自己的人生副本,不是你父母的续集,不是你子女的前传,更不是你朋友的外篇。"
}
}
]
}
}
案例:
GET dis_max/_search
{
"query": {
"dis_max": {
"tie_breaker": 0.5,
"boost": 1.2,
"queries": [
{
"match": {
"title": "副本"
}
},
{
"match": {
"content": "副本"
}
}
]
}
}
}
查询结果:
{
"took" : 10,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 2.4560332,
"hits" : [
{
"_index" : "dis_max",
"_type" : "_doc",
"_id" : "1",
"_score" : 2.4560332,
"_source" : {
"title" : "何为人生副本",
"content" : "你要搞清楚自己的人生副本,不是你父母的续集,不是你子女的前传,更不是你朋友的外篇。"
}
}
]
}
}
组合查询–使用函数查询
ES中使用function_score进行查询可以让用户修改文档的评分。
使用function_score查询,必须为查询定义一个或者多个函数,这些函数将为查询返回的每个文档计算一个新的评分。
ES预定义函数:文章来源:https://www.toymoban.com/news/detail-759976.html
- weight:表示为每个文档应用用一个简单的权重,当weight=2时,score=2 * _score
- field_value_factor:修改_score的值
- random_score:为每个用户都使用一个不同的随机评分
- script_score:使用自定义脚本控制评分计算
# random_score
GET one_piece_character_index/_search
{
"query": {
"function_score": {
"query": {"match_all": {}},
"boost": 5,
"random_score": {},
"boost_mode": "multiply"
}
}
}
查询结果:文章来源地址https://www.toymoban.com/news/detail-759976.html
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 9,
"relation" : "eq"
},
"max_score" : 3.2187843,
"hits" : [
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "mwCNyYYBnnKcIM3Dc_j9",
"_score" : 3.2187843,
"_source" : {
"name" : "索隆",
"identity" : "赏金猎人",
"age" : 20,
"interests" : [
"睡觉",
"修炼",
"喝酒"
]
}
},
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "nwCNyYYBnnKcIM3Dc_j9",
"_score" : 3.0455537,
"_source" : {
"name" : "乔巴",
"identity" : "船医",
"age" : 16,
"interests" : [
"甜食",
"医疗"
]
}
},
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "mgCNyYYBnnKcIM3Dc_j5",
"_score" : 2.842673,
"_source" : {
"name" : "路飞",
"identity" : "船长",
"age" : 18,
"interests" : [
"美食",
"探险"
]
}
},
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "ngCNyYYBnnKcIM3Dc_j9",
"_score" : 2.3572967,
"_source" : {
"name" : "山治",
"identity" : "厨师",
"age" : 20,
"interests" : [
"美食制作",
"抽烟",
"美女"
]
}
},
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "oACNyYYBnnKcIM3Dc_j9",
"_score" : 2.1358142,
"_source" : {
"name" : "罗宾",
"identity" : "考古家",
"age" : 25,
"interests" : [
"考古",
"美食"
]
}
},
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "oQCNyYYBnnKcIM3Dc_j9",
"_score" : 1.5156072,
"_source" : {
"name" : "弗兰奇",
"identity" : "船工",
"age" : 33,
"interests" : [
"造船",
"可乐"
]
}
},
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "nACNyYYBnnKcIM3Dc_j9",
"_score" : 1.1634743,
"_source" : {
"name" : "娜美",
"identity" : "测量员",
"age" : 19,
"interests" : [
"钱",
"橘子",
"美食"
]
}
},
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "nQCNyYYBnnKcIM3Dc_j9",
"_score" : 1.0768002,
"_source" : {
"name" : "乌索普",
"identity" : "狙击手",
"age" : 18,
"interests" : [
"发明武器"
]
}
},
{
"_index" : "one_piece_character_index",
"_type" : "_doc",
"_id" : "ogCNyYYBnnKcIM3Dc_j9",
"_score" : 0.36010146,
"_source" : {
"name" : "布鲁克",
"identity" : "音乐家",
"age" : 88,
"interests" : [
"音乐",
"牛奶",
"剑术"
]
}
}
]
}
}
到了这里,关于7-Elasticsearch组合查询和全文检索的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!