1.Elasticsearch6中RestHighLevelClient和RestLowLevelClient
图片.png
1.1pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zy</groupId>
<artifactId>spring-elasticsearch-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring.version>5.1.3.RELEASE</spring.version>
<jackson.core.version>2.9.8</jackson.core.version>
<elasticsearch.version>6.5.4</elasticsearch.version>
<fastjson.version>1.2.54</fastjson.version>
<lombok.version>1.16.20</lombok.version>
</properties>
<dependencies>
<!-- spring全家桶系列 -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- server-->
<!-- https://mvnrepository.com/artifact/org.elasticsearch/elasticsearch -->
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!-- rest-high-level-client -->
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>${elasticsearch.version}</version>
</dependency>
<!-- json转换 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.core.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.core.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>${lombok.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-core -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
</dependency>
</dependencies>
<build>
<finalName>spring-springmvc</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
1.2web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>spring-elasticsearch-demo</display-name>
<!-- 配置spring主配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置springmvc-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--配置初始化参数-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--配置启动的优先级-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
1.3spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--开启自动扫包-->
<context:component-scan base-package="com.zy.controller"/>
<!--开启注解驱动:用来替换处理器适配器和处理器映射器-->
<mvc:annotation-driven/>
</beans>
1.4applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:component-scan base-package="com.zy.config, com.zy.utils"/>
</beans>
1.5配置类
package com.zy.config;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
@Configuration
public class ESConfigBean {
private String host = "127.0.0.1";
private int port = 9200;
private String schema = "http";
private int connectTimeOut = 1000;
private int socketTimeOut = 30000;
private int connectionRequestTimeOut = 500;
private int maxConnectNum = 100;
private int maxConnectPerRoute = 100;
private HttpHost httpHost;
private boolean uniqueConnectTimeConfig = true;
private boolean uniqueConnectNumConfig = true;
private RestClientBuilder builder;
private RestHighLevelClient client;
/**
* Bean name default 函数名字
*
* @return
*/
@Bean(name = "restHighLevelClient")
public RestHighLevelClient restHighLevelClient() {
httpHost= new HttpHost(host, port, schema);
builder = RestClient.builder(httpHost);
if (uniqueConnectTimeConfig) {
setConnectTimeOutConfig();
}
if (uniqueConnectNumConfig) {
setMutiConnectConfig();
}
client = new RestHighLevelClient(builder);
return client;
}
/**
* 异步httpclient的连接延时配置
*/
public void setConnectTimeOutConfig() {
builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
requestConfigBuilder.setConnectTimeout(connectTimeOut);
requestConfigBuilder.setSocketTimeout(socketTimeOut);
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
return requestConfigBuilder;
}
});
}
/**
* 异步httpclient的连接数配置
*/
public void setMutiConnectConfig() {
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setMaxConnTotal(maxConnectNum);
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
return httpClientBuilder;
}
});
}
/**
* 关闭连接
*/
public void close() {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
1.6工具类
package com.zy.utils;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.*;
import org.elasticsearch.common.xcontent.XContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.io.IOException;
@Component
public class ElasticSearchUtil {
private static final Logger LOGGER = LoggerFactory.getLogger(ElasticSearchUtil.class);
@Autowired
private RestHighLevelClient restHighLevelClient;
private static RestHighLevelClient client;
@PostConstruct
public void init() {
client = this.restHighLevelClient;
}
private static ObjectMapper mapper = new ObjectMapper();
/**
* 创建索引
* @param index
* @param requestOptions
* @return
*/
public static boolean createIndex(String index, RequestOptions requestOptions) {
CreateIndexRequest request = new CreateIndexRequest(index);
boolean flag;
try {
CreateIndexResponse indexResponse = client.indices().create(request, requestOptions);
flag = indexResponse.isAcknowledged();
if (flag) {
LOGGER.info("创建索引成功!");
}
else {
LOGGER.info("创建索引失败!");
}
} catch (IOException e) {
LOGGER.error("failed to create index: {}.", index, e);
flag = false;
}
return flag;
}
/**
* 新增索引
* @param index
* @param document
* @param id
* @param object
* @param requestOptions
* @return
*/
public static String add(String index, String document, String id, JSONObject object, RequestOptions requestOptions) {
IndexRequest indexRequest = new IndexRequest(index, document, id);
try {
indexRequest.source(mapper.writeValueAsString(object), XContentType.JSON);
IndexResponse indexResponse = client.index(indexRequest, requestOptions);
return indexResponse.getId();
} catch (IOException e) {
LOGGER.error("failed to add index: {}, document: {}, id: {}.", index, document, id, e);
}
return null;
}
/**
* 检查某索引是否存在:同步方法
* @param request
* @return
*/
public static boolean checkIndexExist(Request request) {
try {
Response response = client.getLowLevelClient().performRequest(request);
return response.getStatusLine().getReasonPhrase().equals("OK");
} catch (IOException e) {
LOGGER.error("index is not exist, request is {}.", request, e);
}
return false;
}
/**
* 获取低水平客户端
* @return
*/
public static RestClient getLowLevelClient() {
return client.getLowLevelClient();
}
}
1.7controller层
package com.zy.controller;
import com.alibaba.fastjson.JSONObject;
import com.zy.utils.ElasticSearchUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.Request;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@RestController
@RequestMapping("/es/")
public class ESController {
private static final Logger LOGGER = LoggerFactory.getLogger(ESController.class);
/**
* 参考链接
* https://blog.csdn.net/chy2z/article/details/81703012
*/
/**
*
* @return
*/
@GetMapping("index")
@ResponseBody
public Object indexPage() {
return "hello world";
}
/**
* http://localhost:8080/es/createIndex
* 创建索引
*
* @param request
* @param response
* @param index
* @return
*/
@RequestMapping("/createIndex")
@ResponseBody
public String createIndex(HttpServletRequest request, HttpServletResponse response, String index) {
Request request1 = new Request("HEAD", index);
if (!ElasticSearchUtil.checkIndexExist(request1)) {
if (ElasticSearchUtil.createIndex(request1.getEndpoint(), RequestOptions.DEFAULT)) {
return "索引创建成功";
} else {
return "索引已经失败";
}
} else {
return "索引已经存在";
}
}
/**
* 插入记录
* http://localhost:8080/es/add
*
* @return
*/
@RequestMapping("/add")
@ResponseBody
public String addData() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("id", DateTimeFormatter.ofPattern("yyyyMMdd").format(LocalDateTime.now()));
jsonObject.put("age", 30);
jsonObject.put("name", "tom");
jsonObject.put("date", LocalDateTime.now());
String id = ElasticSearchUtil.add("hello",
"hello_type",
jsonObject.getString("id"),
jsonObject,
RequestOptions.DEFAULT);
if (StringUtils.isNotBlank(id)) {
return "success";
} else {
return "failure";
}
}
/**
* 查询所有
*
* @return
*/
@GetMapping("queryAll")
@ResponseBody
public String queryAll() {
HttpEntity entity = new NStringEntity(
"{ \"query\": { \"match_all\": {}}}",
ContentType.APPLICATION_JSON);
String endPoint = "/hello/hello_type/_search";
try {
Request request = new Request("POST", endPoint);
request.setEntity(entity);
Response response = ElasticSearchUtil.getLowLevelClient().performRequest(request);
return EntityUtils.toString(response.getEntity());
} catch (IOException e) {
LOGGER.error("failed to query", e);
}
return "failed to queryAll";
}
/**
* 根据条件查询
*
* @return
*/
@GetMapping("/queryByMatch")
@ResponseBody
public String queryByMatch() {
try {
String endPoint = "/hello/hello_type/_search";
IndexRequest indexRequest = new IndexRequest();
XContentBuilder builder;
try {
builder = JsonXContent.contentBuilder().
startObject().
startObject("query").
startObject("match").
field("name.keyword", "jerry").
// field("name", "jerry").
endObject().
endObject().
endObject();
indexRequest.source(builder);
} catch (IOException e) {
LOGGER.error("failed to query,", e);
}
String source = indexRequest.source().utf8ToString();
LOGGER.info("source---->" + source);
HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
Request request = new Request("post", endPoint);
request.setEntity(entity);
Response response = ElasticSearchUtil.getLowLevelClient().performRequest(request);
return EntityUtils.toString(response.getEntity());
} catch (IOException e) {
LOGGER.error("failed to query, name: {}.", "jerry", e);
}
return "failed to query";
}
/**
* 复合查询
*
* @return
*/
@RequestMapping("/queryByCompound")
@ResponseBody
public String queryByCompound() {
try {
String endPoint = "/hello/hello_type/_search";
IndexRequest indexRequest = new IndexRequest();
XContentBuilder builder;
try {
builder = JsonXContent.contentBuilder().
startObject().
startObject("query").
startObject("bool").
startObject("must").
startObject("match").
field("name.keyword", "tom").
endObject().
endObject().
startObject("filter").
startObject("range").
startObject("age").
field("gte", "10").
field("lte", "50").
endObject().
endObject().
endObject().
endObject().
endObject().
endObject();
indexRequest.source(builder);
} catch (IOException e) {
LOGGER.error("failed to query", e);
}
String source = indexRequest.source().utf8ToString();
LOGGER.info("source---->" + source);
HttpEntity entity = new NStringEntity(source, ContentType.APPLICATION_JSON);
Request request = new Request("post", endPoint);
request.setEntity(entity);
Response response = ElasticSearchUtil.getLowLevelClient().performRequest(request);
return EntityUtils.toString(response.getEntity());
} catch (IOException e) {
LOGGER.error("failed to query", e);
}
return "failed to query";
}
}
网友评论