演示代码
<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.0modelVersion>
<groupId>com.vmwaregroupId>
<artifactId>spring-customartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<maven.compiler.source>17maven.compiler.source>
<maven.compiler.target>17maven.compiler.target>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.elasticsearchgroupId>
<artifactId>elasticsearchartifactId>
<version>7.8.0version>
dependency>
<dependency>
<groupId>org.elasticsearch.clientgroupId>
<artifactId>elasticsearch-rest-high-level-clientartifactId>
<version>7.8.0version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-apiartifactId>
<version>2.8.2version>
dependency>
<dependency>
<groupId>org.apache.logging.log4jgroupId>
<artifactId>log4j-coreartifactId>
<version>2.8.2version>
dependency>
<dependency>
<groupId>com.fasterxml.jackson.coregroupId>
<artifactId>jackson-databindartifactId>
<version>2.9.9version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.12version>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
<version>1.18.24version>
dependency>
<dependency>
<groupId>com.google.code.gsongroupId>
<artifactId>gsonartifactId>
<version>2.8.9version>
dependency>
dependencies>
project>
package com.vmware.elastic;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import java.io.IOException;
/**
* @apiNote 演示创建elastic客户端,与关闭连接
*/
public class HelloElasticSearch {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
client.close();
}
}
package com.vmware.elastic.index;
import org.apache.http.HttpHost;
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 java.io.IOException;
/**
* @apiNote 演示创建索引
*/
public class IndexCreate {
public static void main(String[] args) throws IOException {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
CreateIndexRequest request = new CreateIndexRequest("vmware");
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
System.out.println("索引操作:" + response.isAcknowledged());
client.close();
}
}
package com.vmware.elastic.index;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* @apiNote 演示删除索引
*/
public class IndexDelete {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
DeleteIndexRequest request = new DeleteIndexRequest("vmware");
AcknowledgedResponse response = client.indices().delete(request, RequestOptions.DEFAULT);
System.out.println(response.isAcknowledged());//acknowledged表示操作是否成功
client.close();
}
}
package com.vmware.elastic.index;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.client.indices.GetIndexResponse;
/**
* @apiNote 演示查询索引
*/
public class IndexQuery {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
GetIndexRequest request = new GetIndexRequest("vmware");
GetIndexResponse response = client.indices().get(request, RequestOptions.DEFAULT);
System.out.println(response.getAliases());//获取别名
//{vmware=[]}
System.out.println(response.getMappings());//获取索引映射
//{vmware=org.elasticsearch.cluster.metadata.MappingMetadata@9b2cfd3c}
System.out.println(response.getSettings());//获取索引设置
//{vmware={"index.creation_date":"1690635515922","index.number_of_replicas":"1","index.number_of_shards":"1","index.provided_name":"vmware","index.uuid":"HtIuUNNnTTyz9CT2oz0Lww","index.version.created":"7060299"}}
client.close();
}
}
package com.vmware.elastic.document;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.vmware.elastic.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
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.common.xcontent.XContentType;
/**
* @apiNote 演示创建文档
*/
public class DocumentCreate {
private static Gson gson = new Gson();
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
IndexRequest request = new IndexRequest();
request.index("vmware");
request.id("1001");
User user = new User();
user.setName("张三");
user.setAge(18);
user.setSex("男");
//es client操作索引需要将java对象转为json格式
String json = gson.toJson(user);
request.source(json, XContentType.JSON);
IndexResponse response = client.index(request, RequestOptions.DEFAULT);
DocWriteResponse.Result result = response.getResult();
System.out.println(result);//CREATED
client.close();
}
}
package com.vmware.elastic.document;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
/**
* @apiNote 演示删除文档
*/
public class DocumentDelete {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
DeleteRequest request=new DeleteRequest("vmware","1001");
DeleteResponse response = client.delete(request, RequestOptions.DEFAULT);
System.out.println(response.getResult());//DELETED
client.close();
}
}
package com.vmware.elastic.document;
import com.google.gson.Gson;
import com.vmware.elastic.entity.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
/**
* @apiNote 演示更新文档
*/
public class DocumentUpdate {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
UpdateRequest request = new UpdateRequest();
request.index("vmware");
request.id("1001");
request.doc(XContentType.JSON,"name","李四");
UpdateResponse response = client.update(request, RequestOptions.DEFAULT);
DocWriteResponse.Result result = response.getResult();
System.out.println(result);//UPDATED
client.close();
}
}
package com.vmware.elastic.document;
import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* @apiNote 演示查询文档
*/
public class DocumentQuery {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
GetRequest request = new GetRequest("vmware", "1001");
GetResponse response = client.get(request, RequestOptions.DEFAULT);
System.out.println(response.getSourceAsString());//{"name":"李四","age":18,"sex":"男"}
client.close();
}
}
package com.vmware.elastic.batch;
import org.apache.http.HttpHost;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
/**
* @apiNote 批量创建文档
*/
public class DocumentBatchCreate {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
BulkRequest request = new BulkRequest("vmware");//bulk:大批的
request.add(new IndexRequest().id("1005").source(XContentType.JSON,"name","wangwu","age",30));
request.add(new IndexRequest().id("1006").source(XContentType.JSON,"name","wangwu2","age",40));
request.add(new IndexRequest().id("1007").source(XContentType.JSON,"name","wangwu33","age",50));
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());//6ms
System.out.println(response.getItems());//[Lorg.elasticsearch.action.bulk.BulkItemResponse;@6bb4dd34
client.close();
}
}
package com.vmware.elastic.batch;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
/**
* @apiNote 批量删除
*/
public class DocumentBatchDelete {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
BulkRequest request = new BulkRequest("vmware");//bulk:大批的
request.add(new DeleteRequest().id("1005"));
request.add(new DeleteRequest().id("1006"));
request.add(new DeleteRequest().id("1007"));
BulkResponse response = client.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());//8ms
System.out.println(response.getItems());//[Lorg.elasticsearch.action.bulk.BulkItemResponse;@624ea235
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.MaxAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.util.List;
/**
* @apiNote 聚合查询
*/
public class DocumentAggregateQuery {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
//最大值查询
// MaxAggregationBuilder maxAggregationBuilder = AggregationBuilders.max("maxAge").field("age");
TermsAggregationBuilder termsAggregationBuilder = AggregationBuilders.terms("ageGroup").field("age");//分组查询
request.source(new SearchSourceBuilder().aggregation(termsAggregationBuilder));//构建查询,设置为全量查询
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
for (Aggregation aggregation : response.getAggregations()) {
ParsedLongTerms terms = (ParsedLongTerms) aggregation;
List<? extends Terms.Bucket> buckets = terms.getBuckets();
for (Terms.Bucket bucket : buckets) {
System.out.println(bucket.getKeyAsString() + ":" + bucket.getDocCount());
}
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
/**
* @apiNote 组合查询
*/
public class DocumentCombinationQuery {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();
//构建组合查询条件 must:全部满足
// boolQuery.must(QueryBuilders.matchQuery("name","王五"));
// boolQuery.must(QueryBuilders.matchQuery("age",30));
//构建组合条件 should:满足任意一个条件即可
// boolQuery.should(QueryBuilders.matchQuery("age",30));
// boolQuery.should(QueryBuilders.matchQuery("age",50));
//构建组合查询条件 mushNot:不满足条件
boolQuery.mustNot(QueryBuilders.matchQuery("age",30));
request.source(new SearchSourceBuilder().query(boolQuery));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
/**
* @apiNote 条件查询
*
* 响应时间:0s
* {
* "_index" : "vmware",
* "_type" : "_doc",
* "_id" : "1005",
* "_score" : 1.0,
* "_source" : {
* "name" : "王五",
* "age" : 30
* }
* }
*/
public class DocumentConditionalQuery {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age","30")));//设置查询条件为name=王五
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);//
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
/**
* @apiNote 过滤查询
*/
public class DocumentFilterQuery {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
String[] includes = {"name","age"};//需要包含的字段
String[] excludes = {}; //需要排除的字段
request.source(
new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).fetchSource(includes, excludes)//添加过滤条件
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.Fuzziness;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.FuzzyQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
/**
* @apiNote 模糊查询
*/
public class DocumentFuzzyQuery {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
//设置模糊查询 Fuzziness.ONE:差一个字也可以查询出来 注意:需要字段设置为keyword类型,否则es会对查询条件进行分词导致模糊查询失效
FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("name", "王五").fuzziness(Fuzziness.ONE);
request.source(new SearchSourceBuilder().query(fuzzyQueryBuilder));//构建查询,设置为全量查询
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
/**
* @apiNote 高亮查询
*/
public class DocumentHighLightQuery {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "wangwu");//注意:高亮不支持中文
searchSourceBuilder.query(termQueryBuilder);
//构建高亮查询
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.preTags("" );//设置前缀标签
highlightBuilder.postTags("");//设置后缀标签
highlightBuilder.field("name");//设置高亮字段
searchSourceBuilder.highlighter(highlightBuilder);
request.source(searchSourceBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);//wangwu
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
/**
* @apiNote 全量查询
*
* 响应时间:0s
* {
* "_index" : "vmware",
* "_type" : "_doc",
* "_id" : "1005",
* "_score" : 1.0,
* "_source" : {
* "name" : "王五",
* "age" : 30
* }
* }
* {
* "_index" : "vmware",
* "_type" : "_doc",
* "_id" : "1006",
* "_score" : 1.0,
* "_source" : {
* "name" : "赵六",
* "age" : 40
* }
* }
* {
* "_index" : "vmware",
* "_type" : "_doc",
* "_id" : "1007",
* "_score" : 1.0,
* "_source" : {
* "name" : "陈⑦",
* "age" : 50
* }
* }
*/
public class DocumentMatchAllQuery {
public static void main(String[] args) throws Exception {
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));//构建查询,设置为全量查询
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
/**
* @apiNote 对返回结果排序
*/
public class DocumentOrderQuery {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
request.source(
new SearchSourceBuilder().query(QueryBuilders.matchAllQuery())
.sort("age", SortOrder.ASC)//设置排序规则
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
/**
* @apiNote 分页查询
*/
public class DocumentPagingQuery {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
request.source(
new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).from(0).size(2) //设置分页
);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);
}
client.close();
}
}
package com.vmware.elastic.high;
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.common.unit.TimeValue;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
/**
* @apiNote 范围查询
*/
public class DocumentRangeQuery {
public static void main(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(
new HttpHost("10.192.193.98", 9200))
);
SearchRequest request = new SearchRequest("vmware");
RangeQueryBuilder queryBuilder = QueryBuilders.rangeQuery("age").gte(40).lt(50);//构建范围查询 大于等于40小于50
request.source(new SearchSourceBuilder().query(queryBuilder));
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();//获取查询结果
TimeValue took = response.getTook(); //获取响应时间
System.out.println("响应时间:" + took);
for (SearchHit hit : hits) {
System.out.println(hit);
}
client.close();
}
}