zoukankan      html  css  js  c++  java
  • 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("==========");
            });
        }
    

      

  • 相关阅读:
    MyEclipse Ctrl+F搜索框太小
    SqlServer2012评估期已过问题
    $("#form_iframe").contents().find('.nav-tabs').children().eq(2).hide();
    数字格式化
    equals和==的区别
    静态代码块
    this调用有参构造方法
    正则表达式
    日期和时间字符串格式化
    Legacy autograd function with non-static forward method is deprecated and will be removed in 1.3.
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/13098982.html
Copyright © 2011-2022 走看看