当前位置:   article > 正文

关于ElasticSearch(v7.12.1)的Java客户端集成_junit-jupiter-api-5.7.2

junit-jupiter-api-5.7.2

关于ElasticSearch(v7.12.1)的Java客户端集成

在集成es的时候遇到很多问题,记录一下

maven项目

目录结构
目录结构

pom文件

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.5.0</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>com.sjq</groupId>
	<artifactId>ElasticsearchSDK</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>ElasticsearchSDK</name>
	<description>ElasticsearchSDK</description>
	<properties>
		<java.version>1.8</java.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>

		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>7.12.1</version>
		</dependency>

		<!-- Java High Level REST Client -->
		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>elasticsearch-rest-high-level-client</artifactId>
			<version>7.12.1</version>
		</dependency>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.5.0</version>
			</plugin>
		</plugins>
	</build>

</project>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56

ElasticsearchSdkApplication

package com.sjq.ElasticsearchSDK;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ElasticsearchSdkApplication {

	public static void main(String[] args) {
		SpringApplication.run(ElasticsearchSdkApplication.class, args);
	}

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

ElasticSearchUtils

package com.sjq.ElasticsearchSDK.utils;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class ElasticSearchUtils {

    public static void main(String[] args) {
        try {
            RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient
                    .builder(new HttpHost("127.0.0.1", 9200, "http")));

            SearchRequest request = new SearchRequest("cjxxb");
            SearchSourceBuilder builder = new SearchSourceBuilder();

            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("jjbh", "05");

            builder.query(matchQueryBuilder);
            builder.from(0);
            builder.size(30);

            request.source(builder);

            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            System.out.println("response.getHits() = " + response.getHits());
            restHighLevelClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

普通Java项目

目录结构
在这里插入图片描述

lib目录

accessors-smart-2.4.7.jar
aggs-matrix-stats-client-7.12.1.jar
android-json-0.0.20131108.vaadin1.jar
apiguardian-api-1.1.0.jar
asm-9.1.jar
assertj-core-3.19.0.jar
byte-buddy-1.10.22.jar
byte-buddy-agent-1.10.22.jar
commons-codec-1.15.jar
compiler-0.9.6.jar
elasticsearch-7.12.1.jar
elasticsearch-cli-7.12.1.jar
elasticsearch-core-7.12.1.jar
elasticsearch-geo-7.12.1.jar
elasticsearch-plugin-classloader-7.12.1.jar
elasticsearch-rest-client-7.12.1.jar
elasticsearch-rest-high-level-client-7.12.1.jar
elasticsearch-secure-sm-7.12.1.jar
elasticsearch-x-content-7.12.1.jar
hamcrest-2.2.jar
HdrHistogram-2.1.9.jar
hppc-0.8.1.jar
httpasyncclient-4.1.4.jar
httpclient-4.5.13.jar
httpcore-4.4.14.jar
httpcore-nio-4.4.14.jar
jackson-core-2.12.3.jar
jackson-dataformat-cbor-2.12.3.jar
jackson-dataformat-smile-2.12.3.jar
jackson-dataformat-yaml-2.12.3.jar
jakarta.activation-api-1.2.2.jar
jakarta.annotation-api-1.3.5.jar
jakarta.xml.bind-api-2.3.3.jar
jna-5.7.0-1.jar
joda-time-2.10.4.jar
jopt-simple-5.0.2.jar
json-path-2.5.0.jar
json-smart-2.4.7.jar
jsonassert-1.5.0.jar
jul-to-slf4j-1.7.30.jar
junit-jupiter-5.7.2.jar
junit-jupiter-api-5.7.2.jar
junit-jupiter-engine-5.7.2.jar
junit-jupiter-params-5.7.2.jar
junit-platform-commons-1.7.2.jar
junit-platform-engine-1.7.2.jar
lang-mustache-client-7.12.1.jar
log4j-api-2.14.1.jar
log4j-to-slf4j-2.14.1.jar
logback-classic-1.2.3.jar
logback-core-1.2.3.jar
lucene-analyzers-common-8.8.0.jar
lucene-backward-codecs-8.8.0.jar
lucene-core-8.8.0.jar
lucene-grouping-8.8.0.jar
lucene-highlighter-8.8.0.jar
lucene-join-8.8.0.jar
lucene-memory-8.8.0.jar
lucene-misc-8.8.0.jar
lucene-queries-8.8.0.jar
lucene-queryparser-8.8.0.jar
lucene-sandbox-8.8.0.jar
lucene-spatial-extras-8.8.0.jar
lucene-spatial3d-8.8.0.jar
lucene-suggest-8.8.0.jar
mapper-extras-client-7.12.1.jar
mockito-core-3.9.0.jar
mockito-junit-jupiter-3.9.0.jar
objenesis-3.2.jar
opentest4j-1.2.0.jar
parent-join-client-7.12.1.jar
rank-eval-client-7.12.1.jar
slf4j-api-1.7.30.jar
snakeyaml-1.28.jar
spring-aop-5.3.7.jar
spring-beans-5.3.7.jar
spring-boot-2.5.0.jar
spring-boot-autoconfigure-2.5.0.jar
spring-boot-starter-2.5.0.jar
spring-boot-starter-logging-2.5.0.jar
spring-boot-starter-test-2.5.0.jar
spring-boot-test-2.5.0.jar
spring-boot-test-autoconfigure-2.5.0.jar
spring-context-5.3.7.jar
spring-core-5.3.7.jar
spring-expression-5.3.7.jar
spring-jcl-5.3.7.jar
spring-test-5.3.7.jar
t-digest-3.2.jar
xmlunit-core-2.8.2.jar

ElasticSearchUtils

package com.sjq.ElasticsearchSDK.utils;

import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;

public class ElasticSearchUtils {

    public static void main(String[] args) {
        try {
            RestHighLevelClient restHighLevelClient = new RestHighLevelClient(RestClient
                    .builder(new HttpHost("127.0.0.1", 9200, "http")));

            SearchRequest request = new SearchRequest("cjxxb");
            SearchSourceBuilder builder = new SearchSourceBuilder();

            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("jjbh", "05");

            builder.query(matchQueryBuilder);
            builder.from(0);
            builder.size(30);

            request.source(builder);

            SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
            System.out.println("response.getHits() = " + response.getHits());
            restHighLevelClient.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/68419
推荐阅读
相关标签