Elastic Stack:es JavaApi搜索入门
一.入门API
@SpringBootTest(classes = SearchApplication.class) @RunWith(SpringRunner.class) public class TestSearch { @Qualifier("restHighLevelClient") @Autowired RestHighLevelClient client; @Test public void testSearchAll() throws IOException { //构建搜索请求 SearchRequest request = new SearchRequest("book"); //构建搜索请求体 SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.matchAllQuery()); //第一个参数:包含哪些字段 第二个参数:排除哪些字段 builder.fetchSource(new String[]{},new String[]{"pic","timestamp"}); request.source(builder); //执行搜索请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); //获取结果 SearchHits hits = response.getHits(); SearchHit[] hits1 = hits.getHits(); Arrays.asList(hits1).forEach(hit->{ System.out.println(hit.getId()); System.out.println(hit.getScore()); Map<String, Object> map = hit.getSourceAsMap(); map.forEach((x,y)-> System.out.println(x+":"+y)); System.out.println("=========="); }); } }
二.常用方法
分页:
builder.from(0); builder.size(2);
ids查询:
builder.query(QueryBuilders.idsQuery().addIds("1","3","5"));
match搜索:
builder.query(QueryBuilders.matchQuery("name","java编程"));
multi_match搜索:
第一个参数:查询的关键字
后面的参数:查询的字段
builder.query(QueryBuilders.multiMatchQuery("java","name","description"));
三.bool query查询
@Test public void testSearchBool() throws IOException { //构建搜索请求 SearchRequest request = new SearchRequest("book"); //构建搜索请求体 SearchSourceBuilder builder = new SearchSourceBuilder(); //构建bool BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); boolQueryBuilder.must(QueryBuilders.matchQuery("description","java")); boolQueryBuilder.should(QueryBuilders.matchQuery("name","开发")); builder.query(boolQueryBuilder); //第一个参数:包含哪些字段 第二个参数:排除哪些字段 builder.fetchSource(new String[]{},new String[]{"pic","timestamp"}); request.source(builder); //执行搜索请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); //获取结果 SearchHits hits = response.getHits(); SearchHit[] hits1 = hits.getHits(); Arrays.asList(hits1).forEach(hit->{ System.out.println(hit.getId()); System.out.println(hit.getScore()); Map<String, Object> map = hit.getSourceAsMap(); map.forEach((x,y)-> System.out.println(x+":"+y)); System.out.println("=========="); }); }
filter:
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte("50").lte("90"));
四.sort排序
按照价格降序
@Test public void testSearchSort() throws IOException { //构建搜索请求 SearchRequest request = new SearchRequest("book"); //构建搜索请求体 SearchSourceBuilder builder = new SearchSourceBuilder(); builder.query(QueryBuilders.matchAllQuery()); //第一个参数:包含哪些字段 第二个参数:排除哪些字段 builder.fetchSource(new String[]{},new String[]{"pic","timestamp"}); //排序 builder.sort("price", SortOrder.DESC); request.source(builder); //执行搜索请求 SearchResponse response = client.search(request, RequestOptions.DEFAULT); //获取结果 SearchHits hits = response.getHits(); SearchHit[] hits1 = hits.getHits(); Arrays.asList(hits1).forEach(hit->{ System.out.println(hit.getId()); System.out.println(hit.getScore()); Map<String, Object> map = hit.getSourceAsMap(); map.forEach((x,y)-> System.out.println(x+":"+y)); System.out.println("=========="); }); }