ElasticSearch7.2.0之java查询

新建maven项目,jdk1.8,pom.xml内容如下:




	4.0.0

	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.4.RELEASE
		
	

	com.tiger
	es7
	0.0.1-SNAPSHOT
	jar
	es7

	http://maven.apache.org

	
		1.8
		1.8
		UTF-8
	

	
		
			org.springframework.boot
			spring-boot-starter
		
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.elasticsearch.client
			elasticsearch-rest-high-level-client
			7.2.0
			
				
					org.elasticsearch
					elasticsearch
				
				
					org.elasticsearch.client
					elasticsearch-rest-client
				
			
		

		
			org.elasticsearch.client
			elasticsearch-rest-client
			7.2.0
		
		
		
			org.elasticsearch
			elasticsearch
			7.2.0
		

		
			org.apache.httpcomponents
			httpclient
			4.5.9
		

		
			org.apache.httpcomponents
			httpcore
			4.4.11
		
		
			org.apache.httpcomponents
			httpmime
			4.5.9
		
		
			org.apache.httpcomponents
			httpasyncclient
			4.1.3
		
		
			commons-lang
			commons-lang
			2.6
		

		
			com.alibaba
			fastjson
			1.2.15
		

	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
				
					true
				
			
			
				org.apache.maven.plugins
				maven-compiler-plugin
				2.0.2
				
					utf-8
					1.8
					1.8
				
			
		

		
			
				src/main/resources
			
			
				src/main/java
				
					**/*.xml
				
			
		
	


 

新建测试类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 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 hit = sh[i].getSourceAsMap();
			        	for(String key : hit.keySet()) {
			        		System.out.print(key+"="+hit.get(key)+",");
			        	}
			     		System.out.println();
			        }
				}catch(Exception e) {
					e.printStackTrace();
				}

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

 

 

转载于:https://my.oschina.net/u/161393/blog/3075284

你可能感兴趣的:(ElasticSearch7.2.0之java查询)