1. Elasticsearch Java API有四类client连接方式

  • TransportClient
  • RestClient
  • Jest
  • Spring Data Elasticsearch

其中TransportClient和RestClient是Elasticsearch原生的api。TransportClient可以支持2.x,5.x版本,TransportClient将会在Elasticsearch 7.0弃用并在8.0中完成删除,替而代之,我们使用Java High Level REST Client,它使用HTTP请求而不是Java序列化请求。

Jest是Java社区开发的,是Elasticsearch的Java Http Rest客户端;Spring Data Elasticsearch是spring集成的Elasticsearch开发包。

建议:TransportClient将会在后面的版本中弃用,因此不推荐后续使用;而Jest由于是社区维护,所以更新有一定延迟,目前最新版对接ES6.3.1,近一个月只有四个issue,说明整体活跃度较低,因此也不推荐使用;Spring Data Elasticsearch主要是与Spring生态对接,可以在web系统中整合到Spring中使用。目前比较推荐使用官方的高阶、低阶Rest Client,官方维护,比较值得信赖。本文主要介绍RestClient,其他的只做简单概述。

2. TransportClient

这里考虑到后面版本将弃用TransportClinet,主要简单介绍TransportClient的创建、以及一些基本增删改查操作。

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>transport</artifactId>
  4. <version>6.4.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.elasticsearch</groupId>
  8. <artifactId>elasticsearch</artifactId>
  9. <version>6.4.3</version>
  10. </dependency>
  1. /**
  2. * @Author: Yang JianQiu
  3. * @Date: 2018/11/12 16:40
  4. *
  5. * transportClient将会在7.0版本上过时,并在8.0版本上移除掉,建议使用Java High Level REST Client
  6. */
  7. public class TransportClientUtils {
  8.  
  9. private TransportClient client = null;
  10.  
  11. /**
  12. * 这里使用饿汉单例模式创建TransportClient
  13. */
  14. public TransportClientUtils() {
  15. if (client == null){
  16. synchronized (TransportClientUtils.class){
  17. if (client == null){
  18. client = getClient();
  19. }
  20. }
  21. }
  22. }
  23.  
  24. public TransportClient getClient(){
  25. TransportClient client = null;
  26. try {
  27. Settings settings = Settings.builder()
  28. .put("client.transport.sniff", true)
  29. .put("cluster.name", "bigdata").build();
  30. client = new PreBuiltTransportClient(settings)
  31. .addTransportAddress(new TransportAddress(new InetSocketAddress("192.168.187.201", 9300)));
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. return client;
  36. }
  37.  
  38. public viod test(){
  39. //增,插入记录
  40. IndexResponse response = client.prepareIndex("twitter", "_doc")
  41. .setSource(json, XContentType.JSON)
  42. .get();
  43.  
  44. //根据Id查询
  45. GetResponse response = client.prepareGet("twitter", "_doc", 1).get();
  46.  
  47. //根据Id删除
  48. DeleteResponse response = client.prepareDelete("twitter", "_doc", 1).get();
  49.  
  50. //根据Id更新对应的字段
  51. UpdateRequest updateRequest1 = new UpdateRequest("twitter", "_doc", "NpEWCGcBi36MQkKOSdf3")
  52. .doc(jsonBuilder()
  53. .startObject()
  54. .field("user", "tom")
  55. .endObject()
  56. );
  57. client.update(updateRequest1).get();
  58.  
  59. //另外还有批处理API、search负责查询API、Aggregate聚合API...
  60. }
  61.  
  62. }

具体的TransportClient的其他API应用可以参考Elasticsearch的TransportClient API doc:

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/index.html

3. Rest Client

后面的elasticSearch版本将主要使用Rest Client操作数据。

Rest Client分为Java Low REST Client和Java High Level REST Client。

maven的依赖包:

  1. <dependency>
  2. <groupId>org.elasticsearch.client</groupId>
  3. <artifactId>elasticsearch-rest-high-level-client</artifactId>
  4. <version>6.4.3</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>org.elasticsearch.client</groupId>
  8. <artifactId>elasticsearch-rest-client</artifactId>
  9. <version>6.4.3</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.elasticsearch</groupId>
  13. <artifactId>elasticsearch</artifactId>
  14. <version>6.4.3</version>
  15. </dependency>

RestHighLevelClient与RestClient的创建:

  1. /**
  2. * @author: Swordfall Yeung
  3. * @date:
  4. * @desc:
  5. */
  6. public class RestClientUtils {
  7.  
  8. /**
  9. * 高阶Rest Client
  10. */
  11. private RestHighLevelClient client = null;
  12. /**
  13. * 低阶Rest Client
  14. */
  15. private RestClient restClient = null;
  16.  
  17. /**
  18. * 这里使用饿汉单例模式创建RestHighLevelClient
  19. */
  20. public RestClientUtils() {
  21. if (client == null) {
  22. synchronized (RestHighLevelClient.class) {
  23. if (client == null) {
  24. client = getClient();
  25. }
  26. }
  27. }
  28. }
  29.  
  30. private RestHighLevelClient getClient() {
  31. RestHighLevelClient client = null;
  32.  
  33. try {
  34. client = new RestHighLevelClient(
  35. RestClient.builder(
  36. new HttpHost("192.168.187.201", 9300, "http")
  37. )
  38. );
  39. } catch (Exception e) {
  40. e.printStackTrace();
  41. }
  42. return client;
  43. }
  44.  
  45. private RestClient getRestClient() {
  46. RestClient client = null;
  47.  
  48. try {
  49. client = RestClient.builder(
  50. new HttpHost("192.168.187.201", 9300, "http")
  51. ).build();
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. return client;
  56. }
  57.  
  58. public void closeClient() {
  59. try {
  60. if (client != null) {
  61. client.close();
  62. }
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. }
  66. }
  67.  
  68. /**
  69. * document API 主要是些简单的增删改查操作
  70. */
  71. public void documentAPI() {
  72. //...
  73. }
  74.  
  75. /**
  76. * Search API 主要是些复杂查询操作
  77. */
  78. public void searchAPI() {
  79. //...
  80. }
  81. }

RestHighLevelAPI分为Document APIs、Search APIs、Miscellaneous APIs、Indices APIs、Cluster APIs...等等,这里主要介绍常用的Document APIs和Search APIs,其余的APIs可以参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

3.1 Document APIs

Document APIs主要涉及些增删改查等操作,包括Single document APIs单条操作和Multi-document APIs批量操作。Document APIs均可以设置可选参数,实现同步、异步,也均可抛出异常,这里以Index API为例,后面的不再简述。

Index API介绍:

  1. /**
  2. * 增,插入记录
  3. * 插入操作有四种方式,分同步异步操作,可选参数设置,结果返回IndexResponse,抛出异常
  4. * @throws Exception
  5. */
  6. public void index() throws Exception{
  7.  
  8. //第一种方式: String
  9. IndexRequest request = new IndexRequest("posts", "doc", "1");
  10. String jsonString = "{" +
  11. "\"user\":\"kimchy\"," +
  12. "\"postDate\":\"2013-01-30\"," +
  13. "\"message\":\"trying out Elasticsearch\"" +
  14. "}";
  15. request.source(jsonString, XContentType.JSON);
  16.  
  17. //第二种方式: Map
  18. Map<String, Object> jsonMap = new HashMap<>();
  19. jsonMap.put("user", "kimchy");
  20. jsonMap.put("postDate", new Date());
  21. jsonMap.put("message", "trying out Elasticsearch");
  22. IndexRequest indexRequest = new IndexRequest("posts", "doc", "1").source(jsonMap);
  23.  
  24. //第三种方式: XContentBuilder automatically converted to JSON
  25. XContentBuilder builder = XContentFactory.jsonBuilder();
  26. builder.startObject();
  27. {
  28. builder.field("user", "kimchy");
  29. builder.timeField("postDate" , new Date());
  30. builder.field("message", "trying out Elasticsearch");
  31. }
  32. builder.endObject();
  33. IndexRequest indexRequest1 = new IndexRequest("posts", "doc", "1")
  34. .source(builder);
  35.  
  36. //第四种方式: source -> key-pairs
  37. IndexRequest indexRequest2 = new IndexRequest("posts", "doc", "1")
  38. .source("user", "kimchy",
  39. "postDate", new Date(),
  40. "message", "trying out Elasticsearch"
  41. );
  42.  
  43. //可选的参数设置
  44. request.routing("routing");
  45. request.parent("parent");
  46. request.timeout(TimeValue.timeValueSeconds(1));
  47. request.timeout("1s");
  48. request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);
  49. request.setRefreshPolicy("wait_for");
  50. request.version(2);
  51. request.versionType(VersionType.EXTERNAL);
  52. request.opType(DocWriteRequest.OpType.CREATE);
  53. request.opType("create");
  54. request.setPipeline("pipeline");
  55.  
  56. //同步执行
  57. IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
  58.  
  59. //异步执行
  60. ActionListener<IndexResponse> listener = new ActionListener<IndexResponse>() {
  61. @Override
  62. public void onResponse(IndexResponse indexResponse) {
  63.  
  64. }
  65.  
  66. @Override
  67. public void onFailure(Exception e) {
  68.  
  69. }
  70. };
  71. client.indexAsync(request, RequestOptions.DEFAULT, listener);
  72.  
  73. //Index Response
  74. String index = indexResponse.getIndex();
  75. String type = indexResponse.getType();
  76. String id = indexResponse.getId();
  77. long version = indexResponse.getVersion();
  78.  
  79. //抛出异常
  80. IndexRequest request1 = new IndexRequest("posts", "doc", "1")
  81. .source("field", "value")
  82. .version(1);
  83. try {
  84. IndexResponse response = client.index(request, RequestOptions.DEFAULT);
  85. } catch (ElasticsearchException e) {
  86. if (e.status() == RestStatus.CONFLICT){
  87.  
  88. }
  89. }
  90. }

Get API介绍

  1. /**
  2. * 根据 id 获取数据
  3. * @throws Exception
  4. */
  5. public void get() throws Exception{
  6. GetRequest request = new GetRequest("posts", "doc", "1");
  7.  
  8. //可选参数设置
  9. request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
  10. String[] includes = new String[]{"message", "*Date"};
  11. String[] excludes = Strings.EMPTY_ARRAY;
  12. FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
  13. request.fetchSourceContext(fetchSourceContext);
  14.  
  15. //同步执行
  16. GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
  17.  
  18. //异步执行 listener的写法参照Index的异步执行的listener
  19. client.getAsync(request, RequestOptions.DEFAULT, listener);
  20.  
  21. //Get Response 获取信息
  22.  
  23. //抛出异常
  24. }

Exists API介绍

  1. /**
  2. * 是否存在
  3. * @throws Exception
  4. */
  5. public void exists() throws Exception{
  6. GetRequest getRequest = new GetRequest("posts", "doc", "1");
  7. getRequest.fetchSourceContext(new FetchSourceContext(false));
  8. getRequest.storedFields("_none_");
  9.  
  10. //同步执行
  11. boolean exists = client.exists(getRequest, RequestOptions.DEFAULT);
  12.  
  13. //异步执行 listener的写法参照Index的
  14. client.existsAsync(getRequest, RequestOptions.DEFAULT, listener);
  15.  
  16. if (exists){
  17. System.out.println("存在");
  18. }else {
  19. System.out.println("不存在");
  20. }
  21. }

Delete API介绍

  1. /**
  2. * 根据id删除
  3. * @throws Exception
  4. */
  5. public void delete() throws Exception{
  6. DeleteRequest request = new DeleteRequest("posts", "doc", "1");
  7.  
  8. //同步执行
  9. DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT);
  10.  
  11. //异步执行 listener参照index的
  12. client.deleteAsync(request, RequestOptions.DEFAULT, listener);
  13.  
  14. //Delete Response
  15. String index = deleteResponse.getIndex();
  16.  
  17. // document was not found
  18. if (deleteResponse.getResult() == DocWriteResponse.Result.NOT_FOUND) {
  19.  
  20. }
  21.  
  22. //抛出异常
  23.  
  24. }

Update API介绍

  1. /**
  2. * 根据id更新
  3. * @throws Exception
  4. */
  5. public void update() throws Exception{
  6. UpdateRequest request = new UpdateRequest("posts", "doc", "1");
  7. Map<String, Object> parameters = Collections.singletonMap("count", 4);
  8.  
  9. //第一种方式:inline script
  10. Script inline = new Script(ScriptType.INLINE, "painless", "ctx._source.field += params.count", parameters);
  11. request.script(inline);
  12.  
  13. //第二种方式:stored script
  14. Script stored = new Script(ScriptType.STORED, null, "increment-field", parameters);
  15. request.script(stored);
  16.  
  17. //第三种方式:partial document String
  18. String jsonString = "{" +
  19. "\"updated\":\"2017-01-01\"," +
  20. "\"reason\":\"daily update\"" +
  21. "}";
  22. request.doc(jsonString, XContentType.JSON);
  23.  
  24. //第四种方式:partial document Map
  25. Map<String, Object> jsonMap = new HashMap<>();
  26. jsonMap.put("updated", new Date());
  27. jsonMap.put("reason", "daily update");
  28. request.doc(jsonMap);
  29.  
  30. //第五种方式:partial document XContentBuilder
  31. XContentBuilder builder = XContentFactory.jsonBuilder();
  32. builder.startObject();
  33. {
  34. builder.timeField("updated", new Date());
  35. builder.field("reason", "daily update");
  36. }
  37. builder.endObject();
  38. request.doc(builder);
  39.  
  40. //第六种方式:partial document Object key-pairs
  41. request.doc("updated", new Date(),
  42. "reason", "daily update");
  43.  
  44. //upserts
  45. String jsonString1 = "{\"created\":\"2017-01-01\"}";
  46. request.upsert(jsonString1, XContentType.JSON);
  47.  
  48. //同步执行
  49. UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT);
  50.  
  51. //异步执行, listener创建参考index的
  52. client.updateAsync(request, RequestOptions.DEFAULT, listener);
  53.  
  54. //update Response
  55. GetResult result = updateResponse.getGetResult();
  56. if (result.isExists()) {
  57. String sourceAsString = result.sourceAsString();
  58. Map<String, Object> sourceAsMap = result.sourceAsMap();
  59. byte[] sourceAsBytes = result.source();
  60. } else {
  61.  
  62. }
  63.  
  64. //抛出异常
  65.  
  66. }

Bulk API介绍

  1. /**
  2. * 批量处理
  3. * @throws Exception
  4. */
  5. public void bulk() throws Exception{
  6. BulkRequest request = new BulkRequest();
  7.  
  8. //Other
  9. request.add(new DeleteRequest("posts", "doc", "3"));
  10. request.add(new UpdateRequest("posts", "doc", "2")
  11. .doc(XContentType.JSON, "other", "test"));
  12. request.add(new IndexRequest("posts", "doc", "4")
  13. .source(XContentType.JSON, "field", "baz"));
  14.  
  15. //同步执行
  16. BulkResponse bulkResponses = client.bulk(request, RequestOptions.DEFAULT);
  17.  
  18. //异步执行
  19. ActionListener<BulkResponse> listener = new ActionListener<BulkResponse>() {
  20. @Override
  21. public void onResponse(BulkResponse bulkResponse) {
  22.  
  23. }
  24.  
  25. @Override
  26. public void onFailure(Exception e) {
  27.  
  28. }
  29. };
  30. client.bulkAsync(request, RequestOptions.DEFAULT, listener);
  31.  
  32. //Bulk Response 批处理结果
  33. for (BulkItemResponse bulkItemResponse: bulkResponses){
  34. DocWriteResponse itemResponse = bulkItemResponse.getResponse();
  35.  
  36. if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX
  37. || bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) {
  38. IndexResponse indexResponse = (IndexResponse) itemResponse;
  39.  
  40. } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) {
  41. UpdateResponse updateResponse = (UpdateResponse) itemResponse;
  42.  
  43. } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) {
  44. DeleteResponse deleteResponse = (DeleteResponse) itemResponse;
  45. }
  46. }
  47.  
  48. for (BulkItemResponse bulkItemResponse : bulkResponses) {
  49. if (bulkItemResponse.isFailed()) {
  50. BulkItemResponse.Failure failure = bulkItemResponse.getFailure();
  51.  
  52. }
  53. }
  54.  
  55. //Bulk Processor 自定义批处理器
  56. BulkProcessor.Listener listener1 = new BulkProcessor.Listener() {
  57. @Override
  58. public void beforeBulk(long l, BulkRequest bulkRequest) {
  59.  
  60. }
  61.  
  62. @Override
  63. public void afterBulk(long l, BulkRequest bulkRequest, BulkResponse bulkResponse) {
  64.  
  65. }
  66.  
  67. @Override
  68. public void afterBulk(long l, BulkRequest bulkRequest, Throwable throwable) {
  69.  
  70. }
  71. };
  72.  
  73. BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer =
  74. (request1, bulkListener) -> client.bulkAsync(request1, RequestOptions.DEFAULT, bulkListener);
  75. BulkProcessor bulkProcessor = BulkProcessor.builder(bulkConsumer, listener1).build();
  76.  
  77. BiConsumer<BulkRequest, ActionListener<BulkResponse>> bulkConsumer1 =
  78. (request2, bulkListener) -> client.bulkAsync(request2, RequestOptions.DEFAULT, bulkListener);
  79. BulkProcessor.Builder builder = BulkProcessor.builder(bulkConsumer1, listener1);
  80. builder.setBulkActions(500);
  81. builder.setBulkSize(new ByteSizeValue(1L, ByteSizeUnit.MB));
  82. builder.setConcurrentRequests(0);
  83. builder.setFlushInterval(TimeValue.timeValueSeconds(10L));
  84. builder.setBackoffPolicy(BackoffPolicy
  85. .constantBackoff(TimeValue.timeValueSeconds(1L), 3));
  86.  
  87. //Once the BulkProcessor is created requests can be added to it:
  88. IndexRequest one = new IndexRequest("posts", "doc", "1").
  89. source(XContentType.JSON, "title",
  90. "In which order are my Elasticsearch queries executed?");
  91. IndexRequest two = new IndexRequest("posts", "doc", "2")
  92. .source(XContentType.JSON, "title",
  93. "Current status and upcoming changes in Elasticsearch");
  94. IndexRequest three = new IndexRequest("posts", "doc", "3")
  95. .source(XContentType.JSON, "title",
  96. "The Future of Federated Search in Elasticsearch");
  97. bulkProcessor.add(one);
  98. bulkProcessor.add(two);
  99. bulkProcessor.add(three);
  100.  
  101. boolean terminated = bulkProcessor.awaitClose(30L, TimeUnit.SECONDS);
  102. }

Multi-Get API介绍

  1. /**
  2. * 根据id批量获取数据
  3. * @throws Exception
  4. */
  5. public void multiGet() throws Exception{
  6. MultiGetRequest request = new MultiGetRequest();
  7. request.add(new MultiGetRequest.Item("index","type","example_id"));
  8. request.add(new MultiGetRequest.Item("index", "type", "another_id"));
  9.  
  10. //optional arguments
  11. request.add(new MultiGetRequest.Item("index", "type", "example_id") .fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE));
  12.  
  13. //同步执行
  14. MultiGetResponse responses = client.mget(request, RequestOptions.DEFAULT);
  15.  
  16. //异步执行 listener参考Index的
  17. client.mgetAsync(request, RequestOptions.DEFAULT, listener);
  18.  
  19. //Multi Get Response
  20. MultiGetItemResponse firstItem = response.getResponses()[0];
  21. GetResponse firstGet = firstItem.getResponse();
  22. if (firstGet.isExists()) {
  23.  
  24. }
  25. }

3.2 Search APIs

  1. public void search() throws Exception{
  2. //match all query 查询所有数据
  3. SearchRequest searchRequest = new SearchRequest();
  4. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  5. searchSourceBuilder.query(QueryBuilders.matchAllQuery());
  6. searchRequest.source(searchSourceBuilder);
  7.  
  8. //使用SearchSourceBuilder查询指定字段
  9. SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
  10. sourceBuilder.query(QueryBuilders.termQuery("user", "kimchy"));
  11. sourceBuilder.from(0);
  12. sourceBuilder.size(5);
  13. sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
  14. SearchRequest searchRequest2 = new SearchRequest();
  15. //index 数据库
  16. searchRequest2.indices("posts");
  17. searchRequest2.source(sourceBuilder);
  18.  
  19. //Building queries
  20. //One way, QueryBuilder can be created using its constructor 使用QueryBuilder的构造函数
  21. MatchQueryBuilder matchQueryBuilder = new MatchQueryBuilder("user", "kimchy");
  22. matchQueryBuilder.fuzziness(Fuzziness.AUTO);
  23. matchQueryBuilder.prefixLength(3);
  24. matchQueryBuilder.maxExpansions(10);
  25. //Two way, QueryBuilder objects can also be created using the QueryBuilders utility class. 直接使用matchQuery
  26. QueryBuilder matchQueryBuilder1 = matchQuery("user", "kimchy")
  27. .fuzziness(Fuzziness.AUTO)
  28. .prefixLength(3)
  29. .maxExpansions(10);
  30.  
  31. searchSourceBuilder.query(matchQueryBuilder1);
  32.  
  33. //Specifying Sorting 指定排序
  34. sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
  35. sourceBuilder.sort(new FieldSortBuilder("_uid").order(SortOrder.ASC));
  36.  
  37. //Source filtering, turn off _source retrieval completely
  38. sourceBuilder.fetchSource(false);
  39. //an array of one or more wildcard patterns to control which fields get included or excluded in a more fine grained way
  40. String[] includeFields = new String[] {"title", "user", "innerObject.*"};
  41. String[] excludeFields = new String[] {"_type"};
  42. sourceBuilder.fetchSource(includeFields, excludeFields);
  43.  
  44. //Requesting Aggregations
  45. SearchSourceBuilder searchSourceBuilder2 = new SearchSourceBuilder();
  46. TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company")
  47. .field("company.keyword");
  48. aggregation.subAggregation(AggregationBuilders.avg("average_age")
  49. .field("age"));
  50. searchSourceBuilder2.aggregation(aggregation);
  51.  
  52. //Requesting Suggestions
  53. SearchSourceBuilder searchSourceBuilder3 = new SearchSourceBuilder();
  54. SuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("kmichy");
  55. SuggestBuilder suggestBuilder = new SuggestBuilder();
  56. suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder);
  57. searchSourceBuilder3.suggest(suggestBuilder);
  58.  
  59. //同步执行
  60. SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
  61.  
  62. //异步执行 listener参考index的
  63. client.searchAsync(searchRequest, RequestOptions.DEFAULT, listener);
  64.  
  65. //SearchResponse
  66. RestStatus status = searchResponse.status();
  67. for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
  68. // failures should be handled here
  69. }
  70.  
  71. //Retrieving SearchHits 获取结果数据
  72. SearchHits hits = searchResponse.getHits();
  73. long totalHits = hits.getTotalHits();
  74. float maxScore = hits.getMaxScore();
  75. SearchHit[] searchHits = hits.getHits();
  76. for (SearchHit hit : searchHits) {
  77. // do something with the SearchHit
  78. String index = hit.getIndex();
  79. String type = hit.getType();
  80. String id = hit.getId();
  81. float score = hit.getScore();
  82.  
  83. String sourceAsString = hit.getSourceAsString();
  84. Map<String, Object> sourceAsMap = hit.getSourceAsMap();
  85. String documentTitle = (String) sourceAsMap.get("title");
  86. List<Object> users = (List<Object>) sourceAsMap.get("user");
  87. Map<String, Object> innerObject =
  88. (Map<String, Object>) sourceAsMap.get("innerObject");
  89. }
  90.  
  91. //Retrieving Aggregations
  92. Aggregations aggregations = searchResponse.getAggregations();
  93. Terms byCompanyAggregation = aggregations.get("by_company");
  94. Terms.Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic");
  95. Avg averageAge = elasticBucket.getAggregations().get("average_age");
  96. double avg = averageAge.getValue();
  97.  
  98. Range range = aggregations.get("by_company");
  99. Map<String, Aggregation> aggregationMap = aggregations.getAsMap();
  100. Terms companyAggregation = (Terms) aggregationMap.get("by_company");
  101.  
  102. List<Aggregation> aggregationList = aggregations.asList();
  103. for (Aggregation agg : aggregations) {
  104. String type = agg.getType();
  105. if (type.equals(TermsAggregationBuilder.NAME)) {
  106. Terms.Bucket elasticBucket1 = ((Terms) agg).getBucketByKey("Elastic");
  107. long numberOfDocs = elasticBucket1.getDocCount();
  108. }
  109. }
  110.  
  111. //Retrieving Suggestions
  112. Suggest suggest = searchResponse.getSuggest();
  113. TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user");
  114. for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
  115. for (TermSuggestion.Entry.Option option : entry) {
  116. String suggestText = option.getText().string();
  117. }
  118. }
  119. }

Multi-search APIs介绍:

  1. public void multiSearch() throws Exception{
  2. MultiSearchRequest request = new MultiSearchRequest();
  3. SearchRequest firstSearchRequest = new SearchRequest();
  4. SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  5. searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy"));
  6. firstSearchRequest.source(searchSourceBuilder);
  7. request.add(firstSearchRequest);
  8.  
  9. SearchRequest secondSearchRequest = new SearchRequest();
  10. searchSourceBuilder = new SearchSourceBuilder();
  11. searchSourceBuilder.query(QueryBuilders.matchQuery("user", "luca"));
  12. secondSearchRequest.source(searchSourceBuilder);
  13. request.add(secondSearchRequest);
  14.  
  15. //同步执行
  16. MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT);
  17.  
  18. //同步执行 listener参考index的
  19. client.msearchAsync(request, RequestOptions.DEFAULT, listener);
  20.  
  21. //MultiSearchResponse
  22. MultiSearchResponse.Item firstResponse = response.getResponses()[0];
  23. SearchResponse searchResponse = firstResponse.getResponse();
  24. MultiSearchResponse.Item secondResponse = response.getResponses()[1];
  25. searchResponse = secondResponse.getResponse();
  26. }

SearchTemplate APIs介绍:

  1. /**
  2. * 查询模板
  3. * @throws Exception
  4. */
  5. public void searchTemplate() throws Exception{
  6.  
  7. SearchTemplateRequest request = new SearchTemplateRequest();
  8. request.setRequest(new SearchRequest("posts"));
  9. //Inline Templates
  10. request.setScriptType(ScriptType.INLINE);
  11. //instead of providing an inline script
  12. request.setScriptType(ScriptType.STORED);
  13. request.setScript(
  14. "{" +
  15. " \"query\": { \"match\": { \"{{ field }}\": \"{{ value }}\" } }," +
  16. " \"size\": \"{{ size }}\"" +
  17. "}");
  18. Map<String, Object> scriptParams = new HashMap<>();
  19. scriptParams.put("field", "title");
  20. scriptParams.put("value", "elasticsearch");
  21. scriptParams.put("size", 5);
  22. request.setScriptParams(scriptParams);
  23.  
  24. //同步执行
  25. SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
  26.  
  27. //异步执行 listener参考Index的
  28. client.searchTemplateAsync(request, RequestOptions.DEFAULT, listener);
  29.  
  30. //SearchTemplate Response
  31. SearchResponse searchResponse = response.getResponse();
  32. BytesReference source = response.getSource();
  33. }

Multi-SearchTemplate APIs介绍:

  1. /**
  2. * 多个查询模板执行
  3. * @throws Exception
  4. */
  5. public void MultiSearchTemplate() throws Exception{
  6. String[] searchTerms = {"elasticsearch", "logstash", "kibana"};
  7. MultiSearchTemplateRequest multiRequest = new MultiSearchTemplateRequest();
  8. for (String searchTerm: searchTerms) {
  9. SearchTemplateRequest request = new SearchTemplateRequest();
  10. request.setRequest(new SearchRequest("posts"));
  11.  
  12. request.setScriptType(ScriptType.INLINE);
  13. request.setScript(
  14. "{" +
  15. " \"query\": { \"match\": { \"{{field}}\": \"{{value}}\" }}," +
  16. " \"size\": \"{{size}}\"" +
  17. "}"
  18. );
  19.  
  20. Map<String, Object> scriptParams = new HashMap<>();
  21. scriptParams.put("field", "title");
  22. scriptParams.put("value", searchTerm);
  23. scriptParams.put("size", 5);
  24. request.setScriptParams(scriptParams);
  25.  
  26. multiRequest.add(request);
  27. }
  28.  
  29. //同步执行
  30. MultiSearchTemplateResponse multiResponse = client.msearchTemplate(multiRequest, RequestOptions.DEFAULT);
  31.  
  32. //异步执行
  33. ActionListener<MultiSearchTemplateResponse> listener = new ActionListener<MultiSearchTemplateResponse>() {
  34. @Override
  35. public void onResponse(MultiSearchTemplateResponse response) {
  36.  
  37. }
  38.  
  39. @Override
  40. public void onFailure(Exception e) {
  41.  
  42. }
  43. };
  44. client.msearchTemplateAsync(multiRequest, RequestOptions.DEFAULT, listener);
  45.  
  46. //MultiSearchTemplateResponse
  47. for (MultiSearchTemplateResponse.Item item : multiResponse.getResponses()) {
  48. if (item.isFailure()) {
  49. String error = item.getFailureMessage();
  50. } else {
  51. SearchTemplateResponse searchTemplateResponse = item.getResponse();
  52. SearchResponse searchResponse = searchTemplateResponse.getResponse();
  53. searchResponse.getHits();
  54. }
  55. }
  56. }

其余APIs可以参考:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high.html

4. Jest

Jest是第三方工具,是ElasticSearch的Java HTTP Rest客户端。Jest填补了ElasticSearch缺少Http Rest接口客户端的空白。

Jest的API为:

Jest的具体用法参考:https://blog.csdn.net/u010466329/article/details/75020956/

Jest的代码github地址:https://github.com/searchbox-io/Jest

5. Spring Data Elasticsearch

可参考https://blog.csdn.net/qq_33314107/article/details/80725994

6. 常见报错

NoNodeAvailableException报错,主要是外网IP没有配置

在elasticSearch.yml上配置transport.host和transport.tcp.port即可

transport.host: localhost
transport.tcp.port: 9300

localhost可以是具体的IP地址

7. 总结

个人编写的TransportClient和RestClient的github Demo:

https://github.com/SwordfallYeung/ElasticSearchDemo

参考资料:

https://www.elastic.co/guide/en/elasticsearch/client/index.html

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.4/java-rest-overview.html

https://blog.csdn.net/qq_33314107/article/details/80725913

Elasticsearch 6.4基本操作 - Java版的更多相关文章

  1. 树的基本操作java版

    看了一下树的定义,一些基本的操作,遍历,获取节点数,获取深度等等..这里参考了西电版的数据结构,基本用的都是递归实现的. 很多说明代码中都有,每个方法我都测了一下,这里我把节点类BTreeNode作为 ...

  2. 实现二叉树的基本操作(Java版)

    近期研究了一下二叉树,试着用Java语言实现了二叉树的基本操作,下面分享一下实现代码: package com.sf.test; import java.util.ArrayDeque; import ...

  3. 20165234 [第二届构建之法论坛] 预培训文档(Java版) 学习总结

    [第二届构建之法论坛] 预培训文档(Java版) 学习总结 我通读并学习了此文档,并且动手实践了一遍.以下是我学习过程的记录~ Part1.配置环境 配置JDK 原文中提到了2个容易被混淆的概念 JD ...

  4. 常见排序算法题(java版)

    常见排序算法题(java版) //插入排序:   package org.rut.util.algorithm.support;   import org.rut.util.algorithm.Sor ...

  5. ArcGIS Server 10 Java 版的Rest服务手动配置方法

    Java版的Manager中发布的服务默认只发布了该服务的SOAP接口,而REST接口需要用户在信息服务器,如Tomcat. Apache.WebLogic等中手工配置.由于在Java版的Server ...

  6. PetaPojo —— JAVA版的PetaPoco

    背景 由于工作的一些原因,需要从C#转成JAVA.之前PetaPoco用得真是非常舒服,在学习JAVA的过程中熟悉了一下JAVA的数据组件: MyBatis 非常流行,代码生成也很成熟,性能也很好.但 ...

  7. 【转载】java版打字练习软件

    网上找到一个java版的打字的游戏 import java.applet.Applet; import java.applet.AudioClip; import java.awt.Dimension ...

  8. Elasticsearch 1.X 版本Java插件开发

    接上一篇<Elasticsearch 2.X 版本Java插件开发简述> 开发1.X版本elasticsearch java插件与2.X版本有一些不同,同时在安装部署上也有些不同,主要区别 ...

  9. Elasticsearch 2.X 版本Java插件开发简述

    1:elasticsearch插件分类简述 2:Java插件开发要点 3:如何针对不同版本elasticsearch提供多版本的插件 4:插件具有外部依赖时遇到的一些问题(2016-09-07更新) ...

随机推荐

  1. MySql 学习之路-基础

    Mysql 自学之路 本文包含基础部分与高级部分 一.基础 数据库操作 Show databases:显示所有的数据库 Show tables: 显示所有的数据库表 Use databasename: ...

  2. 记录Nginx模块开发

    --with-http_stub_status_module模块:Nginx的客户端状态 Syntax:stub_status Default:-- Content:server,location l ...

  3. 【Teradata Utility】使用SQL Assistant导出导入数据

    1.导出 (1)选择菜单栏File,点击Export Results,输入导出数据的SQL: select * from etl_data.soure_table; (2)选择导出数据格式为txt或h ...

  4. Zabbix 3.4.7针对一些主机设置期间维护

    场景说明: 由于公司有些主机设置了定时开机关机,每次开机关机得时候都会发邮件告警,每次都需要值班人员提醒,为了处理这种无效告警,可以在zabbix中设置维护 zabbix中的维护---维护期间:用来设 ...

  5. git 入门(转)

    1. good https://github.com/521xueweihan/git-tips Git的奇技淫巧 2.如果之前未使用过 Git,可以学习 Git 小白教程入门 3.思维导图

  6. Kafka 详解(一)------简介

    在前面几篇博客我们介绍过一种消息中间件——RabbitMQ,本篇博客我们介绍另外一个消息中间件——Kafka,Kafka是由LinkedIn开发的,使用Scala编写,是一种分布式,基于发布/订阅的消 ...

  7. 3.HttpSession

    1 HttpSession概述 1.1 什么是HttpSesssion javax.servlet.http.HttpSession接口表示一个会话,我们可以把一个会话内需要共享的数据保存到HttSe ...

  8. 在Bootstrap开发框架的前端视图中使用@RenderPage实现页面内容模块化的隔离,减少复杂度

    在很多开发的场景中,很多情况下我们需要考虑抽象.以及模块化等方面的内容,其目的就是为了使得开发的时候关注的变化内容更加少一些,整体开发更加简单化,从而减少开发的复杂度,在Winform开发的时候,往往 ...

  9. building 'twisted.test.raiser' extension error: Microsoft Visual C++ 14.0 is required. Get it with "Microsoft Visual C++ Build Tools": http://landinghub.visualstudio.com/visual-cpp-build-tools

    Error msg: building 'twisted.test.raiser' extension error: Microsoft Visual C++ 14.0 is required. Ge ...

  10. vue脚手架搭建移动端项目--flexible.js

    通过命令行 node -v 查看是否安装node环境 在 nodejs 和 webpack已安装的前提下,随便一个文件夹下,输入命令行 npm install vue-cli -g 安装完成后,通过 ...