码迷,mamicode.com
首页 > 编程语言 > 详细

Elastic Stack:es JavaApi搜索入门

时间:2020-06-12 14:27:58      阅读:57      评论:0      收藏:0      [点我收藏+]

标签:array   price   search   esc   def   结果   bool   pre   except   

一.入门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("==========");
        });
    }

  

Elastic Stack:es JavaApi搜索入门

标签:array   price   search   esc   def   结果   bool   pre   except   

原文地址:https://www.cnblogs.com/wwjj4811/p/13098982.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!