From ae443c6f0ea53129f42d881623eb5cd1f1419172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=9F=B3=E6=BA=90?= Date: Sat, 24 Aug 2024 21:13:46 +0800 Subject: [PATCH] es 8.15.0 support --- .travis.yml | 2 +- pom.xml | 4 +-- .../action/search/ParsedSearchResponse.java | 4 ++- .../handler/ClusterStateActionHandler.java | 5 ++++ .../ClusterUpdateSettingsActionHandler.java | 2 +- .../handler/CreateIndexActionHandler.java | 2 +- .../handler/DeleteIndexActionHandler.java | 2 +- .../handler/PutMappingActionHandler.java | 2 +- .../org/nlpcn/es4sql/domain/SearchResult.java | 29 ++++++++++--------- .../org/nlpcn/es4sql/AggregationTest.java | 11 +++++-- 10 files changed, 39 insertions(+), 24 deletions(-) diff --git a/.travis.yml b/.travis.yml index 4845ce15..f1f74dc3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ jdk: before_install: - sudo rm -rf /var/lib/elasticsearch - - curl https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.14.3-amd64.deb -o elasticsearch.deb && sudo dpkg -i --force-confnew elasticsearch.deb + - curl https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-8.15.0-amd64.deb -o elasticsearch.deb && sudo dpkg -i --force-confnew elasticsearch.deb - sudo cp ./src/test/resources/elasticsearch.yml /etc/elasticsearch/elasticsearch.yml - sudo cat /etc/elasticsearch/elasticsearch.yml - sudo java -version diff --git a/pom.xml b/pom.xml index d7d77dfe..025d7911 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ 4.0.0 org.nlpcn elasticsearch-sql - 8.14.3.0 + 8.15.0.0 jar Query elasticsearch using SQL elasticsearch-sql @@ -44,7 +44,7 @@ UTF-8 **/MainTestSuite.class sql - 8.14.3 + 8.15.0 org.elasticsearch.plugin.nlpcn.SqlPlug 1.2.15 32.0.0-jre diff --git a/src/main/java/org/elasticsearch/action/search/ParsedSearchResponse.java b/src/main/java/org/elasticsearch/action/search/ParsedSearchResponse.java index 7d882582..4466c6c7 100644 --- a/src/main/java/org/elasticsearch/action/search/ParsedSearchResponse.java +++ b/src/main/java/org/elasticsearch/action/search/ParsedSearchResponse.java @@ -8,6 +8,7 @@ package org.elasticsearch.action.search; +import org.elasticsearch.common.bytes.BytesArray; import org.elasticsearch.common.util.concurrent.ConcurrentCollections; import org.elasticsearch.core.RefCounted; import org.elasticsearch.core.TimeValue; @@ -28,6 +29,7 @@ import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Objects; import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken; @@ -151,7 +153,7 @@ public static SearchResponse innerFromXContent(XContentParser parser) throws IOE tookInMillis, failures.toArray(ShardSearchFailure.EMPTY_ARRAY), clusters, - searchContextId + Objects.nonNull(searchContextId) ? new BytesArray(searchContextId) : null )); } diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/ClusterStateActionHandler.java b/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/ClusterStateActionHandler.java index 0428913b..66ce7a85 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/ClusterStateActionHandler.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/ClusterStateActionHandler.java @@ -37,6 +37,7 @@ import org.elasticsearch.common.unit.RelativeByteSizeValue; import org.elasticsearch.common.xcontent.XContentParserUtils; import org.elasticsearch.health.metadata.HealthMetadata; +import org.elasticsearch.index.IndexVersion; import org.elasticsearch.index.shard.IndexLongFieldRange; import org.elasticsearch.xcontent.XContentParser; @@ -83,6 +84,7 @@ public class ClusterStateActionHandler extends ActionHandler builder.masterTimeout(Time.of(t -> t.time(e.toString())))); - Optional.ofNullable(clusterUpdateSettingsRequest.timeout()).ifPresent(e -> builder.timeout(Time.of(t -> t.time(e.toString())))); + Optional.ofNullable(clusterUpdateSettingsRequest.ackTimeout()).ifPresent(e -> builder.timeout(Time.of(t -> t.time(e.toString())))); return builder.build(); } diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/CreateIndexActionHandler.java b/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/CreateIndexActionHandler.java index 5ead5b5e..bd0efa74 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/CreateIndexActionHandler.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/CreateIndexActionHandler.java @@ -53,7 +53,7 @@ protected CreateIndexRequest convertRequest(org.elasticsearch.action.admin.indic builder.index(createIndexRequest.index()); builder.mappings(fromJson(createIndexRequest.mappings(), TypeMapping._DESERIALIZER)); builder.settings(fromJson(createIndexRequest.settings().toString(), IndexSettings._DESERIALIZER)); - Optional.ofNullable(createIndexRequest.timeout()).ifPresent(e -> builder.timeout(Time.of(t -> t.time(e.toString())))); + Optional.ofNullable(createIndexRequest.ackTimeout()).ifPresent(e -> builder.timeout(Time.of(t -> t.time(e.toString())))); Optional.ofNullable(createIndexRequest.masterNodeTimeout()).ifPresent(e -> builder.masterTimeout(Time.of(t -> t.time(e.toString())))); ActiveShardCount activeShardCount = createIndexRequest.waitForActiveShards(); if (Objects.nonNull(activeShardCount) && activeShardCount.value() > -1) { diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/DeleteIndexActionHandler.java b/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/DeleteIndexActionHandler.java index f2ee8b7d..3fe5c3ad 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/DeleteIndexActionHandler.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/DeleteIndexActionHandler.java @@ -39,7 +39,7 @@ protected DeleteIndexRequest convertRequest(org.elasticsearch.action.admin.indic DeleteIndexRequest.Builder builder = new DeleteIndexRequest.Builder(); builder.index(Arrays.asList(deleteIndexRequest.indices())); Optional.ofNullable(deleteIndexRequest.masterNodeTimeout()).ifPresent(e -> builder.masterTimeout(Time.of(t -> t.time(e.toString())))); - Optional.ofNullable(deleteIndexRequest.timeout()).ifPresent(e -> builder.timeout(Time.of(t -> t.time(e.toString())))); + Optional.ofNullable(deleteIndexRequest.ackTimeout()).ifPresent(e -> builder.timeout(Time.of(t -> t.time(e.toString())))); Optional.ofNullable(deleteIndexRequest.indicesOptions()).ifPresent(options -> { builder.allowNoIndices(options.allowNoIndices()); builder.ignoreUnavailable(options.ignoreUnavailable()); diff --git a/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/PutMappingActionHandler.java b/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/PutMappingActionHandler.java index 15ebc72a..03f647d2 100644 --- a/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/PutMappingActionHandler.java +++ b/src/main/java/org/elasticsearch/plugin/nlpcn/client/handler/PutMappingActionHandler.java @@ -54,7 +54,7 @@ protected PutMappingRequest convertRequest(org.elasticsearch.action.admin.indice builder.ignoreUnavailable(options.ignoreUnavailable()); builder.expandWildcards(getExpandWildcard(options.wildcardOptions())); }); - Optional.ofNullable(putMappingRequest.timeout()).ifPresent(e -> builder.timeout(Time.of(t -> t.time(e.toString())))); + Optional.ofNullable(putMappingRequest.ackTimeout()).ifPresent(e -> builder.timeout(Time.of(t -> t.time(e.toString())))); Optional.ofNullable(putMappingRequest.masterNodeTimeout()).ifPresent(e -> builder.masterTimeout(Time.of(t -> t.time(e.toString())))); return builder.build(); } diff --git a/src/main/java/org/nlpcn/es4sql/domain/SearchResult.java b/src/main/java/org/nlpcn/es4sql/domain/SearchResult.java index 3c289c7c..60e4cd15 100644 --- a/src/main/java/org/nlpcn/es4sql/domain/SearchResult.java +++ b/src/main/java/org/nlpcn/es4sql/domain/SearchResult.java @@ -1,14 +1,8 @@ package org.nlpcn.es4sql.domain; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.document.DocumentField; +import org.elasticsearch.common.util.Maps; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.aggregations.Aggregation; @@ -23,6 +17,13 @@ import org.elasticsearch.search.aggregations.metrics.InternalValueCount; import org.nlpcn.es4sql.exception.SqlParseException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + public class SearchResult { /** * 查询结果 @@ -60,7 +61,7 @@ public SearchResult(SearchResponse resp, Select select) throws SqlParseException this.total = buckets.size(); results = new ArrayList<>(buckets.size()); for (Bucket bucket : buckets) { - Map aggsMap = toAggsMap(bucket.getAggregations().getAsMap()); + Map aggsMap = toAggsMap(bucket.getAggregations().asList()); aggsMap.put("docCount", bucket.getDocCount()); results.add(aggsMap); } @@ -97,15 +98,15 @@ private Map toFieldsMap(Map fields) { /** * 讲es的field域转换为你Object - * - * @param fields + * + * @param aggregations * @return * @throws SqlParseException */ - private Map toAggsMap(Map fields) throws SqlParseException { - Map result = new HashMap<>(); - for (Entry entry : fields.entrySet()) { - result.put(entry.getKey(), covenValue(entry.getValue())); + private Map toAggsMap(List aggregations) throws SqlParseException { + Map result = Maps.newMapWithExpectedSize(aggregations.size()); + for (InternalAggregation aggregation : aggregations) { + result.put(aggregation.getName(), covenValue(aggregation)); } return result; } diff --git a/src/test/java/org/nlpcn/es4sql/AggregationTest.java b/src/test/java/org/nlpcn/es4sql/AggregationTest.java index 04fc65d5..54591970 100644 --- a/src/test/java/org/nlpcn/es4sql/AggregationTest.java +++ b/src/test/java/org/nlpcn/es4sql/AggregationTest.java @@ -4,8 +4,10 @@ import com.google.common.collect.DiscreteDomain; import com.google.common.collect.Range; import org.elasticsearch.action.search.SearchResponse; +import org.elasticsearch.common.util.Maps; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; +import org.elasticsearch.search.aggregations.InternalAggregation; import org.elasticsearch.search.aggregations.InternalAggregations; import org.elasticsearch.search.aggregations.bucket.filter.Filters; import org.elasticsearch.search.aggregations.bucket.filter.ParsedFilter; @@ -201,8 +203,13 @@ public void percentileTestSpecific() throws IOException, SqlParseException, SQLF @Test public void aliasTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException { InternalAggregations result = query(String.format("SELECT COUNT(*) AS mycount FROM %s", TEST_INDEX_ACCOUNT)); - assertThat(result.asMap(), hasKey("mycount")); - } + List aggregations = result.asList(); + Map aggregationsAsMap = Maps.newMapWithExpectedSize(aggregations.size()); + for (InternalAggregation aggregation : aggregations) { + aggregationsAsMap.put(aggregation.getName(), aggregation); + } + assertThat(aggregationsAsMap, hasKey("mycount")); + } @Test public void groupByTest() throws Exception {