码迷,mamicode.com
首页 > Windows程序 > 详细

ElasticSearch GetApi

时间:2020-05-14 20:59:53      阅读:91      评论:0      收藏:0      [点我收藏+]

标签:reference   etc   type   sage   failure   ide   需要   response   映射   

 直接使用文档中的GET请求是可以获取数据的,但在客户都里要指定查询的字段incloudes,否则查询出的数据是空

/**
 * @author wjc
 * @description
 * @date 2020/5/9
 */
@Component
public class GetApi {

    @Autowired
    private RestHighLevelClient highLevelClient;
    @Autowired
    @Qualifier("getListener")
    private ActionListener listener;


    public String get(String index, String id){
        String message = null;
        GetRequest request = new GetRequest(index,  id);
//        GetRequest request = new GetRequest(index,  id);
        //禁用源检索,默认启用
        request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
        //为特定字段配置源包含
        String[] includes = new String[]{"user", "message", "*Date"};
        //为特定字段配置源排除
        String[] excludes = Strings.EMPTY_ARRAY;
//        String[] excludes = new String[]{"message"};
        FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
        request.fetchSourceContext(fetchSourceContext);
//        request.routing("routing");
//        request.preference("preference");
//
//        //为特定的存储字段配置检索(要求字段在映射中单独存储)
//        request.storedFields("message");
//        //将realtime标记设置为false(默认为true)
//        request.realtime(false);
//        //在检索文档之前执行刷新(默认为false)
//        request.refresh(true);
//        request.version(2);
//        request.versionType(VersionType.EXTERNAL);

        try {
            GetResponse getResponse = highLevelClient.get(request, RequestOptions.DEFAULT);
            highLevelClient.getAsync(request, RequestOptions.DEFAULT, listener);
            //检索消息存储字段(要求字段在映射中单独存储)
//            message = getResponse.getField("message").getValue();
            message = getResponse.getSourceAsString();
        }catch (IOException e){

        }//当对不存在的索引执行get请求时,响应有404状态代码,抛出ElasticsearchException,需要按如下方式处理
        catch (ElasticsearchException e) {
            if (e.status() == RestStatus.NOT_FOUND) {
                //处理因索引不存在而引发的异常
            }
            //如果已请求特定的文档版本,而现有文档具有不同的版本号,则会引发版本冲突
            if (e.status() == RestStatus.CONFLICT) {

            }
        }

        return message;
    }

}

 

Listener

@Slf4j
@Configuration
public class ESGetListener {
    @Bean("getListener")
    public ActionListener listener(){
        ActionListener<GetResponse> listener = new ActionListener<GetResponse>() {
            //在执行成功完成时调用。
            @Override
            public void onResponse(GetResponse getResponse) {
                String index = getResponse.getIndex();
                String type = getResponse.getType();
                String id = getResponse.getId();
                if (getResponse.isExists()) {
                    long version = getResponse.getVersion();
                    //以字符串的形式检索文档
                    String sourceAsString = getResponse.getSourceAsString();
                    //以Map<String, Object>的形式检索文档
                    Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
                    //以字节[]的形式检索文档
                    byte[] sourceAsBytes = getResponse.getSourceAsBytes();
                    log.info("jsonString:  " + sourceAsString);
                    log.info("map:  " + JSON.toJSONString(sourceAsMap));
                    log.info("byte[]:   " + new String(sourceAsBytes));
                } else {
                    //处理没有找到文档的场景。注意,虽然返回的响应有404状态代码,但是返回的是有效的GetResponse,
                    // 而不是抛出异常。这种响应不包含任何源文档,其isExists方法返回false。
                }
            }

            //当整个GetRequest失败时调用。
            @Override
            public void onFailure(Exception e) {

            }
        };
        return listener;
    }
}

 

Service

@Service
public class ElasticSearchService {

    @Autowired
    private GetApi getApi;

    

    public String get(String index, String id){
        if(StringUtils.isBlank(index) || StringUtils.isBlank(id)){
            return "";
        }
        return getApi.get(index, id);
    }
}

Controller

@RestController
public class ElasticSearchController {
    @Autowired
    private ElasticSearchService elasticSearchService;

    

    @PostMapping("/es/get/get")
    public String get(String index, String id){
        return elasticSearchService.get(index, id);
    }
}

 

请求示例

技术图片

 

ElasticSearch GetApi

标签:reference   etc   type   sage   failure   ide   需要   response   映射   

原文地址:https://www.cnblogs.com/gqymy/p/12891326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!