ElasticSearch GetApi
2021-03-05 16:28
标签:reference etc type sage failure ide 需要 response 映射 直接使用文档中的GET请求是可以获取数据的,但在客户都里要指定查询的字段incloudes,否则查询出的数据是空 Listener Service Controller 请求示例 ElasticSearch GetApi 标签:reference etc type sage failure ide 需要 response 映射 原文地址:https://www.cnblogs.com/gqymy/p/12891326.html/**
* @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;
}
}
@Slf4j
@Configuration
public class ESGetListener {
@Bean("getListener")
public ActionListener listener(){
ActionListener
@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);
}
}
@RestController
public class ElasticSearchController {
@Autowired
private ElasticSearchService elasticSearchService;
@PostMapping("/es/get/get")
public String get(String index, String id){
return elasticSearchService.get(index, id);
}
}
上一篇:c#中绝对路径和相对路径
下一篇:C# 反射 循环属性、字段赋值
文章标题:ElasticSearch GetApi
文章链接:http://soscw.com/index.php/essay/60528.html