ElasticSearch GetApi
直接使用文档中的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);
}
}请求示例

相关推荐
newbornzhao 2020-09-14
做对一件事很重要 2020-09-07
renjinlong 2020-09-03
明瞳 2020-08-19
李玉志 2020-08-19
mengyue 2020-08-07
molong0 2020-08-06
AFei00 2020-08-03
molong0 2020-08-03
wenwentana 2020-08-03
YYDU 2020-08-03
另外一部分,则需要先做聚类、分类处理,将聚合出的分类结果存入ES集群的聚类索引中。数据处理层的聚合结果存入ES中的指定索引,同时将每个聚合主题相关的数据存入每个document下面的某个field下。
sifeimeng 2020-08-03
心丨悦 2020-08-03
liangwenrong 2020-07-31
sifeimeng 2020-08-01
mengyue 2020-07-30
tigercn 2020-07-29
IceStreamLab 2020-07-29