springboot2.1.8+elasticsearch7.3.2(四),查询文档

我的DEMO项目下载:https://gitee.com/zhuhongliang/soringboot_elastic_search_732.git

按照前几篇文章的方法,现在再创建一个新的实体类Article2.class,除了名称不一样,类成员都一样

public class Article2 {
    private Integer id;
    private String title;
    private String content;
    private Date create_time;
省略getter & setter
}

用前几篇文章写得方法,为这个实体类创建索引,并批量创建文档。
我们来查询一下,看代码:
service:

/**
 *
 * @param clasz 要查询的类
 * @param value 搜索内容
 * @param start 开始下标
 * @param size 搜索范围
 * @param fieldName 搜索字段
 * @return
 */
public SearchHit[] searchDoc(Class clasz, String value, int start, int size, String... fieldName){

    SearchRequest searchRequest;
    if (clasz == null) {
        searchRequest = new SearchRequest();
    }else{
        searchRequest = new SearchRequest(clasz.getSimpleName().toLowerCase());
    }
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    log.info("fieldName.length:{},fieldName:{}",fieldName.length,fieldName);
    QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(value, fieldName);
    searchSourceBuilder.query(queryBuilder);
    searchSourceBuilder.from(start);
    searchSourceBuilder.size(size);
    searchRequest.source(searchSourceBuilder);
    return search2(searchRequest);

}
public SearchHit[] search2(SearchRequest searchRequest){
    try {
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        SearchHits hits = searchResponse.getHits();
        SearchHit[] searchHits = hits.getHits();
        return searchHits;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

测试方法:

@Test
public void searchDoc(){
    int page =1;
    int size =10;
    System.out.println("==============================不指定索引,查询一个字段===============================================");
    SearchHit[] hits = commodityServiceImpl.searchDoc(null, "春晚", (page-1)*size, size, "title");
    for(SearchHit hit:hits){
        log.info("索引:{},value:{}",hit.getIndex(),JSONObject.toJSONString(hit.getSourceAsString()));
    }
    System.out.println("\n\n==============================不指定索引,查询多个字段===============================================");
    hits = commodityServiceImpl.searchDoc(null, "春晚", (page-1)*size, size, "title","content");
    for(SearchHit hit:hits){
        log.info("索引:{},value:{}",hit.getIndex(),JSONObject.toJSONString(hit.getSourceAsString()));
    }
    System.out.println("\n\n==============================指定索引,查询一个字段===============================================");
    hits = commodityServiceImpl.searchDoc(Article2.class, "春晚", (page-1)*size, size, "title");
    for(SearchHit hit:hits){
        log.info("索引:{},value:{}",hit.getIndex(),JSONObject.toJSONString(hit.getSourceAsString()));
    }
    System.out.println("\n\n==============================指定索引,查询多个字段===============================================");
    hits = commodityServiceImpl.searchDoc(Article2.class, "春晚", (page-1)*size, size, "title","content");
    for(SearchHit hit:hits){
        log.info("索引:{},value:{}",hit.getIndex(),JSONObject.toJSONString(hit.getSourceAsString()));
    }
}

输出:

==============================不指定索引,查询一个字段===============================================
2019-09-21 15:16:55.053  INFO 228236 --- [           main] c.z.e.service.impl.ServiceImpl           : fieldName.length:1,fieldName:[title]
2019-09-21 15:16:58.136  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article,value:"{\"content\":\"风住尘香花已尽,日晚倦梳头。物是人非事事休,欲语泪先流。\\n闻说双溪春尚好,也拟泛轻舟。只恐双溪舴艋舟,载不动许多愁。测试寻寻觅觅咏梅.测试咏梅\",\"create_time\":1568997067851,\"title\":\"武陵春·春晚\"}"
2019-09-21 15:16:58.136  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article2,value:"{\"content\":\"风住尘香花已尽,日晚倦梳头。物是人非事事休,欲语泪先流。\\n闻说双溪春尚好,也拟泛轻舟。只恐双溪舴艋舟,载不动许多愁。测试寻寻觅觅咏梅.测试咏梅\",\"create_time\":1569050046334,\"title\":\"武陵春·春晚\"}"


==============================不指定索引,查询多个字段===============================================
2019-09-21 15:16:58.136  INFO 228236 --- [           main] c.z.e.service.impl.ServiceImpl           : fieldName.length:2,fieldName:[title, content]
2019-09-21 15:16:58.147  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article,value:"{\"content\":\"风住尘香花已尽,日晚倦梳头。物是人非事事休,欲语泪先流。\\n闻说双溪春尚好,也拟泛轻舟。只恐双溪舴艋舟,载不动许多愁。测试寻寻觅觅咏梅.测试咏梅\",\"create_time\":1568997067851,\"title\":\"武陵春·春晚\"}"
2019-09-21 15:16:58.147  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article2,value:"{\"content\":\"风住尘香花已尽,日晚倦梳头。物是人非事事休,欲语泪先流。\\n闻说双溪春尚好,也拟泛轻舟。只恐双溪舴艋舟,载不动许多愁。测试寻寻觅觅咏梅.测试咏梅\",\"create_time\":1569050046334,\"title\":\"武陵春·春晚\"}"
2019-09-21 15:16:58.147  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article,value:"{\"content\":\"寻寻觅觅,冷冷清清,凄凄惨惨戚戚。乍暖还寒时候,最难将息。三杯两盏淡酒,怎敌他、晚来风急?雁过也,正伤心,却是旧时相识。\\n满地黄花堆积,憔悴损,如今有谁堪摘?守着窗儿,独自怎生得黑?梧桐更兼细雨,到黄昏、点点滴滴。这次第,怎一个愁字了得!(守着窗儿 一作:守著窗儿)测试春晚\",\"create_time\":1568997067851,\"title\":\"声声慢·寻寻觅觅\"}"
2019-09-21 15:16:58.148  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article2,value:"{\"content\":\"寻寻觅觅,冷冷清清,凄凄惨惨戚戚。乍暖还寒时候,最难将息。三杯两盏淡酒,怎敌他、晚来风急?雁过也,正伤心,却是旧时相识。\\n满地黄花堆积,憔悴损,如今有谁堪摘?守着窗儿,独自怎生得黑?梧桐更兼细雨,到黄昏、点点滴滴。这次第,怎一个愁字了得!(守着窗儿 一作:守著窗儿)测试春晚\",\"create_time\":1569050046334,\"title\":\"声声慢·寻寻觅觅\"}"


==============================指定索引,查询一个字段===============================================
2019-09-21 15:16:58.149  INFO 228236 --- [           main] c.z.e.service.impl.ServiceImpl           : fieldName.length:1,fieldName:[title]
2019-09-21 15:16:58.154  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article2,value:"{\"content\":\"风住尘香花已尽,日晚倦梳头。物是人非事事休,欲语泪先流。\\n闻说双溪春尚好,也拟泛轻舟。只恐双溪舴艋舟,载不动许多愁。测试寻寻觅觅咏梅.测试咏梅\",\"create_time\":1569050046334,\"title\":\"武陵春·春晚\"}"


==============================指定索引,查询多个字段===============================================
2019-09-21 15:16:58.154  INFO 228236 --- [           main] c.z.e.service.impl.ServiceImpl           : fieldName.length:2,fieldName:[title, content]
2019-09-21 15:16:58.158  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article2,value:"{\"content\":\"风住尘香花已尽,日晚倦梳头。物是人非事事休,欲语泪先流。\\n闻说双溪春尚好,也拟泛轻舟。只恐双溪舴艋舟,载不动许多愁。测试寻寻觅觅咏梅.测试咏梅\",\"create_time\":1569050046334,\"title\":\"武陵春·春晚\"}"
2019-09-21 15:16:58.158  INFO 228236 --- [           main] c.z.e.ElasticsearchDemoApplicationTests  : 索引:article2,value:"{\"content\":\"寻寻觅觅,冷冷清清,凄凄惨惨戚戚。乍暖还寒时候,最难将息。三杯两盏淡酒,怎敌他、晚来风急?雁过也,正伤心,却是旧时相识。\\n满地黄花堆积,憔悴损,如今有谁堪摘?守着窗儿,独自怎生得黑?梧桐更兼细雨,到黄昏、点点滴滴。这次第,怎一个愁字了得!(守着窗儿 一作:守著窗儿)测试春晚\",\"create_time\":1569050046334,\"title\":\"声声慢·寻寻觅觅\"}"

从日志中我们能发现,没有置顶索引的查询会从系统所有索引中一起查询内容,查询的字段也可以指定匹配多个字段来查询。很简单。

我的DEMO项目下载:https://gitee.com/zhuhongliang/soringboot_elastic_search_732.git

要发表评论,您必须先登录