ElasticSearch7.2.0之java查询

原创
2019/07/17 15:16
阅读数 537

新建maven项目,jdk1.8,pom.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>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.4.RELEASE</version>
		<relativePath />
	</parent>

	<groupId>com.tiger</groupId>
	<artifactId>es7</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>
	<name>es7</name>

	<url>http://maven.apache.org</url>

	<properties>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter</artifactId>
		</dependency>
		<!--web -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>

		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>elasticsearch-rest-high-level-client</artifactId>
			<version>7.2.0</version>
			<exclusions>
				<exclusion>
					<groupId>org.elasticsearch</groupId>
					<artifactId>elasticsearch</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.elasticsearch.client</groupId>
					<artifactId>elasticsearch-rest-client</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
			<groupId>org.elasticsearch.client</groupId>
			<artifactId>elasticsearch-rest-client</artifactId>
			<version>7.2.0</version>
		</dependency>
		
		<dependency>
			<groupId>org.elasticsearch</groupId>
			<artifactId>elasticsearch</artifactId>
			<version>7.2.0</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpclient</artifactId>
			<version>4.5.9</version>
		</dependency>

		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpcore</artifactId>
			<version>4.4.11</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpmime</artifactId>
			<version>4.5.9</version>
		</dependency>
		<dependency>
			<groupId>org.apache.httpcomponents</groupId>
			<artifactId>httpasyncclient</artifactId>
			<version>4.1.3</version>
		</dependency>
		<dependency>
			<groupId>commons-lang</groupId>
			<artifactId>commons-lang</artifactId>
			<version>2.6</version>
		</dependency>

		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.15</version>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<executable>true</executable>
				</configuration>
			</plugin>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-compiler-plugin</artifactId>
				<version>2.0.2</version>
				<configuration>
					<encoding>utf-8</encoding>
					<source>1.8</source>
					<target>1.8</target>
				</configuration>
			</plugin>
		</plugins>

		<resources>
			<resource>
				<directory>src/main/resources</directory>
			</resource>
			<resource>
				<directory>src/main/java</directory>
				<includes>
					<include>**/*.xml</include>
				</includes>
			</resource>
		</resources>
	</build>

</project>

 

新建测试类Es7Test,引入的类文件如下:

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
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.search.SearchType;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
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.client.indices.GetIndexResponse;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.util.ObjectUtils;

 

1、创建RestHighLevelClient

    

client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http")));

如果是集群,添加所有的HttpHost对象(以英文逗号分隔),RestClient.builder()函数的参数是数组。

2、判断索引是否存在:

GetIndexRequest request = new GetIndexRequest(indexName);
return client.indices().exists(request, RequestOptions.DEFAULT);

3、删除索引:

	DeleteIndexRequest request = new DeleteIndexRequest("test"); 
	client.indices().delete(request, RequestOptions.DEFAULT);

4、创建索引:

CreateIndexRequest request = new CreateIndexRequest("test");
client.indices().create(request, RequestOptions.DEFAULT);

5、插入数据:

BulkRequest bulkRequest = new BulkRequest();
List<String> datas = Files.readAllLines(Paths.get("E:\\test-data.txt"));
for(String line : datas) {
	IndexRequest request = new IndexRequest("test");
	bulkRequest.add(request.source(line, XContentType.JSON));
}
client.bulk(bulkRequest, RequestOptions.DEFAULT);

txt文件每行为一个json字符串。

6、查询数据:

                SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
				sourceBuilder.size(10);
				MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("srcIp", "1.1.4.4");
				RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("timestamp");
				  rangeQueryBuilder.gte(1567545612);
			      rangeQueryBuilder.lte(1857545612);
				BoolQueryBuilder boolq = QueryBuilders.boolQuery();
				boolq.must(matchQueryBuilder);
				boolq.must(rangeQueryBuilder);
				sourceBuilder.query(boolq);
				SearchRequest searchRequest = new SearchRequest("test");
				searchRequest.source(sourceBuilder);
				try {
					SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
					System.out.println("结果总数:"+ response.getHits().getTotalHits().value);
					SearchHit[] sh = response.getHits().getHits();
			        for(int i=0;i<sh.length;i++) {
			        	Map<String,Object> hit = sh[i].getSourceAsMap();
			        	for(String key : hit.keySet()) {
			        		System.out.print(key+"="+hit.get(key)+",");
			        	}
			     		System.out.println();
			        }
				}catch(Exception e) {
					e.printStackTrace();
				}

示例为匹配查询和范围查询。

 

 

展开阅读全文
打赏
1
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
1
分享
返回顶部
顶部