elasticsearch组合查询
组合查询–布尔查询
测试:
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"
}
}
]
}
}
组合查询–固定评分查询
案例:
# 组合查询--固定评分查询
# 数据准备
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" : "西游记-唐僧"
}
}
]
}
}
组合查询–最佳匹配查询
# 组合查询--最佳匹配查询
# 数据准备
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" : "你要搞清楚自己的人生副本,不是你父母的续集,不是你子女的前传,更不是你朋友的外篇。"
}
}
]
}
}
组合查询–使用函数查询
# random_score
get one_piece_character_index/_search
{
"query": {
"function_score": {
"query": {"match_all": {}},
"boost": 5,
"random_score": {},
"boost_mode": "multiply"
}
}
}
查询结果:
{
"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" : [
"音乐",
"牛奶",
"剑术"
]
}
}
]
}
}
发表评论