查询相关的API
分页查询所有文档
	@Autowired
    private RestHighLevelClient hClient;
    
	@Test
    public void testSearch() throws IOException {
        // 1. 创建搜索请求对象, 需指定索引库
        SearchRequest searchRequest = new SearchRequest("orcas");
        // 1.1 指定类型
        searchRequest.types("doc");

        // 2. 搜索源构建对象
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        // 2.1 设置分页参数
        int page = 1; // 页码
        int size = 1;
        int from = (page - 1) * size;
        searchSourceBuilder.from(from); // 起始下标, 从0开始
        searchSourceBuilder.size(); // 每页显示记录数
        // 2.2 设置搜索方式 (该处是查询所有)
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        // 2.3 设置源字段过滤 fetchSource(String[] includes, String[] excludes)
        searchSourceBuilder.fetchSource(new String[]{"name", "price"}, new String[]{});
        // 2.4 设置搜索源至搜索请求对象
        searchRequest.source(searchSourceBuilder);

        // 3. 执行搜索, 向ES发起HTTP请求
        SearchResponse searchResponse = hClient.search(searchRequest);

        // 4. 搜索结果
        SearchHits hits = searchResponse.getHits();
        // 4.1 匹配到的总记录数
        long totalHits = hits.getTotalHits();
        // 4.2 获取匹配度高的文档
        SearchHit[] searchHits = hits.getHits();
        for (SearchHit searchHit : searchHits) {
            String id = searchHit.getId();
            Map<String, Object> sourceMap = searchHit.getSourceAsMap();
            String name = (String)sourceMap.get("name");
            Double price = (Double)sourceMap.get("price");
        }
    }

Term Query
精确匹配, 不分词

// 精确匹配, 不分词
searchSourceBuilder.query(QueryBuilders.termQuery("name", "orcas"));

根据(多个)id查询

searchSourceBuilder.query(QueryBuilders.termsQuery("_id", new String[] {"1", "3", "5"}));

match query
全文检索、分词

searchSourceBuilder.query(QueryBuilders.matchQuery("name", "tiger儿童保温杯")
                                  .minimumShouldMatch("80%"));

multi query
搜索多个field

MultiMatchQueryBuilder multiMatchQuery(Object text, String... fieldNames)
  // .field(String field, float boost)
  searchSourceBuilder.query(QueryBuilders.multiMatchQuery("tiger 保温杯", "name", "desc")
                                        .minimumShouldMatch("50%").field("name", 10));

布尔查询

searchSourceBuilder.query(QueryBuilders.boolQuery().must(termQueryBuilder).should(multiMatchQuery));

过滤
对搜索结果进行过滤,不影响得分,性能较高

boolQueryBuilder.filter(QueryBuilders.termQuery("name", "保温杯"));
boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(200).lte(500));

排序

searchSourceBuilder.sort("price", SortOrder.ASC);

搜索高亮

for (SearchHit searchHit : searchHits) {
            Map<String, HighlightField> highlightFields = searchHit.getHighlightFields();
            if (highlightFields != null) {
                // 取出高亮字段
                HighlightField highlightName = highlightFields.get("name");
                if (highlightName != null) {
                    StringBuffer stringBuffer = new StringBuffer();
                    for (Text text : highlightName.getFragments()) {
                        stringBuffer.append(text);
                    }
                    name = stringBuffer.toString();
                }
            }
        }

        HighlightBuilder highlightBuilder = new HighlightBuilder();
        highlightBuilder.preTags("<tag>");
        highlightBuilder.postTags("</tag>");
        highlightBuilder.fields().add(new HighlightBuilder.Field("name"));
        searchSourceBuilder.highlighter(highlightBuilder);

再对tag标签设置css。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐