上一篇:Elasticsearch Java REST Client 初始化、添加索引及数据
简单查询# 简单通过id获取数据@GetMapping("get")public GetResponse get(String id) { GetResponse response = null; try { GetRequest getRequest = new GetRequest("edu-app-user", "doc", id); response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } return response; }
# GetRequest 可选方法# 禁用源检索,禁用抓取_source,默认启用。获取取所有字段数据,如果禁用请设置要获取的字段数据,不指定结果sorce值为nullrequest.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);@GetMapping("get")public GetResponse get(String id) { GetResponse response = null; try { GetRequest request = new GetRequest("edu-app-user", "doc", id); request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE); response = restHighLevelClient.get(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } return response;}
# 指定返回结果@GetMapping("get2")public GetResponse get2(String id) { GetResponse response = null; try { GetRequest request = new GetRequest("edu-app-user", "doc", id); // 结果中展示的字段 String[] includes = new String[]{"name", "age"}; // 要排除的字段,如果没有,设置为空数组 String[] excludes = Strings.EMPTY_ARRAY; FetchSourceContext fetchSourceContext = new FetchSourceContext(FetchSourceContext.FETCH_SOURCE.fetchSource(), includes, excludes); request.fetchSourceContext(fetchSourceContext); response = restHighLevelClient.get(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } return response;}
# 指定排除结果@GetMapping("get3")public GetResponse get3(String id) { GetResponse response = null; try { GetRequest request = new GetRequest("edu-app-user", "doc", id); // 结果中展示的字段,空则全部展示 String[] includes = Strings.EMPTY_ARRAY; // 要排除的字段 String[] excludes = new String[]{ "age"}; FetchSourceContext fetchSourceContext = new FetchSourceContext(FetchSourceContext.FETCH_SOURCE.fetchSource(), includes, excludes); request.fetchSourceContext(fetchSourceContext); response = restHighLevelClient.get(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } return response; }
# 异步获取数据@GetMapping("get4")public void get4(String id) { try { GetRequest request = new GetRequest("edu-app-user", "doc", id); restHighLevelClient.getAsync(request, RequestOptions.DEFAULT,new EduAppUserGetListener()); } catch (Exception e) { e.printStackTrace(); }}
# 监听类与异步添加数据结果一致,仅更换泛型即可package com.wdz.es.config.es;import org.elasticsearch.action.ActionListener;import org.elasticsearch.action.get.GetResponse;import org.elasticsearch.action.index.IndexResponse;public class EduAppUserGetListener implements ActionListener
# 判断指定数据是否存在# 结果是Boolean,为了提高效率可禁用抓取_source和禁用获取存储字段@GetMapping("get5")public boolean get5(String id) { try { GetRequest request = new GetRequest("edu-app-user", "doc", id); // 禁用源查询,默认启用,禁用后source为null // 禁用抓取_source request.fetchSourceContext(new FetchSourceContext(false)); // 禁用获取存储的字段 request.storedFields("_none_"); return restHighLevelClient.exists(request, RequestOptions.DEFAULT); } catch (IOException e) { e.printStackTrace(); } return false; }
# 异步存在查询@GetMapping("get6")public void get6(String id) { GetRequest request = new GetRequest("edu-app-user", "doc", id); // 禁用源查询,默认启用,禁用后source为null // 禁用抓取_source request.fetchSourceContext(new FetchSourceContext(false)); // 禁用获取存储的字段 request.storedFields("_none_"); restHighLevelClient.existsAsync(request, RequestOptions.DEFAULT, new ExistsAsyncListen());}
# 监听类,与查询数据和添加监听一致,更换泛型即可package com.wdz.es.config.es;import org.elasticsearch.action.ActionListener;public class ExistsAsyncListen implements ActionListener
上一篇:Elasticsearch Java REST Client 初始化、添加索引及数据