当前位置: 首页 > news >正文

ElasticSearch - SpringBoot整合ES之查询所有 match_all

文章目录

        • 1. 数据准备
        • 2. 全量查询 match_all
        • 3. 使用 boost 参数更改 _score

官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/index.html
权威指南:https://www.elastic.co/guide/cn/elasticsearch/guide/current/structured-search.html

1. 数据准备

官方测试数据下载地址:https://download.elastic.co/demos/kibana/gettingstarted/accounts.zip ,数据量很大,我们自己构造数据吧。

PUT /user/_doc/1
{"first_name" : "John","last_name" :  "Smith","age" :        25,"about" :      "I love to go rock climbing","interests": [ "sports"]
}PUT /user/_doc/2
{"first_name" : "zhangsan","last_name" :  "Smith","age" :        19,"about" :      "我是一个安静的人","interests": [ "read" ]
}PUT /user/_doc/3
{"first_name" : "lisi","last_name" :  "Alice","age" :        29,"about" :      "我喜欢规划自己的生活","interests": [ "sports", "read","music" ]
}

2. 全量查询 match_all

最简单的查询,它匹配所有文档,默认给它们一个_score 1.0。

GET /user/_search
{"query": {"match_all": {}}
}

为此对应的java实现如下:

@Slf4j
@Service
public class ElasticSearchImpl {private static final String KNOWLEDGE_INDEX = "knowledge";@Autowiredprivate RestHighLevelClient restHighLevelClient;public void searchUser() throws IOException {SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();searchSourceBuilder.query(matchAllQueryBuilder);SearchRequest searchRequest = new SearchRequest(new String[]{"user"},searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);System.out.println(searchResponse);}
}
{"took" : 21,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 1.0,"hits" : [{"_index" : "user","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports"]}},{"_index" : "user","_type" : "_doc","_id" : "2","_score" : 1.0,"_source" : {"first_name" : "zhangsan","last_name" : "Smith","age" : 19,"about" : "我是一个安静的人","interests" : ["read"]}},{"_index" : "user","_type" : "_doc","_id" : "3","_score" : 1.0,"_source" : {"first_name" : "lisi","last_name" : "Alice","age" : 29,"about" : "我喜欢规划自己的生活","interests" : ["sports","read","music"]}}]}
}

3. 使用 boost 参数更改 _score

默认情况下,Elasticsearch 按相关性分数对匹配的搜索结果进行排序,相关性分数衡量每个文档与查询的匹配程度。相关性分数是一个正浮点数,在 搜索_scoreAPI的元字段中返回。越高 ,文档越相关。

GET /user/_search
{"query": {"match_all": { "boost" : 1.2 }}
}

为此对应的java实现如下:

@Slf4j
@Service
public class ElasticSearchImpl {@Autowiredprivate RestHighLevelClient restHighLevelClient;public void searchUser() throws IOException {SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();matchAllQueryBuilder.boost(1.2f);searchSourceBuilder.query(matchAllQueryBuilder);SearchRequest searchRequest = new SearchRequest(new String[]{"user"},searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);System.out.println(searchResponse);}
}

查询结果:

{"took" : 2,"timed_out" : false,"_shards" : {"total" : 1,"successful" : 1,"skipped" : 0,"failed" : 0},"hits" : {"total" : {"value" : 3,"relation" : "eq"},"max_score" : 1.2,"hits" : [{"_index" : "user","_type" : "_doc","_id" : "1","_score" : 1.2,"_source" : {"first_name" : "John","last_name" : "Smith","age" : 25,"about" : "I love to go rock climbing","interests" : ["sports"]}},{"_index" : "user","_type" : "_doc","_id" : "2","_score" : 1.2,"_source" : {"first_name" : "zhangsan","last_name" : "Smith","age" : 19,"about" : "我是一个安静的人","interests" : ["read"]}},{"_index" : "user","_type" : "_doc","_id" : "3","_score" : 1.2,"_source" : {"first_name" : "lisi","last_name" : "Alice","age" : 29,"about" : "我喜欢规划自己的生活","interests" : ["sports","read","music"]}}]}
}
http://www.lryc.cn/news/26597.html

相关文章:

  • 详谈IIC
  • 【Autoware】采集实验数据bag包并仿真运行
  • 名创优品怎么把创意做成生意?
  • springboot原项目配置文件迁移至nacos
  • 常用的shell脚步操作
  • Java on VS Code 2月更新|JUnit 5 并行测试与 Spring Boot 插件的过滤功能
  • 无线WiFi安全渗透与攻防(三)之Windows扫描wifi和破解WiFi密码
  • Python中的遍历字典的键和值
  • 三天Golang快速入门—结构体
  • 日常算法刷题——力扣704
  • 【服务器数据恢复】VMware虚拟机下的SQL Server数据库数据恢复案例
  • 详解旨在提升EVM底层性能的兼容公链Monad
  • 2023社会工作者证书怎么考 在哪里报名考试
  • 统计学 类别比变量的判断
  • 2.基于Label studio的训练数据标注指南:(智能文档)文档抽取任务、PDF、表格、图片抽取标注等
  • 如何在openKylin操作系统上搭建Qt开发环境
  • T_SQL和SQL的区别
  • 用Python自己写一个分词器,python实现分词功能,隐马尔科夫模型预测问题之维特比算法(Viterbi Algorithm)的Python实现
  • 刷题笔记2 | 977.有序数组的平方 ,209.长度最小的子数组 ,59.螺旋矩阵II ,总结
  • python 支付宝营销活动现金红包开发接入流程-含接口调用加签
  • Python操作Windows
  • Aptos SDK交互笔记(一)
  • 汽车 12V 和 24V 电池输入保护推荐
  • 龙蜥LoongArch架构研发全揭秘,龙芯开辟龙腾计划技术合作新范式
  • 剑指 Offer 16. 数值的整数次方
  • 在苹果电脑 mac 上安装原神(playCover)
  • 数据结构考研习题精选
  • linux常用命令介绍 04 篇——uniq命令使用介绍(Linux重复数据的统计处理)
  • 网站打不开数据库错误等常见问题解决方法
  • 爬虫实战进阶版【1】——某眼专业版实时票房接口破解