提高文档的相关性得分,使其更接近提供的原始日期或地点。 例如,你可以使用此查询为更接近某个日期或位置的文档赋予更大的权重。

你可以使用 distance_feature 查询查找与某个位置最近的邻居。 你还可以在布尔搜索的 “should” 过滤器中使用查询,以将增强的相关性得分添加到布尔查询的得分中。

下面我们用一个具体的例子来展示这个 API 的使用。

准备数据

我们还是拿之前我们的文章 “Elasticsearch: 运用 Field collapsing 来减少基于单个字段的搜索结果” 中的索引来做例子。在那个例子里,我们把数据导入到 Elasticsearch 中。我们可以查看一下它的 mapping:

GET best_games/_mapping
{"best_games" : {"mappings" : {"_meta" : {"created_by" : "ml-file-data-visualizer"},"properties" : {"critic_score" : {"type" : "long"},"developer" : {"type" : "text"},"genre" : {"type" : "keyword"},"global_sales" : {"type" : "double"},"id" : {"type" : "keyword"},"image_url" : {"type" : "keyword"},"name" : {"type" : "text"},"platform" : {"type" : "keyword"},"publisher" : {"type" : "keyword"},"user_score" : {"type" : "long"},"year" : {"type" : "long"}}}}
}

我们从上面可以看出来,上面的 year 显示的是 long 类型的数据。显然这个不是我们所需要的。我们希望它是 date 类型的数据。我们需要重新对我们的数据进行 reindex。对于不很熟悉 reindex 的开发者来说,你可以参照我之前的文章 “Elasticsearch: Reindex 接口”。

我们来重新定义一个新的叫做 best_games 的索引:

PUT best_games1
{"mappings": {"properties": {"critic_score": {"type": "long"},"developer": {"type": "text"},"genre": {"type": "keyword"},"global_sales": {"type": "double"},"id": {"type": "keyword"},"image_url": {"type": "keyword"},"name": {"type": "text"},"platform": {"type": "keyword"},"publisher": {"type": "keyword"},"user_score": {"type": "long"},"year": {"type": "date","format": "strict_year"}}}
}

在上面的 mapping 里,我们重新定义了 year 为 date 类型。那么我们可以通过如下的命令来 reindex 我们的 best_games1 索引:

POST _reindex
{"source": {"index": "best_games"},"dest": {"index": "best_games1"}
}

操作完上面的命令后,我们可以重新查看一下我的 best_games1 索引的文档数量:

GET best_games1/_count

显示结果为:

{"count" : 500,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0}
}

这样我们在 best_games1 中已经存在我们想要的索引数据了。

distance_feature 查询

接下来,我们开始做一些查询。比如我们查询一下 critical_score 大于90的所有文档:

GET /best_games1/_search
{"query": {"bool": {"filter": {"range": {"critic_score": {"gte": 90}}}}}
}

显示的结果为:

    "hits" : [{"_index" : "best_games1","_type" : "_doc","_id" : "hnLfF28BjrINWI3xWOLh","_score" : 0.0,"_source" : {"id" : "mario-kart-ds-ds-2005","name" : "Mario Kart DS","year" : 2005,"platform" : "DS","genre" : "Racing","publisher" : "Nintendo","global_sales" : 23.21,"critic_score" : 91,"user_score" : 8,"developer" : "Nintendo","image_url" : "https://upload.wikimedia.org/wikipedia/en/thumb/a/ad/Mario_Kart_DS_screenshot.png/220px-Mario_Kart_DS_screenshot.png"}},{"_index" : "best_games1","_type" : "_doc","_id" : "inLfF28BjrINWI3xWOLh","_score" : 0.0,"_source" : {"id" : "grand-theft-auto-v-ps3-2013","name" : "Grand Theft Auto V","year" : 2013,"platform" : "PS3","genre" : "Action","publisher" : "Take-Two Interactive","global_sales" : 21.04,"critic_score" : 97,"user_score" : 8,"developer" : "Rockstar North","image_url" : "https://pmcvariety.files.wordpress.com/2013/09/gta-v-big.jpg?w=1000&h=563&crop=1"}},{"_index" : "best_games1","_type" : "_doc","_id" : "i3LfF28BjrINWI3xWOLh","_score" : 0.0,"_source" : {"id" : "grand-theft-auto-san-andreas-ps2-2004","name" : "Grand Theft Auto: San Andreas","year" : 2004,"platform" : "PS2","genre" : "Action","publisher" : "Take-Two Interactive","global_sales" : 20.81,"critic_score" : 95,"user_score" : 9,"developer" : "Rockstar North","image_url" : "http://4.bp.blogspot.com/-IITyrVJdS50/Udvw7XLG-oI/AAAAAAAASwY/H1j2GYBjXng/s1600/GTA+SA+0.jpg"}},...

显然这个结果里有一些文档的年代非常久远,可能并不是我们想要的结果。那么我改如何把那些靠近我们的年代的文档排名到前面呢?答案是使用 distance_feature 查询。

我们使用如下的方法来进行查询:

GET /best_games1/_search
{"query": {"bool": {"filter": {"range": {"critic_score": {"gte": 90}}},"should": {"distance_feature": {"field": "year","pivot": "2555d","origin": "now"}}}}
}

我们通过 distance_feature 的引入,上面定义了从现在开始到7年之间(2555天)所有文档的分数都将被提高,同时超过7年之上的文档都只享受一半的提高。那么这样的搜索的结果是:

    "hits" : [{"_index" : "best_games1","_type" : "_doc","_id" : "-XLfF28BjrINWI3xWOLi","_score" : 0.63837445,"_source" : {"id" : "uncharted-4-a-thiefs-end-ps4-2016","name" : "Uncharted 4: A Thief's End","year" : 2016,"platform" : "PS4","genre" : "Shooter","publisher" : "Sony Computer Entertainment","global_sales" : 5.38,"critic_score" : 93,"user_score" : 7,"developer" : "Naughty Dog","image_url" : "https://i.ytimg.com/vi/hh5HV4iic1Y/maxresdefault.jpg"}},{"_index" : "best_games1","_type" : "_doc","_id" : "T3LfF28BjrINWI3xWOPi","_score" : 0.5850225,"_source" : {"id" : "the-witcher-3-wild-hunt-ps4-2015","name" : "The Witcher 3: Wild Hunt","year" : 2015,"platform" : "PS4","genre" : "Role-Playing","publisher" : "Namco Bandai Games","global_sales" : 3.97,"critic_score" : 92,"user_score" : 9,"developer" : "CD Projekt Red Studio","image_url" : "https://www.godisageek.com/wp-content/uploads/the-witcher-3-monster-1024x576.jpg"}},{"_index" : "best_games1","_type" : "_doc","_id" : "inLfF28BjrINWI3xWOPi","_score" : 0.5850225,"_source" : {"id" : "metal-gear-solid-v-the-phantom-pain-ps4-2015","name" : "Metal Gear Solid V: The Phantom Pain","year" : 2015,"platform" : "PS4","genre" : "Action","publisher" : "Konami Digital Entertainment","global_sales" : 3.41,"critic_score" : 93,"user_score" : 8,"developer" : "Kojima Productions, Moby Dick Studio","image_url" : "https://i.ytimg.com/vi/gtgNUFSoHv8/maxresdefault.jpg"}},...

从上面的显示结果可以看出来,靠近最近年份的文档最先出现,而且得分较高。

同样地,我们也可以基于位置对文档进行提高。以下布尔搜索将返回名称为 chocolate 的文档。 该搜索还使用 distance_feature 查询来增加位置值接近 [-71.3,41.15] 的文档的相关性得分。

GET /items/_search
{"query": {"bool": {"must": {"match": {"name": "chocolate"}},"should": {"distance_feature": {"field": "location","pivot": "1000m","origin": [-71.3, 41.15]}}}}
}

参考:

【1】Distance feature query | Elasticsearch Guide [7.16] | Elastic

Elasticsearch:运用 distance feature 查询来增强相关性相关推荐

  1. 【Elasticsearch】改进布尔查询的搜索相关性

    1.概述 翻译:Improving search relevance with boolean queries 有人翻译:Elasticsearch:使用布尔查询提高搜索的相关性 当你在Elastic ...

  2. Elasticsearch(es) 查询语句语法详解

    Elasticsearch 查询语句采用基于 RESTful 风格的接口封装成 JSON 格式的对象,称之为 Query DSL.Elasticsearch 查询分类大致分为全文查询.词项查询.复合查 ...

  3. 初识ElasticSearch(2) -文档查询之match查询 | 分词器

    1. 分词器: 2. match查询: 2.1. 数据准备 - 创建带分词器的索引映射 2.2. 数据准备 - 添加文档 2.3. 数据准备 - 查看文本分词 2.4. 查询 - 映射有分词器的字段查 ...

  4. 跟乐乐学ES!(三)ElasticSearch 批量操作与高级查询

    上一篇文章:跟乐乐学ES!(二)ElasticSearch基础. 下一篇文章:跟乐乐学ES!(四) java中ElasticSearch客户端的使用. 批量操作 有些增删改查操作是可以进行批量操作的, ...

  5. java操作es聚合操作并显示其他字段_java使用elasticsearch分组进行聚合查询过程解析...

    这篇文章主要介绍了java使用elasticsearch分组进行聚合查询过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 java连接elas ...

  6. 【Elasticsearch】ElasticSearch里面的偏好查询

    1.概述 转载:ElasticSearch里面的偏好查询 在es查询的时候我们可以控制Preference,来完成特定shard或节点上的数据查询,默认情况下查询是随机的. 假如现在我们有一份索引5个 ...

  7. 【Elasticsearch】Elasticsearch Span Query跨度查询

    1.概述 转载:Elasticsearch Span Query跨度查询 ES基于Lucene开发,因此也继承了Lucene的一些多样化的查询,比如本篇说的Span Query跨度查询,就是基于Luc ...

  8. ElasticSearch使用(嵌套查询、嵌套高亮)

    ElasticSearch使用(嵌套查询.嵌套高亮) 嵌套查询 bool 查询 must.should关系 1.只有must 2.只有should 3.must和should同时存在 4.怎样设置sh ...

  9. SpringBoot+Mybatis+Elasticsearch 实现模糊分页查询并标记关键字

    SpringBoot 整合 Elasticsearch 实现模糊分页查询并标记关键字 一.概述 & 介绍 Elasticsearch 是基于 Lucense 技术的搜索引擎(服务器),将数据进 ...

最新文章

  1. 操作系统 作业调度实验报告
  2. HyperLogLog 算法的原理讲解以及 Redis 是如何应用它的
  3. 危害网站关键词优化的因素如何避免?
  4. python3.5怎么使用-Python3.5常见内置方法参数用法实例详解
  5. 牛客竞赛语法入门班数组字符串习题【完结】
  6. 米斯特白帽培训讲义 漏洞篇 文件上传
  7. 2021九江一中高考成绩查询系统,九江一中2018高考成绩
  8. netty 多线程微盘_Netty多线程处理机制
  9. 编译OpenJDK8:CoreLibraries.gmk:37/e_acos.obj/BuildJdk.gmk:70/Main.gmk:116/错误
  10. crm系统需要的服务器,灵当CRM管理系统运行环境-CRM服务器配置
  11. 计算机描绘的基因结构图,傻瓜式图文教程:Genecards基因信息检索与分析、基因结构图绘制.........
  12. dnf时装补丁教程_DNF时装补丁修改教程
  13. 【三维CAD设计经验分享】CrownCAD设计:生成工程图
  14. k8s之滚动更新、金丝雀发布、蓝绿发布
  15. linux广播命令,分享|在 Linux 终端收听广播
  16. [Realtek sdk-4.4.x ]RTL8198D+RTL8192F+RTL8812F WiFi 信道、频宽、加密方式、SSID设置(WPA/WPA/WPA3加密方式)
  17. Android build.gradle配置详解
  18. 飞利浦dicom_如何在飞利浦色相系统中添加第三方智能灯泡
  19. ffmpeg 设置关键帧
  20. 阿里云服务器ECS接入多IP_ECS绑定多个弹性公网IP搭建

热门文章

  1. python开发阿里大于短信
  2. win10错误代码0x80070005解决方法
  3. MPLS LDP:标签分发协议
  4. Linux 输出重定向 命令 > file
  5. 什么是XML语言解析(一)
  6. 值得一读的20个经典故事
  7. 微信小程序商品分享海报
  8. 从乙方到甲方,我在做什么
  9. 微信小程序版学生管理系统演示(一)
  10. Nacos 1.4.1 紧急升级修复Alibaba Nacos 认证绕过漏洞