美文网首页学习
SpringBoot2.0填坑(二):elastic searc

SpringBoot2.0填坑(二):elastic searc

作者: IT实战联盟Lin | 来源:发表于2018-12-25 16:49 被阅读30次

    执行代码

    @GetMapping("searchAll")
        public ESDatas<DiskFile> searchAll(){
            DiskFile diDiskFileDto = new DiskFile();
            diDiskFileDto.setDiskFileId("2");
            diDiskFileDto.setName("张三");
    
            DiskFile diDiskFileDto1 = new DiskFile();
            diDiskFileDto1.setDiskFileId("1");
            diDiskFileDto1.setName("李四");
    
            List<DiskFile> list = new ArrayList<>();
            list.add(diDiskFileDto);
            list.add(diDiskFileDto1);
            esUtil.addOrUpdateDocuments("diskFile",list);
    
            ESDatas<DiskFile> esDatas = esUtil.searchAll("diskFile",DiskFile.class);
    
            return esDatas;
        }
    

    页面报错

    Whitelabel Error Page
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    
    Tue Dec 25 16:38:25 CST 2018
    There was an unexpected error (type=Internal Server Error, status=500).
    {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"diskFile","index_uuid":"_na_","index":"diskFile"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"diskFile","index_uuid":"_na_","index":"diskFile"},"status":404}
    

    前言

    不算是SpringBoot 2.0 的坑,只是在开发项目的时候遇到的问题总结。

    控制台报错

    2018-12-25 16:38:25.794 ERROR 5357 --- [nio-8089-exec-1] o.a.c.c.C.[.[.[.[dispatcherServlet]      : Servlet.service() for servlet [dispatcherServlet] in context with path [/Demo] threw exception [Request processing failed; nested exception is org.frameworkset.elasticsearch.ElasticSearchException: {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"diskFile","index_uuid":"_na_","index":"diskFile"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"diskFile","index_uuid":"_na_","index":"diskFile"},"status":404}] with root cause
    
    org.frameworkset.elasticsearch.ElasticSearchException: {"error":{"root_cause":[{"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"diskFile","index_uuid":"_na_","index":"diskFile"}],"type":"index_not_found_exception","reason":"no such index","resource.type":"index_or_alias","resource.id":"diskFile","index_uuid":"_na_","index":"diskFile"},"status":404}
        at org.frameworkset.elasticsearch.handler.BaseExceptionResponseHandler.handleException(BaseExceptionResponseHandler.java:44) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.frameworkset.elasticsearch.handler.ElasticSearchResponseHandler.handleResponse(ElasticSearchResponseHandler.java:61) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.frameworkset.elasticsearch.handler.ElasticSearchResponseHandler.handleResponse(ElasticSearchResponseHandler.java:16) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:223) ~[httpclient-4.5.6.jar:4.5.6]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:165) ~[httpclient-4.5.6.jar:4.5.6]
        at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:140) ~[httpclient-4.5.6.jar:4.5.6]
        at org.frameworkset.spi.remote.http.HttpRequestUtil.sendBody(HttpRequestUtil.java:1519) ~[bboss-http-5.0.8.jar:na]
        at org.frameworkset.spi.remote.http.HttpRequestUtil.sendJsonBody(HttpRequestUtil.java:1477) ~[bboss-http-5.0.8.jar:na]
        at org.frameworkset.elasticsearch.client.RestSeachExecutor.executeRequest(RestSeachExecutor.java:161) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.frameworkset.elasticsearch.client.ElasticSearchRestClient.executeRequest(ElasticSearchRestClient.java:762) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.frameworkset.elasticsearch.client.ElasticSearchRestClient.executeRequest(ElasticSearchRestClient.java:726) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.frameworkset.elasticsearch.client.RestClientUtil.scroll(RestClientUtil.java:1969) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.frameworkset.elasticsearch.client.RestClientUtil.searchAll(RestClientUtil.java:1494) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.frameworkset.elasticsearch.client.RestClientUtil.searchAll(RestClientUtil.java:1477) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at org.frameworkset.elasticsearch.client.RestClientUtil.searchAll(RestClientUtil.java:1463) ~[bboss-elasticsearch-rest-5.1.5.jar:na]
        at com.itunio
    
    

    解决方案

    • 修改为
    @GetMapping("searchAll")
        public ESDatas<DiskFile> searchAll(){
            DiskFile diDiskFileDto = new DiskFile();
            diDiskFileDto.setDiskFileId("2");
            diDiskFileDto.setName("张三");
    
            DiskFile diDiskFileDto1 = new DiskFile();
            diDiskFileDto1.setDiskFileId("1");
            diDiskFileDto1.setName("李四");
    
            List<DiskFile> list = new ArrayList<>();
            list.add(diDiskFileDto);
            list.add(diDiskFileDto1);
            esUtil.addOrUpdateDocuments("disk_file",list);
    
            ESDatas<DiskFile> esDatas = esUtil.searchAll("disk_file",DiskFile.class);
    
            return esDatas;
        }
    

    结果-成功

    2.pic_hd.jpg

    总结

    ES在存储和查询对象时候,命名规则一定要注意 不要用“驼峰式” 命名,用 下划线“_”隔开。

    贡献者

    相关文章

      网友评论

        本文标题:SpringBoot2.0填坑(二):elastic searc

        本文链接:https://www.haomeiwen.com/subject/bsgflqtx.html