• 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("==========");
            });
        }
    

      

  • 相关阅读:
    04_移动端-伪元素选择器
    03_移动端-结构伪类选择器
    02_移动端-属性选择器
    Dva+Antd创建React项目(一)
    Windows 10 使用打印机扫描
    中间件-redis
    #期望dp#51nod 2015 诺德街
    Codeforces Round #685 (Div. 2)
    USACO 4.2
    #保序回归问题,单调栈,二分#洛谷 5294 [HNOI2019]序列
  • 原文地址:https://www.cnblogs.com/wwjj4811/p/13098982.html
Copyright © 2020-2023  润新知