es查询-Java API
有Low Level REST Client和High Level REST Client。
Low Level REST Client需要自己拼接所有的语句,需要自己解析结果。
High Level REST Client基于 Low Level REST Client。
pom如下。
<dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.3.0</version> <exclusions> <exclusion> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.3.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.3</version> <scope>test</scope> </dependency> </dependencies>
准备日志配置文件
resource目录下创建:log4j2.xml
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN"> <Appenders> <Console name="Console" target="SYSTEM_OUT"> <PatternLayout pattern="%d{yyyy-mm-dd HH:mm:ss} [%t] %-5p %c{1}:%L - %msg%n" /> </Console> </Appenders> <Loggers> <Root level="info"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
样例:
RestHighLevelClient client; //准备一个highlevel client @Before public void init() { final RestHighLevelClient highLevelClient = new RestHighLevelClient(RestClient.builder( new HttpHost("linux121", 9200, "http"), new HttpHost("linux122", 9200, "http"), new HttpHost("linux123", 9200, "http") )); // System.out.println(highLevelClient.cluster().toString()); client = highLevelClient; } @After public void destory() { if (client != null) { try { client.close(); } catch (IOException e) { e.printStackTrace(); } } }
索引操作
//创建索引 @Test public void createIndex() { final CreateIndexRequest indexRequest = new CreateIndexRequest("es_test"); //准备mapping映射信息 String str = "{\n" + " \"settings\": {},\n" + " \"mappings\": {\n" + " \"properties\": {\n" + " \"description\": {\n" + " \"type\": \"text\",\n" + " \"analyzer\": \"ik_max_word\"\n" + " },\n" + " \"name\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"pic\": {\n" + " \"type\": \"text\",\n" + " \"index\": false\n" + " },\n" + " \"studymodel\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }\n" + " }"; //把映射信息添加到request对象中 indexRequest.source(str, XContentType.JSON); //创建索引 try { final CreateIndexResponse createIndexResponse = client.indices().create(indexRequest, RequestOptions.DEFAULT); final boolean acknowledged = createIndexResponse.isAcknowledged(); System.out.println("创建结果:" + acknowledged); } catch (IOException e) { e.printStackTrace(); } } indexRequest.settings( Settings.builder() .put("index.number_of_shards", 5) .put("index.number_of_replicas", 1) ); //指定mapping信息 final XContentBuilder xContentBuilder = XContentFactory.jsonBuilder(); xContentBuilder.startObject(); xContentBuilder.startObject("properties"); xContentBuilder.startObject("description") .field("type", "text") .field("analyzer", "ik_max_word") .endObject(); xContentBuilder.startObject("name") .field("type", "text") .endObject(); xContentBuilder.startObject("pic") .field("type", "text") .field("index", "false") .endObject(); xContentBuilder.startObject("studymodel") .field("type", "text") .endObject(); xContentBuilder.endObject(); xContentBuilder.endObject(); indexRequest.mapping(xContentBuilder); //创建索引 try { final CreateIndexResponse createIndexResponse = client.indices().create(indexRequest, RequestOptions.DEFAULT); final boolean acknowledged = createIndexResponse.isAcknowledged(); System.out.println("创建结果:" + acknowledged); } catch (IOException e) { e.printStackTrace(); } } //删除索引 @Test public void deleteIndex() throws IOException { //创建一个删除索引的请求 final DeleteIndexRequest indexRequest = new DeleteIndexRequest("es_test"); final AcknowledgedResponse delete = client.indices().delete(indexRequest, RequestOptions.DEFAULT); System.out.println(delete.isAcknowledged()); }
文档操作
//添加doc @Test public void addDoc() throws IOException { //准备一个添加文档的对象 final IndexRequest indexRequest = new IndexRequest("es_test").id("1"); /* { "name": "spark添加文档", "description": "spark技术栈", "studymodel":"online", "pic": "http://www.baidu.com" } */ //准备文档内容 String str = " {\n" + " \"name\": \"spark添加文档\",\n" + " \"description\": \"spark技术栈\",\n" + " \"studymodel\":\"online\",\n" + " \"pic\": \"http://www.baidu.com\"\n" + " }"; indexRequest.source(str, XContentType.JSON); //执行添加动作 final IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT); System.out.println(index.status()); }
//查询文档 @Test public void getDoc() throws IOException { //准备一个查询的reqeust请求 final GetRequest getRequest = new GetRequest("es_test"); //设定id he getRequest.id("1"); //执行查询 final GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); final Map<String, Object> sourceAsMap = getResponse.getSourceAsMap(); System.out.println(sourceAsMap); }
//查询所有文档 @Test public void getAllDoc() throws IOException { // 构建一个searchRequest对象 final SearchRequest searchRequest = new SearchRequest(); //指定索引 searchRequest.indices("es_test"); //指定查询条件 matchall final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchAllQuery()); searchRequest.source(sourceBuilder); //执行检索 RequestOptions options参数: 请求选项(例如请求头),如果不需要自定义任何内容,使用 RequestOptions.DEFAULT final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); //解析响应结果 final RestStatus status = response.status(); System.out.println("查询结果状态:" + status); final SearchHits hits = response.getHits(); final SearchHit[] hits1 = hits.getHits(); for (SearchHit searchHit : hits1) { System.out.println("----------------------------"); final Map<String, Object> map = searchHit.getSourceAsMap(); for (Map.Entry<String, Object> entry : map.entrySet()) { System.out.println("查询的字段:" + entry.getKey() + "; 字段value:" + entry.getValue()); } } }
//termquery
@Test
public void termQuery() throws IOException {
//根据关键字 不分词检索
final SearchRequest searchRequest = new SearchRequest("es_test");
//sourcebuilder
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//指定检索的类型
sourceBuilder.query(QueryBuilders.termQuery("studymodel", "online"));
//添加sourcebuilder
searchRequest.source(sourceBuilder);
//执行查询
final SearchResponse response = client.search(searchRequest,
RequestOptions.DEFAULT);
//解析response
final RestStatus status = response.status();
System.out.println("查询结果状态:" + status);
final SearchHits hits = response.getHits();
final SearchHit[] hits1 = hits.getHits();
for (SearchHit searchHit : hits1) {
System.out.println("----------------------------");
final Map<String, Object> map = searchHit.getSourceAsMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("查询的字段:" + entry.getKey() + "; 字段value:" +
entry.getValue());
}
}
}
//分页查询
@Test
public void getAllDocWithPage() throws IOException {
// 构建一个searchRequest对象
final SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("es_test");
//指定查询条件 matchall
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
//指定分页
sourceBuilder.from(0);
sourceBuilder.size(1);
searchRequest.source(sourceBuilder);
//执行检索 RequestOptions options参数: 请求选项(例如请求头),如果不需要自定义任何内容,使用
RequestOptions.DEFAULT
final SearchResponse response = client.search(searchRequest,
RequestOptions.DEFAULT);
//解析响应结果
final RestStatus status = response.status();
System.out.println("查询结果状态:" + status);
final SearchHits hits = response.getHits();
final SearchHit[] hits1 = hits.getHits();
for (SearchHit searchHit : hits1) {
System.out.println("----------------------------");
final Map<String, Object> map = searchHit.getSourceAsMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("查询的字段:" + entry.getKey() + "; 字段value:" +
entry.getValue());
}
}
}
//对返回的source字段进行过滤
@Test
public void searchWithLimit() throws IOException {
// 构建一个searchRequest对象
final SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("es_test");
//指定查询条件 matchall
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchAllQuery());
//准备两个数组,包含,不包含
final String[] includesArr = {"name"};
final String[] excludesArr = {};
sourceBuilder.fetchSource(includesArr, excludesArr);
searchRequest.source(sourceBuilder);
//执行检索 RequestOptions options参数: 请求选项(例如请求头),如果不需要自定义任何内容,使用
RequestOptions.DEFAULT
final SearchResponse response = client.search(searchRequest,
RequestOptions.DEFAULT);
//解析响应结果
final RestStatus status = response.status();
System.out.println("查询结果状态:" + status);
final SearchHits hits = response.getHits();
final SearchHit[] hits1 = hits.getHits();
for (SearchHit searchHit : hits1) {
System.out.println("----------------------------");
final Map<String, Object> map = searchHit.getSourceAsMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("查询的字段:" + entry.getKey() + "; 字段value:" +
entry.getValue());
}
}
}
// 排序 sort
@Test
public void searchWithSort() throws IOException {
// 构建一个searchRequest对象
final SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("book");
//指定查询条件 matchall
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//指定排序规则
sourceBuilder.sort(new FieldSortBuilder("price").order(SortOrder.DESC));
sourceBuilder.query(QueryBuilders.matchAllQuery());
searchRequest.source(sourceBuilder);
//执行检索 RequestOptions options参数: 请求选项(例如请求头),如果不需要自定义任何内容,使用
RequestOptions.DEFAULT
final SearchResponse response = client.search(searchRequest,
RequestOptions.DEFAULT);
//解析响应结果
final RestStatus status = response.status();
System.out.println("查询结果状态:" + status);
final SearchHits hits = response.getHits();
final SearchHit[] hits1 = hits.getHits();
for (SearchHit searchHit : hits1) {
System.out.println("----------------------------");
final Map<String, Object> map = searchHit.getSourceAsMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("查询的字段:" + entry.getKey() + "; 字段value:" +
entry.getValue());
}
}
}
//聚合分析
//按照name分组,对组内计算一个avg的price
@Test
public void searchAgg() throws IOException {
// 构建一个searchRequest对象
final SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("book");
//指定查询条件 matchall
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//指定聚合分析的规则
final TermsAggregationBuilder aggregationBuilder =
AggregationBuilders.terms("name_group").field("name");
aggregationBuilder.subAggregation(AggregationBuilders.avg("avg_price").field("price"));
sourceBuilder.aggregation(aggregationBuilder);
searchRequest.source(sourceBuilder);
//执行检索 RequestOptions options参数: 请求选项(例如请求头),如果不需要自定义任何内容,使用
RequestOptions.DEFAULT
final SearchResponse searchResponse = client.search(searchRequest,
RequestOptions.DEFAULT);
//解析响应结果
final Terms name_group = searchResponse.getAggregations().get("name_group");
for (Terms.Bucket bucket : name_group.getBuckets()) {
final Object key = bucket.getKey();
final Avg avg_price = bucket.getAggregations().get("avg_price");
System.out.println("name:" + key + ",平均价格:" + avg_price.getValue());
}
}
Fielddata is disabled on text fields by default. Set `fielddata=true` on[`your_field_name`] in order to load fielddata in memory by uninverting theinverted index. Note that this can however use significant memory.
https://www.elastic.co/guide/en/elasticsearch/reference/current/fielddata.html
有朝一日同风起,扶摇直上九万里