05-ElasticSearch高级搜索

*

package com.coolman.hotel.test;

import com.coolman.hotel.pojo.HotelDoc;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.lucene.search.TotalHits;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;

@SpringBootTest
public class FullTextSearchDemo {
    // 注入 RestHighLevelClient对象
    @Autowired
    private RestHighLevelClient restHighLevelClient;
// jackson
private final ObjectMapper objectMapper = new ObjectMapper();

/**
 * 查询所有测试
 */
@Test
public void testMatchAll() throws IOException {
    // 1. 创建一个查询请求对象
    SearchRequest searchRequest = new SearchRequest("hotel");   // 指定索引

    // 2. 添加查询的类型
    MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();
    searchRequest.source().query(matchAllQueryBuilder);     // source就相当于{}
    searchRequest.source().size(100);     // RestAPI默认返回的是10条数据,可以更改size的属性,即可自定义返回的数据量

    // 3. 发出查询的请求,得到响应结果
    SearchResponse response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);

    // 4. 处理响应的结果
    handlerResponse(response);

}

/**
 * 用来处理响应数据(相当于解析返回的JSON数据)
 * @param response
 */
private void handlerResponse(SearchResponse response) throws JsonProcessingException {
    // 1. 得到命中的数量(即总记录数量)
    SearchHits hits = response.getHits();
    long totalCount = hits.getTotalHits().value;// 总记录数
    System.out.println("总记录数量为:" + totalCount);

    // 2. 获取本次查询出来的列表数据
    SearchHit[] hitsArray = hits.getHits();
    for (SearchHit hit : hitsArray) {
        // 得到json字符串
        String json = hit.getSourceAsString();
        // 将json字符串转换为实体类对象
        HotelDoc hotelDoc = objectMapper.readValue(json, HotelDoc.class);
        System.out.println(hotelDoc);
    }
}

} ​~~~

Original: https://www.cnblogs.com/OnlyOnYourself-lzw/p/16570714.html
Author: OnlyOnYourself-Lzw
Title: 05-ElasticSearch高级搜索

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/581571/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

  • 记一次vcenter连接esxi失败问题排查(443端口不通)

    vecenter错误 esxi宿主机重启后,vcenter连接esxi提示超时,使用vmware客户端连接esxi也提示超时,开始介入排查。 故障排查 如何进入命令终端 运行alt…

    Linux 2023年6月14日
    079
  • 学习

    1.1、参考博客 参考的教程如下: Original: https://www.cnblogs.com/agui125/p/16032402.htmlAuthor: 风御之举Tit…

    Linux 2023年6月13日
    0102
  • .NET使用StackExchange.Redis

    csharp;gutter:true; using StackExchange.Redis; using System; using System.Collections.Gene…

    Linux 2023年5月28日
    095
  • C语言之初认识

    1.低级语言可以简明说:非常接近底层语言 2.可移值性:可以嵌入各大系统中 3.简单性:语法简单,语法规则没有太大限制,c语言的代码要求比较随意,一行可以写入多行语句,每条语句写完…

    Linux 2023年6月8日
    088
  • 大数据之Hadoop集群中MapReduce的Join操作

    如下两张输入表格 order表 id pid amount 1001 01 1 1002 02 2 1003 03 3 1004 01 4 1005 02 5 1006 03 6 …

    Linux 2023年6月8日
    099
  • python学习

    目录: 1、课程推荐以及书籍推荐 2、学习记录 2.1:无 1. 实践过程 廖雪峰的官方网站 2. 学习记录 2.1 无: posted @2022-02-12 19:44 风御之…

    Linux 2023年6月13日
    0104
  • RPA跨系统自动生成采购订单

    bash;gutter:true;1、从开发器启动机器人2、RPA登录友采云3、RPA根据筛选条件,导出采购订单4、RPA请并登录NC5、RPA把读取到的数据,逐个录入到NC系统中…

    Linux 2023年6月7日
    0136
  • 多进程知识简单总结

    多进程间的全局变量不共享 一、代码展示 import multiprocessing as mul_p import time egg1 = 1 def write(egg2, q…

    Linux 2023年6月14日
    086
  • python入门基础知识三(列表和元组)

    1. 形式 var = [‘char1′,’char2′,’char3’,…] var = [v…

    Linux 2023年6月7日
    0106
  • 操作系统实战45讲- 02 几行汇编几行C:实现一个最简单的内核

    本节源代码位置https://gitee.com/lmos/cosmos/tree/master/lesson02/HelloOS Hello OS 之前,我们先要搞清楚 Hell…

    Linux 2023年6月7日
    098
  • 文件相关命令

    pwd指令 基本语法:pwd功能:显示当前工作的绝对目录 ls指令 基本语法:ls [选项][目录或者文件]常用选项 -a 显示所有文件及目录 (. 开头的隐藏文件也会列出) -l…

    Linux 2023年6月6日
    084
  • Forms组件;cookie与session

    Forms组件 需求 如果需要编写一个校验用户名和密码是否合法的功能,那么 前端需要自己编写获取用户数据的各种标签 前端需要自己想方设法的展示错误的提示信息 后端需要自己想方设法的…

    Linux 2023年6月7日
    087
  • 解决Conda改源后无法安装软件包的问题

    前言 有时候哪怕修改了 Conda 源也一直无法安装一个想要的软件包,亦或者找到了目标软件包,下载速度却很慢,速度感人,也可能直接 Conda 就找不到你想安装的软件包 此时有两种…

    Linux 2023年6月14日
    090
  • uniapp封装request请求,常用公共函数等,非常实用

    1、项目目录如下图,utils目录下的文件和main.js文件在下面可以直接下载使用 2、demo案例,前端代码 php;gutter:true; export default {…

    Linux 2023年6月7日
    0105
  • Docker私有仓库部署

    修改daemon.json 重启docker服务 镜像上传至私有仓库 标记此镜像为私有仓库的镜像 再次启动私服容器 Original: https://www.cnblogs.co…

    Linux 2023年6月8日
    073
  • CentOS 文本编辑器

    Linux 终端的文本编辑器中,较著名的有:Nano、Vim、Emacs。其它文本编辑器还有 Gedit、Sublime,Atom 等等。 1.1、基础命令 nano:打开 nan…

    Linux 2023年6月8日
    0114
亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球