Elastic Stack:es JavaApi搜索入门

Posted 秋风飒飒吹

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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("==========");
        });
    }

  

以上是关于Elastic Stack:es JavaApi搜索入门的主要内容,如果未能解决你的问题,请参考以下文章

Elastic Stack:es 搜索入门

Elastic stack技术栈学习— springboot集成ES API详解

Elastic stack--ES/kibana/filebeat/metricbeat

Elastic stack技术栈学习—— SpringBoot集成ES(更新中)

Elastic Stack:es Mapping映射入门

ES 集中式日志分析平台 Elastic Stack(介绍)