前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Elasticsearch: 运用 Pinned query 来提高文档的排名 (7.5发行版新功能)

Elasticsearch: 运用 Pinned query 来提高文档的排名 (7.5发行版新功能)

作者头像
腾讯云大数据
修改2021-01-08 16:01:41
9960
修改2021-01-08 16:01:41
举报

腾讯云 Elasticsearch Service】高可用,可伸缩,云端全托管。集成X-Pack高级特性,适用日志分析/企业搜索/BI分析等场景


Pinned 查询用来提升所选文档的排名,使其高于匹配给定查询的文档。 此功能通常用于引导搜索者查找精选的文档,这些文档在搜索的任何 “organic” 匹配项之上被提升。 使用存储在_id字段中的文档 ID 来标识升级或“固定”的文档。

下面有给一个例子来说明:

准备数据

首先我们使用如下的bulk API接口来把我们所需要的数据导入到Elasticsearch之中:

代码语言:javascript
复制
POST _bulk{ "index" : { "_index" : "twitter", "_id": 1} }{"user":"张三","message":"今儿天气不错啊,出去转转去","uid":2,"city":"北京","province":"北京","country":"中国","address":"中国北京市海淀区","location":{"lat":"39.970718","lon":"116.325747"}, "DOB":"1980-12-01"}{ "index" : { "_index" : "twitter", "_id": 2 }}{"user":"老刘","message":"出发,下一站云南!","uid":3,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区台基厂三条3号","location":{"lat":"39.904313","lon":"116.412754"}, "DOB":"1981-12-01"}{ "index" : { "_index" : "twitter", "_id": 3} }{"user":"李四","message":"happy birthday!","uid":4,"city":"北京","province":"北京","country":"中国","address":"中国北京市东城区","location":{"lat":"39.893801","lon":"116.408986"}, "DOB":"1982-12-01"}{ "index" : { "_index" : "twitter", "_id": 4} }{"user":"老贾","message":"123,gogogo","uid":5,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区建国门","location":{"lat":"39.718256","lon":"116.367910"}, "DOB":"1983-12-01"}{ "index" : { "_index" : "twitter", "_id": 5} }{"user":"老王","message":"Happy BirthDay My Friend!","uid":6,"city":"北京","province":"北京","country":"中国","address":"中国北京市朝阳区国贸","location":{"lat":"39.918256","lon":"116.467910"}, "DOB":"1984-12-01"}{ "index" : { "_index" : "twitter", "_id": 6} }{"user":"老吴","message":"好友来了都今天我生日,好友来了,什么 birthday happy 就成!","uid":7,"city":"上海","province":"上海","country":"中国","address":"中国上海市闵行区","location":{"lat":"31.175927","lon":"121.383328"}, "DOB":"1985-12-01"}

这样,我们就有6个数据。它们的Id分别是从1到6。

搜索

正常搜索

首先我们来做一个正常的搜索,比如寻找所有在北京的文档:

代码语言:javascript
复制
GET twitter/_search{  "query": {    "match": {      "city.keyword": "北京"    }  }}

那么查询的结果为:

代码语言:javascript
复制
    "hits" : [      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "1",        "_score" : 0.24116206,        "_source" : {          "user" : "张三",          "message" : "今儿天气不错啊,出去转转去",          "uid" : 2,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市海淀区",          "location" : {            "lat" : "39.970718",            "lon" : "116.325747"          },          "DOB" : "1980-12-01"        }      },      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "2",        "_score" : 0.24116206,        "_source" : {          "user" : "老刘",          "message" : "出发,下一站云南!",          "uid" : 3,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市东城区台基厂三条3号",          "location" : {            "lat" : "39.904313",            "lon" : "116.412754"          },          "DOB" : "1981-12-01"        }      },      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "3",        "_score" : 0.24116206,        "_source" : {          "user" : "李四",          "message" : "happy birthday!",          "uid" : 4,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市东城区",          "location" : {            "lat" : "39.893801",            "lon" : "116.408986"          },          "DOB" : "1982-12-01"        }      },      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "4",        "_score" : 0.24116206,        "_source" : {          "user" : "老贾",          "message" : "123,gogogo",          "uid" : 5,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市朝阳区建国门",          "location" : {            "lat" : "39.718256",            "lon" : "116.367910"          },          "DOB" : "1983-12-01"        }      },      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "5",        "_score" : 0.24116206,        "_source" : {          "user" : "老王",          "message" : "Happy BirthDay My Friend!",          "uid" : 6,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市朝阳区国贸",          "location" : {            "lat" : "39.918256",            "lon" : "116.467910"          },          "DOB" : "1984-12-01"        }      }    ]  }

我们可以看出来共有5条数据结果,它们的_id分别是从1到5。

那么现在的问题是:如果我想把_id为4和5的那两个文档排在查询的最前面,那么我们该如何来做呢?答案是我们是使用 pinned query。

Pinned query

我们可以使用如下的方式来进行查询:

代码语言:javascript
复制
GET twitter/_search{  "query": {    "pinned": {      "ids": [        "4",        "5"      ],      "organic": {        "match": {          "city.keyword": "北京"        }      }    }  }}

那么我们再来看一下我们的查询结果:

代码语言:javascript
复制
    "hits" : [      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "4",        "_score" : 1.7014124E38,        "_source" : {          "user" : "老贾",          "message" : "123,gogogo",          "uid" : 5,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市朝阳区建国门",          "location" : {            "lat" : "39.718256",            "lon" : "116.367910"          },          "DOB" : "1983-12-01"        }      },      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "5",        "_score" : 1.7014122E38,        "_source" : {          "user" : "老王",          "message" : "Happy BirthDay My Friend!",          "uid" : 6,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市朝阳区国贸",          "location" : {            "lat" : "39.918256",            "lon" : "116.467910"          },          "DOB" : "1984-12-01"        }      },      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "1",        "_score" : 0.24116206,        "_source" : {          "user" : "张三",          "message" : "今儿天气不错啊,出去转转去",          "uid" : 2,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市海淀区",          "location" : {            "lat" : "39.970718",            "lon" : "116.325747"          },          "DOB" : "1980-12-01"        }      },      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "2",        "_score" : 0.24116206,        "_source" : {          "user" : "老刘",          "message" : "出发,下一站云南!",          "uid" : 3,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市东城区台基厂三条3号",          "location" : {            "lat" : "39.904313",            "lon" : "116.412754"          },          "DOB" : "1981-12-01"        }      },      {        "_index" : "twitter",        "_type" : "_doc",        "_id" : "3",        "_score" : 0.24116206,        "_source" : {          "user" : "李四",          "message" : "happy birthday!",          "uid" : 4,          "city" : "北京",          "province" : "北京",          "country" : "中国",          "address" : "中国北京市东城区",          "location" : {            "lat" : "39.893801",            "lon" : "116.408986"          },          "DOB" : "1982-12-01"        }      }    ]  }

在这一次的查询结果中,我们可以看到_id为4和5的两个文档的排名是排在最前面,它们的分数被提高了。

参考:

【1】 https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-pinned-query.html


最新活动

包含文章发布时段最新活动,前往ES产品介绍页,可查找ES当前活动统一入口

Elasticsearch Service自建迁移特惠政策>>

Elasticsearch Service 新用户特惠狂欢,最低4折首购优惠 >>

Elasticsearch Service 企业首购特惠,助力企业复工复产>>

关注“腾讯云大数据”公众号,技术交流、最新活动、服务专享一站Get~

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 【腾讯云 Elasticsearch Service】高可用,可伸缩,云端全托管。集成X-Pack高级特性,适用日志分析/企业搜索/BI分析等场景
  • 准备数据
  • 搜索
    • 正常搜索
      • Pinned query
      • 最新活动
      相关产品与服务
      Elasticsearch Service
      腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档