当前位置: 代码网 > it编程>编程语言>Asp.net > 7-Elasticsearch组合查询和全文检索

7-Elasticsearch组合查询和全文检索

2024年08月03日 Asp.net 我要评论
布尔查询是把多个子查询组合成一个布尔表达式,所有子查询之间逻辑关系是and,只有当一个文档满足布尔查询中的所有子查询条件时,ES才会认为该文档满足查询条件。使用function_score查询,必须为查询定义一个或者多个函数,这些函数将为查询返回的每个文档计算一个新的评分。dis_max值的是在文档匹配评分中,只将最佳匹配的评分作为查询的评分结果返回。它将多个查询条件组合在一起,并且将查询的结果和结果的评分组合在一起。​2、查询语句中可以嵌套多个查询条件。组合查询中的常用的查询方式:布尔查询。

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" : [
            "音乐",
            "牛奶",
            "剑术"
          ]
        }
      }
    ]
  }
}
(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com