当前位置: 首页 > 编程日记 > 正文

SpringBoot 操作elasticsearch

SpringBoot 操作elasticsearch

版本环境

  • jdk1.8
  • elasticsearch 7.6.1

maven

 <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>

image-20200718170100737

注意版本,如果版本不对 需要手动进行版本指定

<properties><java.version>1.8</java.version><elasticsearch.version>7.6.1</elasticsearch.version></properties>

image-20200718170246067

创建配置类

package com.oss.oss.config;import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;/*** 声明其为一个配置对象* 生成一个连接elasticsearch 的连接对象 注入到spring容器中*/
@Configuration
public class ElasticsearchClientConfiguration {@Beanpublic RestHighLevelClient restHighLevelClient(){RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));return client;}
}

创建测试方法

api 说明文档地址

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-document-exists.html

image-20200718231903392

package com.oss.oss;import com.alibaba.fastjson.JSON;
import com.oss.oss.bean.User;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.test.context.SpringBootTest;import java.io.IOException;@SpringBootTest
class OssApplicationTests {@Autowired@Qualifier("restHighLevelClient")private RestHighLevelClient client;//测试创建索引@Testpublic void createIndex(){try {CreateIndexRequest request = new CreateIndexRequest("test04");CreateIndexResponse createIndexResponse =client.indices().create(request, RequestOptions.DEFAULT);System.out.println(createIndexResponse);} catch (IOException e) {e.printStackTrace();}}/*** 判断索引是否存在*/@Testpublic void existsIndex(){try {GetIndexRequest request = new GetIndexRequest("test04");boolean exists = client.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists);} catch (IOException e) {e.printStackTrace();}}/*** 删除索引*/@Testpublic void deleteIndex(){try {DeleteIndexRequest request = new DeleteIndexRequest("test04");AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT);System.out.println(deleteIndexResponse.isAcknowledged());} catch (IOException e) {e.printStackTrace();}}/*** 文档新增*/@Testpublic void createDocument(){try{IndexRequest request = new IndexRequest("test03");User User = new User("李白","诗仙李白",56);request.id("4");request.timeout("1s");request.source(JSON.toJSONString(User), XContentType.JSON);IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);System.out.println(indexResponse.toString());System.out.println(indexResponse.status());} catch (Exception e){e.printStackTrace();}}/*** 文档是否存在*/@Testpublic void existDocument(){try {GetRequest getRequest = new GetRequest("test03","4");getRequest.fetchSourceContext(new FetchSourceContext(false));getRequest.storedFields("_none_");boolean exis = client.exists(getRequest,RequestOptions.DEFAULT);System.out.println(exis);} catch (IOException e) {e.printStackTrace();}}/*** 查询文档*/@Testpublic void searchDocument(){try {GetRequest getRequest = new GetRequest("test03","4");GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);System.out.println(getResponse.getSourceAsString());System.out.println(getResponse);} catch (IOException e) {e.printStackTrace();}}/*** 条件查询**/@Testpublic void mulitSearchDocument(){try {SearchRequest searchRequest = new SearchRequest("test03");SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(QueryBuilders.matchQuery("name","李白"));searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);System.out.println(JSON.toJSONString(searchResponse.getHits()));System.out.println("=================================");for (SearchHit documentFields : searchResponse.getHits().getHits()){System.out.println(documentFields.getSourceAsMap());}} catch (IOException e) {e.printStackTrace();}}
}

相关文章:

替换WCF默认序列化方式

创建类 : using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ServiceModel.Description; using System.Xml; using System.Runtime.Serialization; namespace Kingge.Mini.Network { public class NetDataContra…

Linux下vi编辑器命令精华版

最近开始使用vi编辑器&#xff0c;用了几天&#xff0c;发现其实还是比较好用的。对自己常用的命令做个总结&#xff0c;以备实时查阅。一下内容是对网络多篇文章的总结。进入vi的命令&#xff1a;vi filename :打开或新建文件&#xff0c;并将光标置于第一行首 vi filename &…

测试用例评审关注点

测试用例评审关注点 1、用例设计是否清晰、合理、简洁&#xff1b; 2、用例是否高效对需求进行覆盖&#xff0c;是否覆盖测试需求上的所有功能点&#xff1b; 3、优先级是否合理&#xff1b; 4、用例是否有很好的可执行性&#xff08;例如用例的“执行步骤”、“期待结果”是否…

命令行收集(DOS/Linux/nc/xscan/xsniffer)

#1 一&#xff1a; net use \\ip\ipc$ " " /user:" " 建立IPC空链接 net use \\ip\ipc$ "密码" /user:"用户名" 建立IPC非空链接 net use h: \\ip\c$ "密码" /user:"用户名" 直接登陆后映射对方C&#xff1a;到…

偏前端 - vue-cli(axios请求数据==》token+按接口参数顺序(参数值拼接base64)- MD5)...

token按接口参数顺序&#xff08;参数值拼接base64&#xff09;-> MD5) 请教于“喵咪”&#xff0c;再此特别鸣谢&#xff01;~ 特别强调&#xff1a;import qs from qs; 这个内部方法一定要用哦。参数签名base64md5都成功的情况华。接口还是爆500&#xff0c;就是和这个有关…

Docker 搭建elasticsearch 7.6.x集群

Docker 搭建elasticsearch 7.6.x集群 文章目录拉取镜像设置Elasticsearch挂载目录编写elasticsearch.yml配置文件node-1node-2node-3创建镜像验证是否搭建成功问题max virtual memory areas vm.max_map_count [65530] is too low, increase to at least参考拉取镜像 docker pu…

乐嘉性格色彩分析测试题

乐嘉<性格色彩>分析测试题 说明&#xff1a;最符合你的句子用红色作标记 1. 关于人生观&#xff0c;我的内心其实是&#xff1a; A 希望能够有尽量多的人生体验&#xff0c;所以会有非常多样化的想法。 B在小心合理的基础上&#xff0c;谨慎地确定自己的目标&#xff…

OAuth_1

OAuth2.0是一个应用之间彼此访问数据的开源授权协议。比如&#xff0c;一个游戏应用可以 访问Facebook的用户数据。用户访问web游戏应用&#xff0c;该游戏应用要求用户通过Facebook 登录。用户登录到Facebook&#xff0c;再重定向回游戏应用&#xff0c;游戏应用就可以访问用户…

ubuntu 使用阿里云 apt 源

以下内容来自 https://opsx.alibaba.com/mirror Ubuntu对应的“帮助”信息 修改方式&#xff1a;打开 /et/apt/sources.list将http://archive.ubuntu.com/替换为mirrors.aliyun.com即可 PS&#xff1a;网络上的信息&#xff0c;一定得注意时效性。以下内容&#xff0c;均为此时…

ecos 编译时无法找到 tclConfig.sh 和 tkConfig.sh

这是因为 tcl-devel tk-devel 一般系统中默认是不安装的&#xff0c;至少cent-os 5.5 和fedora 11是这样的&#xff0c;安装这两个包即可。 # yum install tcl-devel tk-devel 补记&#xff1a; ubuntu 10.04.2 上为了便于多个版本的tcl的存在&#xff0c;tcl被安装的位置不太一…

从消息处理角度看应用程序与windows的关系(图示)

转载于:https://blog.51cto.com/gzkhrh/338533

002:用Python设计第一个游戏

笔记 什么是BIF&#xff1f; 答&#xff1a;BIF 即 Built-in Functions&#xff0c;内置函数。为了方便快速编写脚本程序&#xff0c;Python 提供了非常丰富的内置函数&#xff0c;我们只需要直接调用即可&#xff0c;例如 print() 的功能是“打印到屏幕”&#xff0c;input() …

哑谜,回文和暴力之美

暴力搜索是一个有趣的东西。至少刘汝佳是这么认为的。编程之美的4.10节就是典型的暴力题。虽然作者将其难度定义为一颗星&#xff0c;但却不能因此认为这个类型的问题就是那么容易的&#xff0c;很多可能需要一些有创造力的想法。 不妨试试下面这几道题&#xff1a; Island of …

身份证敏感信息处理 图片添加蒙版

实现效果 需要的jar包 <!-- https://mvnrepository.com/artifact/com.jhlabs/filters --><dependency><groupId>com.jhlabs</groupId><artifactId>filters</artifactId><version>2.0.235-1</version></dependency> 调…

Linux bash管道符“|”使用介绍与例子

https://blog.csdn.net/wangqianyilynn/article/details/75576815转载于:https://www.cnblogs.com/dhName/p/10967718.html

PostgreSQL第一步:安装

PostgreSQL是一个自由的对象-关系数据库服务器&#xff08;数据库管理系统&#xff09;&#xff0c;它在灵活的BSD-风格许可证下发行。它在其他开放源代码数据库系统&#xff08;比如MySQL和Firebird&#xff09;&#xff0c;和专有系统比如Oracle、Sybase、IBM的DB2和Microsof…

【Henu ACM Round#15 A】 A and B and Chess

【链接】 我是链接,点我呀:) 【题意】 在这里输入题意 【题解】 统计大写和小写的个数。 比较答案。输出即可。 【代码】 #include <bits/stdc.h> using namespace std;string s[10]; map<char,int> dic; int inc[300];int main() {for (int i 0;i < 8;i)cin…

[转载] static class 静态类(Java)

一般情况下是不可以用static修饰类的。如果一定要用static修饰类的话&#xff0c;通常static修饰的是匿名内部类。   在一个类中创建另外一个类&#xff0c;叫做成员内部类。这个成员内部类可以静态的&#xff08;利用static关键字修饰&#xff09;&#xff0c;也可以是非静态…

tomcat监控-psi-probe使用

什么是psi-probe 这是一款 Tomcat 管理和监控工具&#xff0c;前身是 Lambda Probe。由于 Lambda Probe 2006不再更新&#xff0c;所以 PSI Probe 算是对其的一个 Fork 版本并一直更新至今。 下载 下载地址&#xff1a;https://github.com/psi-probe/psi-probe 百度网盘地址…

配置文件app.config

无论对于客户端程序还是web应用程序&#xff0c;配置文件的作用不言而喻&#xff0c;现总结用法如下&#xff1a; 1. 创建配置节类 必须创建继承自ConfigurationSection的对象才能进行配置数据读写操作&#xff0c;ConfigurationSection提供了索引器用来获取和设置配置数据&…

windows远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可

远程桌面如果超出最大连接数, 使用命令行mstsc /console登录即可。 &#xff08;也可以用 mstsc /admin&#xff09; 可以在运行里使用mstsc /console /v:IP:远程端口即可强制登录; 如果直接在远程桌面连接端使用就直接输入/console /v:IP:远程端口. 如&#xff1a;mstsc /cons…

AppiumForWin安装

尝试安装Windows版本的Appium参考&#xff1a;http://www.cnblogs.com/fnng/p/4540731.html第一步&#xff1a;安装nodehttps://nodejs.org/en/安装成功后使用&#xff1a;node -v&#xff0c;进行验证第二步&#xff1a;安装Appium下面的方法失败&#xff1a;原因下载不成功&a…

prometheus--初见

什么是prometheus Prometheus&#xff08;普罗米修斯&#xff09;是一个开源系统监控和警报工具&#xff0c;最初是在SoundCloud建立的。自2012年成立以来&#xff0c;许多公司和组织都采用了普罗米修斯&#xff0c;该项目拥有一个非常活跃的开发者和用户社区。它现在是一个独立…

JavaScript时间日期格式化

/** 时间对象的格式化;*/ Date.prototype.format function(format) {/** eg:format"YYYY-MM-dd hh:mm:ss";*/var o {"M" :this.getMonth() 1, // month"d" :this.getDate(), // day"h" :this.getHours(), // hour"m" :th…

Hello World

作为所有编程语言的起始阶段&#xff0c;HELLO WORLD占据着无法改变的地位&#xff0c;所有中/英/法/德/美……版本的编程教材中&#xff0c;HELLO WORLD总是作为第一个TEST记录于书本之中&#xff0c;所有的编程第一步就在于此了&#xff01;经典之中的经典&#xff01;HELLO …

POJ 1144 Network (求割点)

题意&#xff1a; 给定一幅无向图&#xff0c; 求出图的割点。 割点模板&#xff1a;http://www.cnblogs.com/Jadon97/p/8328750.html 分析&#xff1a; 输入有点麻烦&#xff0c; 用stringsteam 会比较简单 #include<cstdio> #include<iostream> #include<queu…

mongoose简单使用

介绍&安装 官网&#xff1a;http://www.mongoosejs.net/ npm i -S mongoose 使用 1.连接mongodb&创建模型 var mongoose require(mongoose)​//1、连接mongodb mongoose.connect(mongodb://localhost/test)​//2、设置文档结构var userSchema new mongoose.Schema…

Codeforces Round #563 (Div. 2)/CF1174

Codeforces Round #563 (Div. 2)/CF1174 CF1174A Ehab Fails to Be Thanos 其实就是要\(\sum\limits_{i1}^n a_i\)与\(\sum\limits_{n1}^{2n}a_i\)差值最大&#xff0c;排一下序就好了 CF1174B Ehab Is an Odd Person 一个显然的结论就是如果至少有一个奇数和一个偶数&#xff…

Enterprise Architect 中文经典教程

一、Enterprise Architect简介Enterprise Architect是一个对于软件系统开发有着极好支持的CASE软件&#xff08;Computer Aided Software Engineering&#xff09;。EA不同于普通的UML画图工具&#xff08;如VISIO&#xff09;&#xff0c;它将支撑系统开发的全过程。在需求分析…

[WebDev]Web 开发与设计师速查手册大全

Cheat Sheet 一词在中文中并没有很贴切的对译&#xff0c;大概是考试作弊条一类的东西&#xff0c;这要求 Cheat Sheet 必须短小精悍又覆盖广泛&#xff0c;作为 Web 开发与设计师&#xff0c;免不了在工作时查询大量资料&#xff0c;某个 Web 色值&#xff0c;某个 JavaScript…