ElasticSearch
创建maven项目,引入elasticsearch依赖,使用版本7.8.0
1 2 3 4 5 6 7 8 9 10 11
| <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>7.8.0</version> </dependency> <!-- es客户端 --> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.8.0</version> </dependency>
|
创建es客户端
1 2 3 4 5 6 7 8 9 10
| public static void main(String[] args) throws IOException { RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("127.0.0.1", 9200, "http")));
client.close();
}
|
创建索引
1 2 3 4 5 6
| CreateIndexResponse order = client.indices().create(new CreateIndexRequest("order"), RequestOptions.DEFAULT);
boolean acknowledged = order.isAcknowledged();
System.out.println("创建结果: " + (acknowledged ? "成功": "失败"));
|
查询索引信息
1 2 3 4 5 6 7
| GetIndexResponse order = client.indices().get(new GetIndexRequest("order"), RequestOptions.DEFAULT);
System.out.println(order.getAliases()); System.out.println(order.getMappings()); System.out.println(order.getSettings());
|
删除索引
1 2 3 4
| AcknowledgedResponse order = client.indices().delete(new DeleteIndexRequest("order"),RequestOptions.DEFAULT); boolean acknowledged = order.isAcknowledged(); System.out.println("删除索引结果: " + (acknowledged ? "successful": "failure"));
|
创建文档
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| IndexRequest indexRequest = new IndexRequest("order");
indexRequest.id("1002");
Order order = new Order(); order.setName("手机"); order.setDesc("小米&xiaomi12"); order.setCount(1); order.setPrice(3999.99); String json = new ObjectMapper().writeValueAsString(order);
indexRequest.source(json, XContentType.JSON);
IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.getResult());
|
更新文档
1 2 3 4 5 6 7 8
| UpdateRequest order = new UpdateRequest();
order.index("order").id("1002");
order.doc(XContentType.JSON, "price", 2999.00); UpdateResponse update = client.update(order, RequestOptions.DEFAULT); System.out.println(update.getResult());
|
查询
1 2 3 4 5 6
| GetRequest getRequest = new GetRequest(); getRequest.index("order").id("1002"); GetResponse documentFields = client.get(getRequest, RequestOptions.DEFAULT); String sourceAsString = documentFields.getSourceAsString(); System.out.println(sourceAsString);
|
删除
1 2 3 4 5
| DeleteRequest deleteRequest = new DeleteRequest(); deleteRequest.index("order").id("1003"); DeleteResponse delete = client.delete(deleteRequest, RequestOptions.DEFAULT); System.out.println(delete.getResult());
|
全量查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("order");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()); searchRequest.source(query); SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); for (SearchHit hit : search.getHits()) { System.out.println(hit.getSourceAsString()); }
|
条件查询&分页&排序&字段过滤
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("order");
SearchSourceBuilder query = new SearchSourceBuilder().query(QueryBuilders.matchQuery("name", "手机"));
query.from(0).size(3);
query.sort("price", SortOrder.DESC);
String[] includes = {"name", "price"};
String[] excludes = {"count"}; query.fetchSource(includes, excludes); searchRequest.source(query); SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); for (SearchHit hit : search.getHits()) { System.out.println(hit.getSourceAsString()); }
|
组合查询(多条件组合查询、范围查询)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("order");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.must(QueryBuilders.matchQuery("count", "1")); BoolQueryBuilder boolQueryBuilderOr = QueryBuilders.boolQuery(); boolQueryBuilderOr.should(QueryBuilders.matchQuery("price", 1999.0)); boolQueryBuilderOr.should(QueryBuilders.matchQuery("price", 2999.0)); boolQueryBuilder.must(boolQueryBuilderOr);
BoolQueryBuilder boolQueryBuilderAnd = QueryBuilders.boolQuery(); RangeQueryBuilder priceQuery = QueryBuilders.rangeQuery("price"); priceQuery.gt(2000).lt(4000); boolQueryBuilderAnd.must(priceQuery); boolQueryBuilder.must(boolQueryBuilderAnd); searchSourceBuilder.query(boolQueryBuilder);
searchRequest.source(searchSourceBuilder); SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); for (SearchHit hit : search.getHits()) { System.out.println(hit.getSourceAsString()); }
|
模糊查询&高亮查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("order");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
FuzzyQueryBuilder fuzziness = QueryBuilders.fuzzyQuery("name", "手表").fuzziness(Fuzziness.ONE);
HighlightBuilder highlightBuilder = new HighlightBuilder(); highlightBuilder.preTags("<font color='red'>").postTags("</font>").field("name"); searchSourceBuilder.highlighter(highlightBuilder); searchSourceBuilder.query(fuzziness); searchRequest.source(searchSourceBuilder); SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); for (SearchHit hit : search.getHits()) { System.out.println(hit.getSourceAsString()); }
|
分组查询
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("order"); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("max_group").field("price"); searchSourceBuilder.aggregation(aggregationBuilder); searchRequest.source(searchSourceBuilder); SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT); for (SearchHit hit : search.getHits()) { System.out.println(hit.getSourceAsString()); }
|
PS:github地址: https://github.com/Alwaysbryant/es-study-javaapi.git