前言
本节实现高级查询中的全量查询
实现
之前的批量插入做一点修改,ESTest_Doc_Insert_Batch.java
package com.zwy.es;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpHost;
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.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import java.io.IOException;
public class ESTest_Doc_Insert_Batch {
public static void main(String[] args) throws IOException {
//
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 批量插入数据
BulkRequest request = new BulkRequest();
// 定义数据,添加入request
request.add(new IndexRequest().index("users").id("1001").source(XContentType.JSON, "name", "张三", "age", 30, "sex", "男"));
request.add(new IndexRequest().index("users").id("1002").source(XContentType.JSON, "name", "李四", "age", 30, "sex", "女"));
request.add(new IndexRequest().index("users").id("1003").source(XContentType.JSON, "name", "王五", "age", 40, "sex", "男"));
request.add(new IndexRequest().index("users").id("1004").source(XContentType.JSON, "name", "王五1", "age", 30, "sex", "女"));
request.add(new IndexRequest().index("users").id("1005").source(XContentType.JSON, "name", "王五2", "age", 50, "sex", "男"));
request.add(new IndexRequest().index("users").id("1003").source(XContentType.JSON, "name", "王五3", "age", 50, "sex", "男"));
BulkResponse response = esClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(response.getTook());
System.out.println(response.getItems());
// 关闭ES客户端
esClient.close();
}
}
ESTest_Doc_Query.java
package com.zwy.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
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.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.io.IOException;
public class ESTest_Doc_Query {
public static void main(String[] args) throws IOException {
//
RestHighLevelClient esClient = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http"))
);
// 查询数据
SearchRequest request = new SearchRequest();
request.indices("users");
request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));
SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
System.out.println(hits.getTotalHits()); // 打印
System.out.println(response.getTook()); // 打印
for (SearchHit hit : hits) {
System.out.println(hit.getSourceAsString());
}
// 关闭ES客户端
esClient.close();
}
}
文件结构
文件结构如下:
运行
先运行ESTest_Doc_Insert_Batch.java,插入数据
使用GET请求,输入地址如下:
http://127.0.0.1:9200/users/_search
用POSTMAN查看:
可以看到我们新增的插入数据
然后运行ESTest_Doc_Query.java