diff --git a/core/src/main/java/org/elasticsearch/action/ActionModule.java b/core/src/main/java/org/elasticsearch/action/ActionModule.java index be9387f9a8ac0..2b33a66942878 100644 --- a/core/src/main/java/org/elasticsearch/action/ActionModule.java +++ b/core/src/main/java/org/elasticsearch/action/ActionModule.java @@ -173,8 +173,6 @@ import org.elasticsearch.action.search.TransportMultiSearchAction; import org.elasticsearch.action.search.TransportSearchAction; import org.elasticsearch.action.search.TransportSearchScrollAction; -import org.elasticsearch.action.suggest.SuggestAction; -import org.elasticsearch.action.suggest.TransportSuggestAction; import org.elasticsearch.action.support.ActionFilter; import org.elasticsearch.action.support.ActionFilters; import org.elasticsearch.action.support.AutoCreateIndex; @@ -320,7 +318,6 @@ protected void configure() { registerAction(MultiTermVectorsAction.INSTANCE, TransportMultiTermVectorsAction.class, TransportShardMultiTermsVectorAction.class); registerAction(DeleteAction.INSTANCE, TransportDeleteAction.class); - registerAction(SuggestAction.INSTANCE, TransportSuggestAction.class); registerAction(UpdateAction.INSTANCE, TransportUpdateAction.class); registerAction(MultiGetAction.INSTANCE, TransportMultiGetAction.class, TransportShardMultiGetAction.class); diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStats.java b/core/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStats.java index b6ee76b16ed9c..b130e6b378f39 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStats.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStats.java @@ -42,7 +42,6 @@ import org.elasticsearch.index.shard.DocsStats; import org.elasticsearch.index.shard.IndexShard; import org.elasticsearch.index.store.StoreStats; -import org.elasticsearch.index.suggest.stats.SuggestStats; import org.elasticsearch.index.translog.TranslogStats; import org.elasticsearch.index.warmer.WarmerStats; import org.elasticsearch.indices.IndicesQueryCache; @@ -109,7 +108,7 @@ public CommonStats(CommonStatsFlags flags) { translog = new TranslogStats(); break; case Suggest: - suggest = new SuggestStats(); + // skip break; case RequestCache: requestCache = new RequestCacheStats(); @@ -177,7 +176,7 @@ public CommonStats(IndicesQueryCache indicesQueryCache, PercolatorQueryCache per translog = indexShard.translogStats(); break; case Suggest: - suggest = indexShard.suggestStats(); + // skip break; case RequestCache: requestCache = indexShard.requestCache().stats(); @@ -236,9 +235,6 @@ public CommonStats(IndicesQueryCache indicesQueryCache, PercolatorQueryCache per @Nullable public TranslogStats translog; - @Nullable - public SuggestStats suggest; - @Nullable public RequestCacheStats requestCache; @@ -367,14 +363,6 @@ public void add(CommonStats stats) { } else { translog.add(stats.getTranslog()); } - if (suggest == null) { - if (stats.getSuggest() != null) { - suggest = new SuggestStats(); - suggest.add(stats.getSuggest()); - } - } else { - suggest.add(stats.getSuggest()); - } if (requestCache == null) { if (stats.getRequestCache() != null) { requestCache = new RequestCacheStats(); @@ -468,11 +456,6 @@ public TranslogStats getTranslog() { return translog; } - @Nullable - public SuggestStats getSuggest() { - return suggest; - } - @Nullable public RequestCacheStats getRequestCache() { return requestCache; @@ -555,7 +538,6 @@ public void readFrom(StreamInput in) throws IOException { segments = SegmentsStats.readSegmentsStats(in); } translog = in.readOptionalStreamable(TranslogStats::new); - suggest = in.readOptionalStreamable(SuggestStats::new); requestCache = in.readOptionalStreamable(RequestCacheStats::new); recoveryStats = in.readOptionalStreamable(RecoveryStats::new); } @@ -647,7 +629,6 @@ public void writeTo(StreamOutput out) throws IOException { segments.writeTo(out); } out.writeOptionalStreamable(translog); - out.writeOptionalStreamable(suggest); out.writeOptionalStreamable(requestCache); out.writeOptionalStreamable(recoveryStats); } @@ -700,9 +681,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws if (translog != null) { translog.toXContent(builder, params); } - if (suggest != null) { - suggest.toXContent(builder, params); - } if (requestCache != null) { requestCache.toXContent(builder, params); } diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStatsFlags.java b/core/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStatsFlags.java index c67c35a410883..f00f22da536ae 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStatsFlags.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/stats/CommonStatsFlags.java @@ -244,7 +244,7 @@ public static enum Flag { Completion("completion"), Segments("segments"), Translog("translog"), - Suggest("suggest"), + Suggest("suggest"), // unused RequestCache("request_cache"), Recovery("recovery"); diff --git a/core/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsRequestBuilder.java b/core/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsRequestBuilder.java index 0ae21a3ac09e2..cad919cbd1844 100644 --- a/core/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsRequestBuilder.java +++ b/core/src/main/java/org/elasticsearch/action/admin/indices/stats/IndicesStatsRequestBuilder.java @@ -152,11 +152,6 @@ public IndicesStatsRequestBuilder setTranslog(boolean translog) { return this; } - public IndicesStatsRequestBuilder setSuggest(boolean suggest) { - request.suggest(suggest); - return this; - } - public IndicesStatsRequestBuilder setRequestCache(boolean requestCache) { request.requestCache(requestCache); return this; diff --git a/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java b/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java index 9d3c200ed984f..e87d393bf05c3 100644 --- a/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java +++ b/core/src/main/java/org/elasticsearch/action/search/SearchRequest.java @@ -295,6 +295,13 @@ public Boolean requestCache() { return this.requestCache; } + /** + * @return true if the request only has suggest + */ + public boolean isSuggestOnly() { + return source != null && source.isSuggestOnly(); + } + @Override public void readFrom(StreamInput in) throws IOException { super.readFrom(in); diff --git a/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java b/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java index 8fe99c1e5901e..5b20a05bad599 100644 --- a/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java +++ b/core/src/main/java/org/elasticsearch/action/search/TransportSearchAction.java @@ -38,6 +38,7 @@ import java.util.Set; import static org.elasticsearch.action.search.SearchType.QUERY_AND_FETCH; +import static org.elasticsearch.action.search.SearchType.QUERY_THEN_FETCH; /** * @@ -72,6 +73,17 @@ protected void doExecute(SearchRequest searchRequest, ActionListener { - - public static final SuggestAction INSTANCE = new SuggestAction(); - public static final String NAME = "indices:data/read/suggest"; - - private SuggestAction() { - super(NAME); - } - - @Override - public SuggestResponse newResponse() { - return new SuggestResponse(new Suggest()); - } - - @Override - public SuggestRequestBuilder newRequestBuilder(ElasticsearchClient client) { - return new SuggestRequestBuilder(client, this); - } -} diff --git a/core/src/main/java/org/elasticsearch/action/suggest/SuggestRequest.java b/core/src/main/java/org/elasticsearch/action/suggest/SuggestRequest.java deleted file mode 100644 index 1398dd1dcf1bf..0000000000000 --- a/core/src/main/java/org/elasticsearch/action/suggest/SuggestRequest.java +++ /dev/null @@ -1,154 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.action.suggest; - -import org.elasticsearch.action.ActionRequestValidationException; -import org.elasticsearch.action.support.broadcast.BroadcastRequest; -import org.elasticsearch.common.Nullable; -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.json.JsonXContent; -import org.elasticsearch.search.suggest.SuggestBuilder; - -import java.io.IOException; -import java.util.Arrays; -import java.util.Objects; - -/** - * A request to get suggestions for corrections of phrases. Best created with - * {@link org.elasticsearch.client.Requests#suggestRequest(String...)}. - *

- * The request requires the suggest query source to be set using - * {@link #suggest(org.elasticsearch.search.suggest.SuggestBuilder)} - * - * @see SuggestResponse - * @see org.elasticsearch.client.Client#suggest(SuggestRequest) - * @see org.elasticsearch.client.Requests#suggestRequest(String...) - * @see org.elasticsearch.search.suggest.SuggestBuilders - */ -public final class SuggestRequest extends BroadcastRequest { - - @Nullable - private String routing; - - @Nullable - private String preference; - - private SuggestBuilder suggest; - - public SuggestRequest() { - } - - /** - * Constructs a new suggest request against the provided indices. No indices provided means it will - * run against all indices. - */ - public SuggestRequest(String... indices) { - super(indices); - } - - @Override - public ActionRequestValidationException validate() { - ActionRequestValidationException validationException = super.validate(); - return validationException; - } - - /** - * The suggestion query to get correction suggestions for - */ - public SuggestBuilder suggest() { - return suggest; - } - - /** - * set a new source for the suggest query - */ - public SuggestRequest suggest(SuggestBuilder suggest) { - Objects.requireNonNull(suggest, "suggest must not be null"); - this.suggest = suggest; - return this; - } - - /** - * A comma separated list of routing values to control the shards the search will be executed on. - */ - public String routing() { - return this.routing; - } - - /** - * A comma separated list of routing values to control the shards the search will be executed on. - */ - public SuggestRequest routing(String routing) { - this.routing = routing; - return this; - } - - /** - * The routing values to control the shards that the search will be executed on. - */ - public SuggestRequest routing(String... routings) { - this.routing = Strings.arrayToCommaDelimitedString(routings); - return this; - } - - public SuggestRequest preference(String preference) { - this.preference = preference; - return this; - } - - public String preference() { - return this.preference; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - routing = in.readOptionalString(); - preference = in.readOptionalString(); - suggest = SuggestBuilder.PROTOTYPE.readFrom(in); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - Objects.requireNonNull(suggest, "suggest must not be null"); - super.writeTo(out); - out.writeOptionalString(routing); - out.writeOptionalString(preference); - suggest.writeTo(out); - } - - @Override - public String toString() { - Objects.requireNonNull(suggest, "suggest must not be null"); - String sSource = "_na_"; - try { - XContentBuilder builder = JsonXContent.contentBuilder(); - builder = suggest.toXContent(builder, ToXContent.EMPTY_PARAMS); - sSource = builder.string(); - } catch (Exception e) { - // ignore - } - return "[" + Arrays.toString(indices) + "]" + ", suggest[" + sSource + "]"; - } -} diff --git a/core/src/main/java/org/elasticsearch/action/suggest/SuggestRequestBuilder.java b/core/src/main/java/org/elasticsearch/action/suggest/SuggestRequestBuilder.java deleted file mode 100644 index b64745bf40038..0000000000000 --- a/core/src/main/java/org/elasticsearch/action/suggest/SuggestRequestBuilder.java +++ /dev/null @@ -1,85 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.action.suggest; - -import org.elasticsearch.action.support.broadcast.BroadcastOperationRequestBuilder; -import org.elasticsearch.client.ElasticsearchClient; -import org.elasticsearch.search.suggest.SuggestBuilder; -import org.elasticsearch.search.suggest.SuggestionBuilder; - -/** - * A suggest action request builder. - */ -public class SuggestRequestBuilder extends BroadcastOperationRequestBuilder { - - final SuggestBuilder suggest = new SuggestBuilder(); - - public SuggestRequestBuilder(ElasticsearchClient client, SuggestAction action) { - super(client, action, new SuggestRequest()); - } - - /** - * Add a definition for suggestions to the request - * @param name the name for the suggestion that will also be used in the response - * @param suggestion the suggestion configuration - */ - public SuggestRequestBuilder addSuggestion(String name, SuggestionBuilder suggestion) { - suggest.addSuggestion(name, suggestion); - return this; - } - - /** - * A comma separated list of routing values to control the shards the search will be executed on. - */ - public SuggestRequestBuilder setRouting(String routing) { - request.routing(routing); - return this; - } - - public SuggestRequestBuilder setSuggestText(String globalText) { - this.suggest.setGlobalText(globalText); - return this; - } - - /** - * Sets the preference to execute the search. Defaults to randomize across shards. Can be set to - * _local to prefer local shards, _primary to execute only on primary shards, - * _shards:x,y to operate on shards x & y, or a custom value, which guarantees that the same order - * will be used across different requests. - */ - public SuggestRequestBuilder setPreference(String preference) { - request.preference(preference); - return this; - } - - /** - * The routing values to control the shards that the search will be executed on. - */ - public SuggestRequestBuilder setRouting(String... routing) { - request.routing(routing); - return this; - } - - @Override - protected SuggestRequest beforeExecute(SuggestRequest request) { - request.suggest(suggest); - return request; - } -} diff --git a/core/src/main/java/org/elasticsearch/action/suggest/SuggestResponse.java b/core/src/main/java/org/elasticsearch/action/suggest/SuggestResponse.java deleted file mode 100644 index 445e804b5b589..0000000000000 --- a/core/src/main/java/org/elasticsearch/action/suggest/SuggestResponse.java +++ /dev/null @@ -1,82 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.action.suggest; - -import org.elasticsearch.action.ShardOperationFailedException; -import org.elasticsearch.action.support.broadcast.BroadcastResponse; -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentFactory; -import org.elasticsearch.search.suggest.Suggest; - -import java.io.IOException; -import java.util.List; - -import static org.elasticsearch.common.xcontent.ToXContent.EMPTY_PARAMS; - -/** - * The response of the suggest action. - */ -public final class SuggestResponse extends BroadcastResponse { - - private final Suggest suggest; - - SuggestResponse(Suggest suggest) { - this.suggest = suggest; - } - - SuggestResponse(Suggest suggest, int totalShards, int successfulShards, int failedShards, List shardFailures) { - super(totalShards, successfulShards, failedShards, shardFailures); - this.suggest = suggest; - } - - /** - * The Suggestions of the phrase. - */ - public Suggest getSuggest() { - return suggest; - } - - @Override - public void readFrom(StreamInput in) throws IOException { - super.readFrom(in); - this.suggest.readFrom(in); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - super.writeTo(out); - this.suggest.writeTo(out); - } - - @Override - public String toString() { - try { - XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); - builder.startObject(); - suggest.toXContent(builder, EMPTY_PARAMS); - builder.endObject(); - return builder.string(); - } catch (IOException e) { - return "{ \"error\" : \"" + e.getMessage() + "\"}"; - } - } -} diff --git a/core/src/main/java/org/elasticsearch/action/suggest/TransportSuggestAction.java b/core/src/main/java/org/elasticsearch/action/suggest/TransportSuggestAction.java deleted file mode 100644 index 95bf111ed718f..0000000000000 --- a/core/src/main/java/org/elasticsearch/action/suggest/TransportSuggestAction.java +++ /dev/null @@ -1,152 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.action.suggest; - -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.action.ShardOperationFailedException; -import org.elasticsearch.action.support.ActionFilters; -import org.elasticsearch.action.support.DefaultShardOperationFailedException; -import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException; -import org.elasticsearch.action.support.broadcast.TransportBroadcastAction; -import org.elasticsearch.cluster.ClusterState; -import org.elasticsearch.cluster.block.ClusterBlockException; -import org.elasticsearch.cluster.block.ClusterBlockLevel; -import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver; -import org.elasticsearch.cluster.routing.GroupShardsIterator; -import org.elasticsearch.cluster.routing.ShardRouting; -import org.elasticsearch.cluster.service.ClusterService; -import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.index.IndexService; -import org.elasticsearch.index.engine.Engine; -import org.elasticsearch.index.shard.IndexShard; -import org.elasticsearch.index.suggest.stats.ShardSuggestMetric; -import org.elasticsearch.indices.IndicesService; -import org.elasticsearch.search.suggest.Suggest; -import org.elasticsearch.search.suggest.SuggestBuilder; -import org.elasticsearch.search.suggest.SuggestPhase; -import org.elasticsearch.search.suggest.SuggestionSearchContext; -import org.elasticsearch.threadpool.ThreadPool; -import org.elasticsearch.transport.TransportService; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.atomic.AtomicReferenceArray; - -/** - * Defines the transport of a suggestion request across the cluster - */ -public class TransportSuggestAction - extends TransportBroadcastAction { - - private final IndicesService indicesService; - private final SuggestPhase suggestPhase; - - @Inject - public TransportSuggestAction(Settings settings, ThreadPool threadPool, ClusterService clusterService, - TransportService transportService, IndicesService indicesService, SuggestPhase suggestPhase, - ActionFilters actionFilters, IndexNameExpressionResolver indexNameExpressionResolver) { - super(settings, SuggestAction.NAME, threadPool, clusterService, transportService, actionFilters, indexNameExpressionResolver, - SuggestRequest::new, ShardSuggestRequest::new, ThreadPool.Names.SUGGEST); - this.indicesService = indicesService; - this.suggestPhase = suggestPhase; - } - - @Override - protected ShardSuggestRequest newShardRequest(int numShards, ShardRouting shard, SuggestRequest request) { - return new ShardSuggestRequest(shard.shardId(), request); - } - - @Override - protected ShardSuggestResponse newShardResponse() { - return new ShardSuggestResponse(); - } - - @Override - protected GroupShardsIterator shards(ClusterState clusterState, SuggestRequest request, String[] concreteIndices) { - Map> routingMap = - indexNameExpressionResolver.resolveSearchRouting(clusterState, request.routing(), request.indices()); - return clusterService.operationRouting().searchShards(clusterState, concreteIndices, routingMap, request.preference()); - } - - @Override - protected ClusterBlockException checkGlobalBlock(ClusterState state, SuggestRequest request) { - return state.blocks().globalBlockedException(ClusterBlockLevel.READ); - } - - @Override - protected ClusterBlockException checkRequestBlock(ClusterState state, SuggestRequest countRequest, String[] concreteIndices) { - return state.blocks().indicesBlockedException(ClusterBlockLevel.READ, concreteIndices); - } - - @Override - protected SuggestResponse newResponse(SuggestRequest request, AtomicReferenceArray shardsResponses, ClusterState clusterState) { - int successfulShards = 0; - int failedShards = 0; - - final Map> groupedSuggestions = new HashMap<>(); - - List shardFailures = null; - for (int i = 0; i < shardsResponses.length(); i++) { - Object shardResponse = shardsResponses.get(i); - if (shardResponse == null) { - // simply ignore non active shards - } else if (shardResponse instanceof BroadcastShardOperationFailedException) { - failedShards++; - if (shardFailures == null) { - shardFailures = new ArrayList<>(); - } - shardFailures.add(new DefaultShardOperationFailedException((BroadcastShardOperationFailedException) shardResponse)); - } else { - Suggest suggest = ((ShardSuggestResponse) shardResponse).getSuggest(); - Suggest.group(groupedSuggestions, suggest); - successfulShards++; - } - } - - return new SuggestResponse(new Suggest(Suggest.reduce(groupedSuggestions)), shardsResponses.length(), - successfulShards, failedShards, shardFailures); - } - - @Override - protected ShardSuggestResponse shardOperation(ShardSuggestRequest request) { - IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex()); - IndexShard indexShard = indexService.getShard(request.shardId().id()); - ShardSuggestMetric suggestMetric = indexShard.getSuggestMetric(); - suggestMetric.preSuggest(); - long startTime = System.nanoTime(); - try (Engine.Searcher searcher = indexShard.acquireSearcher("suggest")) { - SuggestBuilder suggest = request.suggest(); - if (suggest != null) { - final SuggestionSearchContext context = suggest.build(indexService.newQueryShardContext()); - final Suggest result = suggestPhase.execute(context, searcher.searcher()); - return new ShardSuggestResponse(request.shardId(), result); - } - return new ShardSuggestResponse(request.shardId(), new Suggest()); - } catch (Throwable ex) { - throw new ElasticsearchException("failed to execute suggest", ex); - } finally { - suggestMetric.postSuggest(System.nanoTime() - startTime); - } - } -} diff --git a/core/src/main/java/org/elasticsearch/action/suggest/package-info.java b/core/src/main/java/org/elasticsearch/action/suggest/package-info.java deleted file mode 100644 index a2c0f48ea51b1..0000000000000 --- a/core/src/main/java/org/elasticsearch/action/suggest/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/** - * Suggest action. - */ -package org.elasticsearch.action.suggest; \ No newline at end of file diff --git a/core/src/main/java/org/elasticsearch/client/Client.java b/core/src/main/java/org/elasticsearch/client/Client.java index e5d8d4f55b7be..1c7676bc91cda 100644 --- a/core/src/main/java/org/elasticsearch/client/Client.java +++ b/core/src/main/java/org/elasticsearch/client/Client.java @@ -68,9 +68,6 @@ import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchScrollRequest; import org.elasticsearch.action.search.SearchScrollRequestBuilder; -import org.elasticsearch.action.suggest.SuggestRequest; -import org.elasticsearch.action.suggest.SuggestRequestBuilder; -import org.elasticsearch.action.suggest.SuggestResponse; import org.elasticsearch.action.termvectors.MultiTermVectorsRequest; import org.elasticsearch.action.termvectors.MultiTermVectorsRequestBuilder; import org.elasticsearch.action.termvectors.MultiTermVectorsResponse; @@ -367,29 +364,6 @@ public interface Client extends ElasticsearchClient, Releasable { */ MultiGetRequestBuilder prepareMultiGet(); - /** - * Suggestion matching a specific phrase. - * - * @param request The suggest request - * @return The result future - * @see Requests#suggestRequest(String...) - */ - ActionFuture suggest(SuggestRequest request); - - /** - * Suggestions matching a specific phrase. - * - * @param request The suggest request - * @param listener A listener to be notified of the result - * @see Requests#suggestRequest(String...) - */ - void suggest(SuggestRequest request, ActionListener listener); - - /** - * Suggestions matching a specific phrase. - */ - SuggestRequestBuilder prepareSuggest(String... indices); - /** * Search across one or more indices and one or more types with a query. * diff --git a/core/src/main/java/org/elasticsearch/client/Requests.java b/core/src/main/java/org/elasticsearch/client/Requests.java index 53c6abd971c6d..276bd9d906276 100644 --- a/core/src/main/java/org/elasticsearch/client/Requests.java +++ b/core/src/main/java/org/elasticsearch/client/Requests.java @@ -60,9 +60,7 @@ import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchScrollRequest; -import org.elasticsearch.action.suggest.SuggestRequest; import org.elasticsearch.common.xcontent.XContentType; -import org.elasticsearch.search.suggest.SuggestBuilder; /** * A handy one stop shop for creating requests (make sure to import static this class). @@ -126,16 +124,6 @@ public static GetRequest getRequest(String index) { return new GetRequest(index); } - /** - * Creates a suggest request for getting suggestions from provided indices. - * The suggest query has to be set using {@link org.elasticsearch.action.suggest.SuggestRequest#suggest(SuggestBuilder)}. - * @param indices The indices to suggest from. Use null or _all to execute against all indices - * @see org.elasticsearch.client.Client#suggest(org.elasticsearch.action.suggest.SuggestRequest) - */ - public static SuggestRequest suggestRequest(String... indices) { - return new SuggestRequest(indices); - } - /** * Creates a search request against one or more indices. Note, the search source must be set either using the * actual JSON search source, or the {@link org.elasticsearch.search.builder.SearchSourceBuilder}. diff --git a/core/src/main/java/org/elasticsearch/client/support/AbstractClient.java b/core/src/main/java/org/elasticsearch/client/support/AbstractClient.java index f729d5287df12..0044890ee3561 100644 --- a/core/src/main/java/org/elasticsearch/client/support/AbstractClient.java +++ b/core/src/main/java/org/elasticsearch/client/support/AbstractClient.java @@ -314,10 +314,6 @@ import org.elasticsearch.action.search.SearchScrollAction; import org.elasticsearch.action.search.SearchScrollRequest; import org.elasticsearch.action.search.SearchScrollRequestBuilder; -import org.elasticsearch.action.suggest.SuggestAction; -import org.elasticsearch.action.suggest.SuggestRequest; -import org.elasticsearch.action.suggest.SuggestRequestBuilder; -import org.elasticsearch.action.suggest.SuggestResponse; import org.elasticsearch.action.support.PlainActionFuture; import org.elasticsearch.action.support.ThreadedActionListener; import org.elasticsearch.action.termvectors.MultiTermVectorsAction; @@ -660,21 +656,6 @@ public MultiSearchRequestBuilder prepareMultiSearch() { return new MultiSearchRequestBuilder(this, MultiSearchAction.INSTANCE); } - @Override - public ActionFuture suggest(final SuggestRequest request) { - return execute(SuggestAction.INSTANCE, request); - } - - @Override - public void suggest(final SuggestRequest request, final ActionListener listener) { - execute(SuggestAction.INSTANCE, request, listener); - } - - @Override - public SuggestRequestBuilder prepareSuggest(String... indices) { - return new SuggestRequestBuilder(this, SuggestAction.INSTANCE).setIndices(indices); - } - @Override public ActionFuture termVectors(final TermVectorsRequest request) { return execute(TermVectorsAction.INSTANCE, request); diff --git a/core/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java b/core/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java index c35a4cdbadb97..bcabd7c5e125f 100644 --- a/core/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java +++ b/core/src/main/java/org/elasticsearch/index/search/stats/SearchStats.java @@ -51,6 +51,10 @@ public static class Stats implements Streamable, ToXContent { private long scrollTimeInMillis; private long scrollCurrent; + private long suggestCount; + private long suggestTimeInMillis; + private long suggestCurrent; + Stats() { } @@ -58,7 +62,8 @@ public static class Stats implements Streamable, ToXContent { public Stats( long queryCount, long queryTimeInMillis, long queryCurrent, long fetchCount, long fetchTimeInMillis, long fetchCurrent, - long scrollCount, long scrollTimeInMillis, long scrollCurrent + long scrollCount, long scrollTimeInMillis, long scrollCurrent, + long suggestCount, long suggestTimeInMillis, long suggestCurrent ) { this.queryCount = queryCount; this.queryTimeInMillis = queryTimeInMillis; @@ -71,13 +76,19 @@ public Stats( this.scrollCount = scrollCount; this.scrollTimeInMillis = scrollTimeInMillis; this.scrollCurrent = scrollCurrent; + + this.suggestCount = suggestCount; + this.suggestTimeInMillis = suggestTimeInMillis; + this.suggestCurrent = suggestCurrent; + } public Stats(Stats stats) { this( stats.queryCount, stats.queryTimeInMillis, stats.queryCurrent, stats.fetchCount, stats.fetchTimeInMillis, stats.fetchCurrent, - stats.scrollCount, stats.scrollTimeInMillis, stats.scrollCurrent + stats.scrollCount, stats.scrollTimeInMillis, stats.scrollCurrent, + stats.suggestCount, stats.suggestTimeInMillis, stats.suggestCurrent ); } @@ -93,6 +104,10 @@ public void add(Stats stats) { scrollCount += stats.scrollCount; scrollTimeInMillis += stats.scrollTimeInMillis; scrollCurrent += stats.scrollCurrent; + + suggestCount += stats.suggestCount; + suggestTimeInMillis += stats.suggestTimeInMillis; + suggestCurrent += stats.suggestCurrent; } public long getQueryCount() { @@ -143,6 +158,22 @@ public long getScrollCurrent() { return scrollCurrent; } + public long getSuggestCount() { + return suggestCount; + } + + public long getSuggestTimeInMillis() { + return suggestTimeInMillis; + } + + public TimeValue getSuggestTime() { + return new TimeValue(suggestTimeInMillis); + } + + public long getSuggestCurrent() { + return suggestCurrent; + } + public static Stats readStats(StreamInput in) throws IOException { Stats stats = new Stats(); stats.readFrom(in); @@ -162,6 +193,10 @@ public void readFrom(StreamInput in) throws IOException { scrollCount = in.readVLong(); scrollTimeInMillis = in.readVLong(); scrollCurrent = in.readVLong(); + + suggestCount = in.readVLong(); + suggestTimeInMillis = in.readVLong(); + suggestCurrent = in.readVLong(); } @Override @@ -177,6 +212,10 @@ public void writeTo(StreamOutput out) throws IOException { out.writeVLong(scrollCount); out.writeVLong(scrollTimeInMillis); out.writeVLong(scrollCurrent); + + out.writeVLong(suggestCount); + out.writeVLong(suggestTimeInMillis); + out.writeVLong(suggestCurrent); } @Override @@ -193,6 +232,10 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws builder.timeValueField(Fields.SCROLL_TIME_IN_MILLIS, Fields.SCROLL_TIME, scrollTimeInMillis); builder.field(Fields.SCROLL_CURRENT, scrollCurrent); + builder.field(Fields.SUGGEST_TOTAL, suggestCount); + builder.timeValueField(Fields.SUGGEST_TIME_IN_MILLIS, Fields.SUGGEST_TIME, suggestTimeInMillis); + builder.field(Fields.SUGGEST_CURRENT, suggestCurrent); + return builder; } } @@ -292,6 +335,10 @@ static final class Fields { static final XContentBuilderString SCROLL_TIME = new XContentBuilderString("scroll_time"); static final XContentBuilderString SCROLL_TIME_IN_MILLIS = new XContentBuilderString("scroll_time_in_millis"); static final XContentBuilderString SCROLL_CURRENT = new XContentBuilderString("scroll_current"); + static final XContentBuilderString SUGGEST_TOTAL = new XContentBuilderString("suggest_total"); + static final XContentBuilderString SUGGEST_TIME = new XContentBuilderString("suggest_time"); + static final XContentBuilderString SUGGEST_TIME_IN_MILLIS = new XContentBuilderString("suggest_time_in_millis"); + static final XContentBuilderString SUGGEST_CURRENT = new XContentBuilderString("suggest_current"); } public static SearchStats readSearchStats(StreamInput in) throws IOException { diff --git a/core/src/main/java/org/elasticsearch/index/search/stats/ShardSearchStats.java b/core/src/main/java/org/elasticsearch/index/search/stats/ShardSearchStats.java index 1a155d17964d6..748bb01bd54b5 100644 --- a/core/src/main/java/org/elasticsearch/index/search/stats/ShardSearchStats.java +++ b/core/src/main/java/org/elasticsearch/index/search/stats/ShardSearchStats.java @@ -23,13 +23,13 @@ import org.elasticsearch.common.metrics.CounterMetric; import org.elasticsearch.common.metrics.MeanMetric; import org.elasticsearch.common.regex.Regex; -import org.elasticsearch.common.settings.Settings; import org.elasticsearch.index.SearchSlowLog; import org.elasticsearch.search.internal.SearchContext; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.function.Consumer; import static java.util.Collections.emptyMap; @@ -72,64 +72,51 @@ public SearchStats stats(String... groups) { } public void onPreQueryPhase(SearchContext searchContext) { - totalStats.queryCurrent.inc(); - if (searchContext.groupStats() != null) { - for (int i = 0; i < searchContext.groupStats().size(); i++) { - groupStats(searchContext.groupStats().get(i)).queryCurrent.inc(); + computeStats(searchContext, statsHolder -> { + if (searchContext.hasOnlySuggest()) { + statsHolder.suggestCurrent.inc(); + } else { + statsHolder.queryCurrent.inc(); } - } + }); } public void onFailedQueryPhase(SearchContext searchContext) { - totalStats.queryCurrent.dec(); - if (searchContext.groupStats() != null) { - for (int i = 0; i < searchContext.groupStats().size(); i++) { - groupStats(searchContext.groupStats().get(i)).queryCurrent.dec(); + computeStats(searchContext, statsHolder -> { + if (searchContext.hasOnlySuggest()) { + statsHolder.suggestCurrent.dec(); + } else { + statsHolder.queryCurrent.dec(); } - } + }); } public void onQueryPhase(SearchContext searchContext, long tookInNanos) { - totalStats.queryMetric.inc(tookInNanos); - totalStats.queryCurrent.dec(); - if (searchContext.groupStats() != null) { - for (int i = 0; i < searchContext.groupStats().size(); i++) { - StatsHolder statsHolder = groupStats(searchContext.groupStats().get(i)); + computeStats(searchContext, statsHolder -> { + if (searchContext.hasOnlySuggest()) { + statsHolder.suggestMetric.inc(tookInNanos); + statsHolder.suggestCurrent.dec(); + } else { statsHolder.queryMetric.inc(tookInNanos); statsHolder.queryCurrent.dec(); } - } + }); slowLogSearchService.onQueryPhase(searchContext, tookInNanos); } public void onPreFetchPhase(SearchContext searchContext) { - totalStats.fetchCurrent.inc(); - if (searchContext.groupStats() != null) { - for (int i = 0; i < searchContext.groupStats().size(); i++) { - groupStats(searchContext.groupStats().get(i)).fetchCurrent.inc(); - } - } + computeStats(searchContext, statsHolder -> statsHolder.fetchCurrent.inc()); } public void onFailedFetchPhase(SearchContext searchContext) { - totalStats.fetchCurrent.dec(); - if (searchContext.groupStats() != null) { - for (int i = 0; i < searchContext.groupStats().size(); i++) { - groupStats(searchContext.groupStats().get(i)).fetchCurrent.dec(); - } - } + computeStats(searchContext, statsHolder -> statsHolder.fetchCurrent.dec()); } public void onFetchPhase(SearchContext searchContext, long tookInNanos) { - totalStats.fetchMetric.inc(tookInNanos); - totalStats.fetchCurrent.dec(); - if (searchContext.groupStats() != null) { - for (int i = 0; i < searchContext.groupStats().size(); i++) { - StatsHolder statsHolder = groupStats(searchContext.groupStats().get(i)); - statsHolder.fetchMetric.inc(tookInNanos); - statsHolder.fetchCurrent.dec(); - } - } + computeStats(searchContext, statsHolder -> { + statsHolder.fetchMetric.inc(tookInNanos); + statsHolder.fetchCurrent.dec(); + }); slowLogSearchService.onFetchPhase(searchContext, tookInNanos); } @@ -149,6 +136,15 @@ public void clear() { } } + private void computeStats(SearchContext searchContext, Consumer consumer) { + consumer.accept(totalStats); + if (searchContext.groupStats() != null) { + for (int i = 0; i < searchContext.groupStats().size(); i++) { + consumer.accept(groupStats(searchContext.groupStats().get(i))); + } + } + } + private StatsHolder groupStats(String group) { StatsHolder stats = groupsStats.get(group); if (stats == null) { @@ -184,26 +180,30 @@ final static class StatsHolder { public final MeanMetric queryMetric = new MeanMetric(); public final MeanMetric fetchMetric = new MeanMetric(); public final MeanMetric scrollMetric = new MeanMetric(); + public final MeanMetric suggestMetric = new MeanMetric(); public final CounterMetric queryCurrent = new CounterMetric(); public final CounterMetric fetchCurrent = new CounterMetric(); public final CounterMetric scrollCurrent = new CounterMetric(); + public final CounterMetric suggestCurrent = new CounterMetric(); public SearchStats.Stats stats() { return new SearchStats.Stats( queryMetric.count(), TimeUnit.NANOSECONDS.toMillis(queryMetric.sum()), queryCurrent.count(), fetchMetric.count(), TimeUnit.NANOSECONDS.toMillis(fetchMetric.sum()), fetchCurrent.count(), - scrollMetric.count(), TimeUnit.NANOSECONDS.toMillis(scrollMetric.sum()), scrollCurrent.count() + scrollMetric.count(), TimeUnit.NANOSECONDS.toMillis(scrollMetric.sum()), scrollCurrent.count(), + suggestMetric.count(), TimeUnit.NANOSECONDS.toMillis(suggestMetric.sum()), suggestCurrent.count() ); } public long totalCurrent() { - return queryCurrent.count() + fetchCurrent.count() + scrollCurrent.count(); + return queryCurrent.count() + fetchCurrent.count() + scrollCurrent.count() + suggestCurrent.count(); } public void clear() { queryMetric.clear(); fetchMetric.clear(); scrollMetric.clear(); + suggestMetric.clear(); } } } diff --git a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java index 7b19a12bbaa9f..b667a1de689c9 100644 --- a/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java +++ b/core/src/main/java/org/elasticsearch/index/shard/IndexShard.java @@ -92,8 +92,6 @@ import org.elasticsearch.index.store.Store.MetadataSnapshot; import org.elasticsearch.index.store.StoreFileMetaData; import org.elasticsearch.index.store.StoreStats; -import org.elasticsearch.index.suggest.stats.ShardSuggestMetric; -import org.elasticsearch.index.suggest.stats.SuggestStats; import org.elasticsearch.index.translog.Translog; import org.elasticsearch.index.translog.TranslogConfig; import org.elasticsearch.index.translog.TranslogStats; @@ -135,7 +133,6 @@ public class IndexShard extends AbstractIndexShardComponent { private final ShardRequestCache shardQueryCache; private final ShardFieldData shardFieldData; private final IndexFieldDataService indexFieldDataService; - private final ShardSuggestMetric shardSuggestMetric = new ShardSuggestMetric(); private final ShardBitsetFilterCache shardBitsetFilterCache; private final Object mutex = new Object(); private final String checkIndexOnStartup; @@ -254,10 +251,6 @@ public ShardGetService getService() { return this.getService; } - public ShardSuggestMetric getSuggestMetric() { - return shardSuggestMetric; - } - public ShardBitsetFilterCache shardBitsetFilterCache() { return shardBitsetFilterCache; } @@ -631,10 +624,6 @@ public TranslogStats translogStats() { return getEngine().getTranslog().stats(); } - public SuggestStats suggestStats() { - return shardSuggestMetric.stats(); - } - public CompletionStats completionStats(String... fields) { CompletionStats completionStats = new CompletionStats(); try (final Engine.Searcher currentSearcher = acquireSearcher("completion_stats")) { diff --git a/core/src/main/java/org/elasticsearch/index/suggest/stats/ShardSuggestMetric.java b/core/src/main/java/org/elasticsearch/index/suggest/stats/ShardSuggestMetric.java deleted file mode 100644 index 750d7de7b22f9..0000000000000 --- a/core/src/main/java/org/elasticsearch/index/suggest/stats/ShardSuggestMetric.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.suggest.stats; - -import org.elasticsearch.common.metrics.CounterMetric; -import org.elasticsearch.common.metrics.MeanMetric; - -import java.util.concurrent.TimeUnit; - -/** - * - */ -public final class ShardSuggestMetric { - private final MeanMetric suggestMetric = new MeanMetric(); - private final CounterMetric currentMetric = new CounterMetric(); - - /** - * Called before suggest - */ - public void preSuggest() { - currentMetric.inc(); - } - - /** - * Called after suggest - * @param tookInNanos time of suggest used in nanos - */ - public void postSuggest(long tookInNanos) { - currentMetric.dec(); - suggestMetric.inc(tookInNanos); - } - - /** - * @return The current stats - */ - public SuggestStats stats() { - return new SuggestStats(suggestMetric.count(), TimeUnit.NANOSECONDS.toMillis(suggestMetric.sum()), currentMetric.count()); - } -} diff --git a/core/src/main/java/org/elasticsearch/index/suggest/stats/SuggestStats.java b/core/src/main/java/org/elasticsearch/index/suggest/stats/SuggestStats.java deleted file mode 100644 index 1183a64833ae0..0000000000000 --- a/core/src/main/java/org/elasticsearch/index/suggest/stats/SuggestStats.java +++ /dev/null @@ -1,124 +0,0 @@ -/* - * Licensed to Elasticsearch under one or more contributor - * license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright - * ownership. Elasticsearch licenses this file to you under - * the Apache License, Version 2.0 (the "License"); you may - * not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.elasticsearch.index.suggest.stats; - -import org.elasticsearch.common.io.stream.StreamInput; -import org.elasticsearch.common.io.stream.StreamOutput; -import org.elasticsearch.common.io.stream.Streamable; -import org.elasticsearch.common.unit.TimeValue; -import org.elasticsearch.common.xcontent.ToXContent; -import org.elasticsearch.common.xcontent.XContentBuilder; -import org.elasticsearch.common.xcontent.XContentBuilderString; - -import java.io.IOException; - -/** - * Exposes suggest related statistics. - */ -public class SuggestStats implements Streamable, ToXContent { - - private long suggestCount; - private long suggestTimeInMillis; - private long current; - - public SuggestStats() { - } - - SuggestStats(long suggestCount, long suggestTimeInMillis, long current) { - this.suggestCount = suggestCount; - this.suggestTimeInMillis = suggestTimeInMillis; - this.current = current; - } - - /** - * @return The number of times the suggest api has been invoked. - */ - public long getCount() { - return suggestCount; - } - - /** - * @return The total amount of time spend in the suggest api - */ - public long getTimeInMillis() { - return suggestTimeInMillis; - } - - /** - * @return The total amount of time spend in the suggest api - */ - public TimeValue getTime() { - return new TimeValue(getTimeInMillis()); - } - - /** - * @return The total amount of active suggest api invocations. - */ - public long getCurrent() { - return current; - } - - public void add(SuggestStats suggestStats) { - if (suggestStats != null) { - suggestCount += suggestStats.getCount(); - suggestTimeInMillis += suggestStats.getTimeInMillis(); - current += suggestStats.getCurrent(); - } - } - - public static SuggestStats readSuggestStats(StreamInput in) throws IOException { - SuggestStats stats = new SuggestStats(); - stats.readFrom(in); - return stats; - } - - static final class Fields { - static final XContentBuilderString SUGGEST = new XContentBuilderString("suggest"); - static final XContentBuilderString TOTAL = new XContentBuilderString("total"); - static final XContentBuilderString TIME = new XContentBuilderString("time"); - static final XContentBuilderString TIME_IN_MILLIS = new XContentBuilderString("time_in_millis"); - static final XContentBuilderString CURRENT = new XContentBuilderString("current"); - } - - - @Override - public void readFrom(StreamInput in) throws IOException { - suggestCount = in.readVLong(); - suggestTimeInMillis = in.readVLong(); - current = in.readVLong(); - } - - @Override - public void writeTo(StreamOutput out) throws IOException { - out.writeVLong(suggestCount); - out.writeVLong(suggestTimeInMillis); - out.writeVLong(current); - } - - @Override - public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { - builder.startObject(Fields.SUGGEST); - builder.field(Fields.TOTAL, suggestCount); - builder.timeValueField(Fields.TIME_IN_MILLIS, Fields.TIME, suggestTimeInMillis); - builder.field(Fields.CURRENT, current); - builder.endObject(); - return builder; - } -} diff --git a/core/src/main/java/org/elasticsearch/indices/NodeIndicesStats.java b/core/src/main/java/org/elasticsearch/indices/NodeIndicesStats.java index fce4e8411dbb4..237975f2899a1 100644 --- a/core/src/main/java/org/elasticsearch/indices/NodeIndicesStats.java +++ b/core/src/main/java/org/elasticsearch/indices/NodeIndicesStats.java @@ -44,7 +44,6 @@ import org.elasticsearch.index.search.stats.SearchStats; import org.elasticsearch.index.shard.DocsStats; import org.elasticsearch.index.store.StoreStats; -import org.elasticsearch.index.suggest.stats.SuggestStats; import org.elasticsearch.search.suggest.completion.CompletionStats; import java.io.IOException; @@ -149,11 +148,6 @@ public SegmentsStats getSegments() { return stats.getSegments(); } - @Nullable - public SuggestStats getSuggest() { - return stats.getSuggest(); - } - @Nullable public RecoveryStats getRecoveryStats() { return stats.getRecoveryStats(); diff --git a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/stats/RestIndicesStatsAction.java b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/stats/RestIndicesStatsAction.java index 92fb21db38c5d..dbda83709baff 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/admin/indices/stats/RestIndicesStatsAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/admin/indices/stats/RestIndicesStatsAction.java @@ -71,7 +71,7 @@ public void handleRequest(final RestRequest request, final RestChannel channel, indicesStatsRequest.docs(metrics.contains("docs")); indicesStatsRequest.store(metrics.contains("store")); indicesStatsRequest.indexing(metrics.contains("indexing")); - indicesStatsRequest.search(metrics.contains("search")); + indicesStatsRequest.search(metrics.contains("search") || metrics.contains("suggest")); indicesStatsRequest.get(metrics.contains("get")); indicesStatsRequest.merge(metrics.contains("merge")); indicesStatsRequest.refresh(metrics.contains("refresh")); @@ -82,7 +82,6 @@ public void handleRequest(final RestRequest request, final RestChannel channel, indicesStatsRequest.segments(metrics.contains("segments")); indicesStatsRequest.fieldData(metrics.contains("fielddata")); indicesStatsRequest.completion(metrics.contains("completion")); - indicesStatsRequest.suggest(metrics.contains("suggest")); indicesStatsRequest.requestCache(metrics.contains("request_cache")); indicesStatsRequest.recovery(metrics.contains("recovery")); indicesStatsRequest.translog(metrics.contains("translog")); diff --git a/core/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java b/core/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java index 398ef9c15f771..958fa40b54b1b 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/cat/RestIndicesAction.java @@ -490,14 +490,14 @@ private Table buildTable(RestRequest request, String[] indices, ClusterHealthRes table.addCell(indexStats == null ? null : indexStats.getTotal().getWarmer().totalTime()); table.addCell(indexStats == null ? null : indexStats.getPrimaries().getWarmer().totalTime()); - table.addCell(indexStats == null ? null : indexStats.getTotal().getSuggest().getCurrent()); - table.addCell(indexStats == null ? null : indexStats.getPrimaries().getSuggest().getCurrent()); + table.addCell(indexStats == null ? null : indexStats.getTotal().getSearch().getTotal().getSuggestCurrent()); + table.addCell(indexStats == null ? null : indexStats.getPrimaries().getSearch().getTotal().getSuggestCurrent()); - table.addCell(indexStats == null ? null : indexStats.getTotal().getSuggest().getTime()); - table.addCell(indexStats == null ? null : indexStats.getPrimaries().getSuggest().getTime()); + table.addCell(indexStats == null ? null : indexStats.getTotal().getSearch().getTotal().getSuggestTime()); + table.addCell(indexStats == null ? null : indexStats.getPrimaries().getSearch().getTotal().getSuggestTime()); - table.addCell(indexStats == null ? null : indexStats.getTotal().getSuggest().getCount()); - table.addCell(indexStats == null ? null : indexStats.getPrimaries().getSuggest().getCount()); + table.addCell(indexStats == null ? null : indexStats.getTotal().getSearch().getTotal().getSuggestCount()); + table.addCell(indexStats == null ? null : indexStats.getPrimaries().getSearch().getTotal().getSuggestCount()); table.addCell(indexStats == null ? null : indexStats.getTotal().getTotalMemory()); table.addCell(indexStats == null ? null : indexStats.getPrimaries().getTotalMemory()); diff --git a/core/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java b/core/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java index c7b1550cbae36..54ee855b54364 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/cat/RestNodesAction.java @@ -49,7 +49,6 @@ import org.elasticsearch.index.percolator.PercolatorQueryCacheStats; import org.elasticsearch.index.refresh.RefreshStats; import org.elasticsearch.index.search.stats.SearchStats; -import org.elasticsearch.index.suggest.stats.SuggestStats; import org.elasticsearch.indices.NodeIndicesStats; import org.elasticsearch.monitor.fs.FsInfo; import org.elasticsearch.monitor.jvm.JvmInfo; @@ -362,10 +361,9 @@ private Table buildTable(RestRequest req, ClusterStateResponse state, NodesInfoR table.addCell(segmentsStats == null ? null : segmentsStats.getVersionMapMemory()); table.addCell(segmentsStats == null ? null : segmentsStats.getBitsetMemory()); - SuggestStats suggestStats = indicesStats == null ? null : indicesStats.getSuggest(); - table.addCell(suggestStats == null ? null : suggestStats.getCurrent()); - table.addCell(suggestStats == null ? null : suggestStats.getTime()); - table.addCell(suggestStats == null ? null : suggestStats.getCount()); + table.addCell(searchStats == null ? null : searchStats.getTotal().getSuggestCurrent()); + table.addCell(searchStats == null ? null : searchStats.getTotal().getSuggestTime()); + table.addCell(searchStats == null ? null : searchStats.getTotal().getSuggestCount()); table.endRow(); } diff --git a/core/src/main/java/org/elasticsearch/rest/action/suggest/RestSuggestAction.java b/core/src/main/java/org/elasticsearch/rest/action/suggest/RestSuggestAction.java index 53d9e668de1a0..1bbc662929c0b 100644 --- a/core/src/main/java/org/elasticsearch/rest/action/suggest/RestSuggestAction.java +++ b/core/src/main/java/org/elasticsearch/rest/action/suggest/RestSuggestAction.java @@ -19,14 +19,15 @@ package org.elasticsearch.rest.action.suggest; -import org.elasticsearch.action.suggest.SuggestRequest; -import org.elasticsearch.action.suggest.SuggestResponse; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.client.Client; import org.elasticsearch.common.Strings; import org.elasticsearch.common.bytes.BytesReference; import org.elasticsearch.common.inject.Inject; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.common.xcontent.ToXContent; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentParser; @@ -41,6 +42,7 @@ import org.elasticsearch.rest.RestStatus; import org.elasticsearch.rest.action.support.RestActions; import org.elasticsearch.rest.action.support.RestBuilderListener; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.suggest.Suggest; import org.elasticsearch.search.suggest.SuggestBuilder; import org.elasticsearch.search.suggest.Suggesters; @@ -73,28 +75,29 @@ public RestSuggestAction(Settings settings, RestController controller, Client cl @Override public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws IOException { - SuggestRequest suggestRequest = new SuggestRequest(Strings.splitStringByCommaToArray(request.param("index"))); - suggestRequest.indicesOptions(IndicesOptions.fromRequest(request, suggestRequest.indicesOptions())); + final SearchRequest searchRequest = new SearchRequest(Strings.splitStringByCommaToArray(request.param("index")), new SearchSourceBuilder()); + searchRequest.indicesOptions(IndicesOptions.fromRequest(request, searchRequest.indicesOptions())); if (RestActions.hasBodyContent(request)) { final BytesReference sourceBytes = RestActions.getRestContent(request); try (XContentParser parser = XContentFactory.xContent(sourceBytes).createParser(sourceBytes)) { final QueryParseContext context = new QueryParseContext(queryRegistry); context.reset(parser); context.parseFieldMatcher(parseFieldMatcher); - suggestRequest.suggest(SuggestBuilder.fromXContent(context, suggesters)); + searchRequest.source().suggest(SuggestBuilder.fromXContent(context, suggesters)); } } else { throw new IllegalArgumentException("no content or source provided to execute suggestion"); } - suggestRequest.routing(request.param("routing")); - suggestRequest.preference(request.param("preference")); - - client.suggest(suggestRequest, new RestBuilderListener(channel) { + searchRequest.routing(request.param("routing")); + searchRequest.preference(request.param("preference")); + client.search(searchRequest, new RestBuilderListener(channel) { @Override - public RestResponse buildResponse(SuggestResponse response, XContentBuilder builder) throws Exception { - RestStatus restStatus = RestStatus.status(response.getSuccessfulShards(), response.getTotalShards(), response.getShardFailures()); + public RestResponse buildResponse(SearchResponse response, XContentBuilder builder) throws Exception { + RestStatus restStatus = RestStatus.status(response.getSuccessfulShards(), + response.getTotalShards(), response.getShardFailures()); builder.startObject(); - buildBroadcastShardsHeader(builder, request, response); + buildBroadcastShardsHeader(builder, request, response.getTotalShards(), + response.getSuccessfulShards(), response.getFailedShards(), response.getShardFailures()); Suggest suggest = response.getSuggest(); if (suggest != null) { suggest.toInnerXContent(builder, request); diff --git a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index eae12def5e447..c83794ced906e 100644 --- a/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/core/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -729,6 +729,14 @@ public BytesReference ext() { return ext; } + /** + * @return true if the source only has suggest + */ + public boolean isSuggestOnly() { + return suggestBuilder != null + && queryBuilder == null && aggregations == null; + } + /** * Rewrites this search source builder into its primitive form. e.g. by * rewriting the QueryBuilder. If the builder did not change the identity diff --git a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index 71a289331f875..f3fe48f6682cd 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -191,6 +191,9 @@ public void doClose() { */ @Override public void preProcess() { + if (hasOnlySuggest() ) { + return; + } if (scrollContext == null) { long from = from() == -1 ? 0 : from(); long size = size() == -1 ? 10 : size(); diff --git a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java index 1881109c9b656..f9d0b6b9283ba 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -353,6 +353,14 @@ public void clearReleasables(Lifetime lifetime) { } } + /** + * @return true if the request contains only suggest + */ + public final boolean hasOnlySuggest() { + return request().source() != null + && request().source().isSuggestOnly(); + } + /** * Looks up the given field, but does not restrict to fields in the types set on this context. */ diff --git a/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java b/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java index f40f253e3aea1..62210655a006b 100644 --- a/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java +++ b/core/src/main/java/org/elasticsearch/search/query/QueryPhase.java @@ -116,6 +116,12 @@ public void preProcess(SearchContext context) { @Override public void execute(SearchContext searchContext) throws QueryPhaseExecutionException { + if (searchContext.hasOnlySuggest()) { + suggestPhase.execute(searchContext); + // TODO: fix this once we can fetch docs for suggestions + searchContext.queryResult().topDocs(new TopDocs(0, Lucene.EMPTY_SCORE_DOCS, 0)); + return; + } // Pre-process aggregations as late as possible. In the case of a DFS_Q_T_F // request, preProcess is called on the DFS phase phase, this is why we pre-process them // here to make sure it happens during the QUERY phase diff --git a/core/src/main/java/org/elasticsearch/search/suggest/SuggestPhase.java b/core/src/main/java/org/elasticsearch/search/suggest/SuggestPhase.java index 8831d5d093acc..520322587bb2e 100644 --- a/core/src/main/java/org/elasticsearch/search/suggest/SuggestPhase.java +++ b/core/src/main/java/org/elasticsearch/search/suggest/SuggestPhase.java @@ -18,7 +18,6 @@ */ package org.elasticsearch.search.suggest; -import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.util.CharsRefBuilder; import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.component.AbstractComponent; @@ -65,10 +64,6 @@ public void execute(SearchContext context) { if (suggest == null) { return; } - context.queryResult().suggest(execute(suggest, context.searcher())); - } - - public Suggest execute(SuggestionSearchContext suggest, IndexSearcher searcher) { try { CharsRefBuilder spare = new CharsRefBuilder(); final List>> suggestions = new ArrayList<>(suggest.suggestions().size()); @@ -76,14 +71,14 @@ public Suggest execute(SuggestionSearchContext suggest, IndexSearcher searcher) for (Map.Entry entry : suggest.suggestions().entrySet()) { SuggestionSearchContext.SuggestionContext suggestion = entry.getValue(); Suggester suggester = suggestion.getSuggester(); - Suggestion> result = suggester.execute(entry.getKey(), suggestion, searcher, spare); + Suggestion> result = + suggester.execute(entry.getKey(), suggestion, context.searcher(), spare); if (result != null) { assert entry.getKey().equals(result.name); suggestions.add(result); } } - - return new Suggest(suggestions); + context.queryResult().suggest(new Suggest(suggestions)); } catch (IOException e) { throw new ElasticsearchException("I/O exception during suggest phase", e); } diff --git a/core/src/test/java/org/elasticsearch/index/suggest/stats/SuggestStatsIT.java b/core/src/test/java/org/elasticsearch/index/suggest/stats/SuggestStatsIT.java index 8711e89883f69..62dec09e906eb 100644 --- a/core/src/test/java/org/elasticsearch/index/suggest/stats/SuggestStatsIT.java +++ b/core/src/test/java/org/elasticsearch/index/suggest/stats/SuggestStatsIT.java @@ -22,13 +22,15 @@ import org.elasticsearch.action.admin.cluster.node.stats.NodeStats; import org.elasticsearch.action.admin.cluster.node.stats.NodesStatsResponse; import org.elasticsearch.action.admin.indices.stats.IndicesStatsResponse; -import org.elasticsearch.action.suggest.SuggestRequestBuilder; -import org.elasticsearch.action.suggest.SuggestResponse; +import org.elasticsearch.action.search.SearchRequestBuilder; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.cluster.ClusterState; import org.elasticsearch.cluster.routing.GroupShardsIterator; import org.elasticsearch.cluster.routing.ShardIterator; import org.elasticsearch.cluster.routing.ShardRouting; import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.index.search.stats.SearchStats; +import org.elasticsearch.search.suggest.SuggestBuilder; import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import org.elasticsearch.test.ESIntegTestCase; @@ -86,49 +88,50 @@ public void testSimpleStats() throws Exception { long startTime = System.currentTimeMillis(); for (int i = 0; i < suggestAllIdx; i++) { - SuggestResponse suggestResponse = addSuggestions(internalCluster().clientNodeClient().prepareSuggest(), i).get(); + SearchResponse suggestResponse = addSuggestions(internalCluster().clientNodeClient().prepareSearch(), i).get(); assertAllSuccessful(suggestResponse); } for (int i = 0; i < suggestIdx1; i++) { - SuggestResponse suggestResponse = addSuggestions(internalCluster().clientNodeClient().prepareSuggest("test1"), i).get(); + SearchResponse suggestResponse = addSuggestions(internalCluster().clientNodeClient().prepareSearch("test1"), i).get(); assertAllSuccessful(suggestResponse); } for (int i = 0; i < suggestIdx2; i++) { - SuggestResponse suggestResponse = addSuggestions(internalCluster().clientNodeClient().prepareSuggest("test2"), i).get(); + SearchResponse suggestResponse = addSuggestions(internalCluster().clientNodeClient().prepareSearch("test2"), i).get(); assertAllSuccessful(suggestResponse); } long endTime = System.currentTimeMillis(); IndicesStatsResponse indicesStats = client().admin().indices().prepareStats().execute().actionGet(); + final SearchStats.Stats suggest = indicesStats.getTotal().getSearch().getTotal(); // check current - assertThat(indicesStats.getTotal().getSuggest().getCurrent(), equalTo(0L)); + assertThat(suggest.getSuggestCurrent(), equalTo(0L)); // check suggest count - assertThat(indicesStats.getTotal().getSuggest().getCount(), equalTo((long) (suggestAllIdx * totalShards + suggestIdx1 * shardsIdx1 + suggestIdx2 * shardsIdx2))); - assertThat(indicesStats.getIndices().get("test1").getTotal().getSuggest().getCount(), equalTo((long) ((suggestAllIdx + suggestIdx1) * shardsIdx1))); - assertThat(indicesStats.getIndices().get("test2").getTotal().getSuggest().getCount(), equalTo((long) ((suggestAllIdx + suggestIdx2) * shardsIdx2))); + assertThat(suggest.getSuggestCount(), equalTo((long) (suggestAllIdx * totalShards + suggestIdx1 * shardsIdx1 + suggestIdx2 * shardsIdx2))); + assertThat(indicesStats.getIndices().get("test1").getTotal().getSearch().getTotal().getSuggestCount(), equalTo((long) ((suggestAllIdx + suggestIdx1) * shardsIdx1))); + assertThat(indicesStats.getIndices().get("test2").getTotal().getSearch().getTotal().getSuggestCount(), equalTo((long) ((suggestAllIdx + suggestIdx2) * shardsIdx2))); logger.info("iter {}, iter1 {}, iter2 {}, {}", suggestAllIdx, suggestIdx1, suggestIdx2, endTime - startTime); // check suggest time - assertThat(indicesStats.getTotal().getSuggest().getTimeInMillis(), greaterThan(0L)); + assertThat(suggest.getSuggestTimeInMillis(), greaterThan(0L)); // the upperbound is num shards * total time since we do searches in parallel - assertThat(indicesStats.getTotal().getSuggest().getTimeInMillis(), lessThanOrEqualTo(totalShards * (endTime - startTime))); + assertThat(suggest.getSuggestTimeInMillis(), lessThanOrEqualTo(totalShards * (endTime - startTime))); NodesStatsResponse nodeStats = client().admin().cluster().prepareNodesStats().execute().actionGet(); NodeStats[] nodes = nodeStats.getNodes(); Set nodeIdsWithIndex = nodeIdsWithIndex("test1", "test2"); int num = 0; for (NodeStats stat : nodes) { - SuggestStats suggestStats = stat.getIndices().getSuggest(); + SearchStats.Stats suggestStats = stat.getIndices().getSearch().getTotal(); logger.info("evaluating {}", stat.getNode()); if (nodeIdsWithIndex.contains(stat.getNode().getId())) { - assertThat(suggestStats.getCount(), greaterThan(0L)); - assertThat(suggestStats.getTimeInMillis(), greaterThan(0L)); + assertThat(suggestStats.getSuggestCount(), greaterThan(0L)); + assertThat(suggestStats.getSuggestTimeInMillis(), greaterThan(0L)); num++; } else { - assertThat(suggestStats.getCount(), equalTo(0L)); - assertThat(suggestStats.getTimeInMillis(), equalTo(0L)); + assertThat(suggestStats.getSuggestCount(), equalTo(0L)); + assertThat(suggestStats.getSuggestTimeInMillis(), equalTo(0L)); } } @@ -136,15 +139,16 @@ public void testSimpleStats() throws Exception { } - private SuggestRequestBuilder addSuggestions(SuggestRequestBuilder request, int i) { + private SearchRequestBuilder addSuggestions(SearchRequestBuilder request, int i) { + final SuggestBuilder suggestBuilder = new SuggestBuilder(); for (int s = 0; s < randomIntBetween(2, 10); s++) { if (randomBoolean()) { - request.addSuggestion("s" + s, new PhraseSuggestionBuilder("f").text("test" + i + " test" + (i - 1))); + suggestBuilder.addSuggestion("s" + s, new PhraseSuggestionBuilder("f").text("test" + i + " test" + (i - 1))); } else { - request.addSuggestion("s" + s, new TermSuggestionBuilder("f").text("test" + i)); + suggestBuilder.addSuggestion("s" + s, new TermSuggestionBuilder("f").text("test" + i)); } } - return request; + return request.suggest(suggestBuilder); } private Set nodeIdsWithIndex(String... indices) { diff --git a/core/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java b/core/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java index 8ea053a64ab11..046d0b4400f4b 100644 --- a/core/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java +++ b/core/src/test/java/org/elasticsearch/indices/IndicesOptionsIntegrationIT.java @@ -44,7 +44,6 @@ import org.elasticsearch.action.search.MultiSearchResponse; import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.action.suggest.SuggestRequestBuilder; import org.elasticsearch.action.support.IndicesOptions; import org.elasticsearch.common.Strings; import org.elasticsearch.common.settings.Setting; @@ -53,6 +52,7 @@ import org.elasticsearch.common.settings.SettingsModule; import org.elasticsearch.index.IndexNotFoundException; import org.elasticsearch.plugins.Plugin; +import org.elasticsearch.search.suggest.SuggestBuilder; import org.elasticsearch.search.suggest.SuggestBuilders; import org.elasticsearch.test.ESIntegTestCase; @@ -94,7 +94,6 @@ public void testSpecifiedIndexUnavailableMultipleIndices() throws Exception { verify(typesExists("test1", "test2"), true); verify(percolate("test1", "test2"), true); verify(mpercolate(null, "test1", "test2"), false); - verify(suggest("test1", "test2"), true); verify(getAliases("test1", "test2"), true); verify(getFieldMapping("test1", "test2"), true); verify(getMapping("test1", "test2"), true); @@ -114,7 +113,6 @@ public void testSpecifiedIndexUnavailableMultipleIndices() throws Exception { verify(typesExists("test1", "test2").setIndicesOptions(options), true); verify(percolate("test1", "test2").setIndicesOptions(options), true); verify(mpercolate(options, "test1", "test2").setIndicesOptions(options), false); - verify(suggest("test1", "test2").setIndicesOptions(options), true); verify(getAliases("test1", "test2").setIndicesOptions(options), true); verify(getFieldMapping("test1", "test2").setIndicesOptions(options), true); verify(getMapping("test1", "test2").setIndicesOptions(options), true); @@ -134,7 +132,6 @@ public void testSpecifiedIndexUnavailableMultipleIndices() throws Exception { verify(typesExists("test1", "test2").setIndicesOptions(options), false); verify(percolate("test1", "test2").setIndicesOptions(options), false); verify(mpercolate(options, "test1", "test2").setIndicesOptions(options), false); - verify(suggest("test1", "test2").setIndicesOptions(options), false); verify(getAliases("test1", "test2").setIndicesOptions(options), false); verify(getFieldMapping("test1", "test2").setIndicesOptions(options), false); verify(getMapping("test1", "test2").setIndicesOptions(options), false); @@ -156,7 +153,6 @@ public void testSpecifiedIndexUnavailableMultipleIndices() throws Exception { verify(typesExists("test1", "test2").setIndicesOptions(options), false); verify(percolate("test1", "test2").setIndicesOptions(options), false); verify(mpercolate(options, "test1", "test2").setIndicesOptions(options), false); - verify(suggest("test1", "test2").setIndicesOptions(options), false); verify(getAliases("test1", "test2").setIndicesOptions(options), false); verify(getFieldMapping("test1", "test2").setIndicesOptions(options), false); verify(getMapping("test1", "test2").setIndicesOptions(options), false); @@ -186,7 +182,6 @@ public void testSpecifiedIndexUnavailableSingleIndexThatIsClosed() throws Except verify(typesExists("test1").setIndicesOptions(options), true); verify(percolate("test1").setIndicesOptions(options), true); verify(mpercolate(options, "test1").setIndicesOptions(options), true); - verify(suggest("test1").setIndicesOptions(options), true); verify(getAliases("test1").setIndicesOptions(options), true); verify(getFieldMapping("test1").setIndicesOptions(options), true); verify(getMapping("test1").setIndicesOptions(options), true); @@ -206,7 +201,6 @@ public void testSpecifiedIndexUnavailableSingleIndexThatIsClosed() throws Except verify(typesExists("test1").setIndicesOptions(options), false); verify(percolate("test1").setIndicesOptions(options), false); verify(mpercolate(options, "test1").setIndicesOptions(options), false); - verify(suggest("test1").setIndicesOptions(options), false); verify(getAliases("test1").setIndicesOptions(options), false); verify(getFieldMapping("test1").setIndicesOptions(options), false); verify(getMapping("test1").setIndicesOptions(options), false); @@ -229,7 +223,6 @@ public void testSpecifiedIndexUnavailableSingleIndexThatIsClosed() throws Except verify(typesExists("test1").setIndicesOptions(options), false); verify(percolate("test1").setIndicesOptions(options), false); verify(mpercolate(options, "test1").setIndicesOptions(options), false); - verify(suggest("test1").setIndicesOptions(options), false); verify(getAliases("test1").setIndicesOptions(options), false); verify(getFieldMapping("test1").setIndicesOptions(options), false); verify(getMapping("test1").setIndicesOptions(options), false); @@ -250,7 +243,6 @@ public void testSpecifiedIndexUnavailableSingleIndex() throws Exception { verify(aliasExists("test1").setIndicesOptions(options), true); verify(typesExists("test1").setIndicesOptions(options), true); verify(percolate("test1").setIndicesOptions(options), true); - verify(suggest("test1").setIndicesOptions(options), true); verify(getAliases("test1").setIndicesOptions(options), true); verify(getFieldMapping("test1").setIndicesOptions(options), true); verify(getMapping("test1").setIndicesOptions(options), true); @@ -269,7 +261,6 @@ public void testSpecifiedIndexUnavailableSingleIndex() throws Exception { verify(aliasExists("test1").setIndicesOptions(options), false); verify(typesExists("test1").setIndicesOptions(options), false); verify(percolate("test1").setIndicesOptions(options), false); - verify(suggest("test1").setIndicesOptions(options), false); verify(getAliases("test1").setIndicesOptions(options), false); verify(getFieldMapping("test1").setIndicesOptions(options), false); verify(getMapping("test1").setIndicesOptions(options), false); @@ -291,7 +282,6 @@ public void testSpecifiedIndexUnavailableSingleIndex() throws Exception { verify(aliasExists("test1").setIndicesOptions(options), false); verify(typesExists("test1").setIndicesOptions(options), false); verify(percolate("test1").setIndicesOptions(options), false); - verify(suggest("test1").setIndicesOptions(options), false); verify(getAliases("test1").setIndicesOptions(options), false); verify(getFieldMapping("test1").setIndicesOptions(options), false); verify(getMapping("test1").setIndicesOptions(options), false); @@ -344,7 +334,6 @@ public void testWildcardBehaviour() throws Exception { verify(typesExists(indices), false); verify(percolate(indices), false); verify(mpercolate(null, indices), false); - verify(suggest(indices), false); verify(getAliases(indices), false); verify(getFieldMapping(indices), false); verify(getMapping(indices), false); @@ -365,7 +354,6 @@ public void testWildcardBehaviour() throws Exception { verify(typesExists(indices).setIndicesOptions(options), false); verify(percolate(indices).setIndicesOptions(options), false); verify(mpercolate(options, indices), false); - verify(suggest(indices).setIndicesOptions(options), false); verify(getAliases(indices).setIndicesOptions(options), false); verify(getFieldMapping(indices).setIndicesOptions(options), false); verify(getMapping(indices).setIndicesOptions(options), false); @@ -389,7 +377,6 @@ public void testWildcardBehaviour() throws Exception { verify(typesExists(indices), false); verify(percolate(indices), false); verify(mpercolate(null, indices), false); - verify(suggest(indices), false); verify(getAliases(indices), false); verify(getFieldMapping(indices), false); verify(getMapping(indices), false); @@ -410,7 +397,6 @@ public void testWildcardBehaviour() throws Exception { verify(typesExists(indices), false); verify(percolate(indices), false); verify(mpercolate(null, indices), false); - verify(suggest(indices), false); verify(getAliases(indices), false); verify(getFieldMapping(indices), false); verify(getMapping(indices), false); @@ -431,7 +417,6 @@ public void testWildcardBehaviour() throws Exception { verify(typesExists(indices).setIndicesOptions(options), false); verify(percolate(indices).setIndicesOptions(options), false); verify(mpercolate(options, indices), false); - verify(suggest(indices).setIndicesOptions(options), false); verify(getAliases(indices).setIndicesOptions(options), false); verify(getFieldMapping(indices).setIndicesOptions(options), false); verify(getMapping(indices).setIndicesOptions(options), false); @@ -755,10 +740,6 @@ private static MultiPercolateRequestBuilder mpercolate(IndicesOptions options, S return builder.add(percolate(indices)); } - private static SuggestRequestBuilder suggest(String... indices) { - return client().prepareSuggest(indices).addSuggestion("name", SuggestBuilders.termSuggestion("a")); - } - private static GetAliasesRequestBuilder getAliases(String... indices) { return client().admin().indices().prepareGetAliases("dummy").addIndices(indices); } diff --git a/core/src/test/java/org/elasticsearch/indices/stats/IndexStatsIT.java b/core/src/test/java/org/elasticsearch/indices/stats/IndexStatsIT.java index b7cb64a7d2f01..12e6d2b4799e9 100644 --- a/core/src/test/java/org/elasticsearch/indices/stats/IndexStatsIT.java +++ b/core/src/test/java/org/elasticsearch/indices/stats/IndexStatsIT.java @@ -61,6 +61,7 @@ import static org.elasticsearch.cluster.metadata.IndexMetaData.PROTO; import static org.elasticsearch.cluster.metadata.IndexMetaData.SETTING_NUMBER_OF_REPLICAS; import static org.elasticsearch.common.settings.Settings.settingsBuilder; +import static org.elasticsearch.common.xcontent.XContentFactory.contentBuilder; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked; import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAllSuccessful; @@ -575,6 +576,10 @@ public void testAllFlags() throws Exception { IndicesStatsResponse stats = builder.execute().actionGet(); for (Flag flag : values) { + if (flag == Flag.Suggest) { + // suggest flag is unused + continue; + } assertThat(isSet(flag, stats.getPrimaries()), equalTo(false)); assertThat(isSet(flag, stats.getTotal()), equalTo(false)); } @@ -610,6 +615,10 @@ public void testAllFlags() throws Exception { } for (Flag flag : EnumSet.complementOf(flags)) { // check the complement + if (flag == Flag.Suggest) { + // suggest flag is unused + continue; + } assertThat(isSet(flag, stats.getPrimaries()), equalTo(false)); assertThat(isSet(flag, stats.getTotal()), equalTo(false)); } @@ -914,8 +923,7 @@ private static void set(Flag flag, IndicesStatsRequestBuilder builder, boolean s case Translog: builder.setTranslog(set); break; - case Suggest: - builder.setSuggest(set); + case Suggest: // unused break; case RequestCache: builder.setRequestCache(set); @@ -961,8 +969,8 @@ private static boolean isSet(Flag flag, CommonStats response) { return response.getSegments() != null; case Translog: return response.getTranslog() != null; - case Suggest: - return response.getSuggest() != null; + case Suggest: // unused + return true; case RequestCache: return response.getRequestCache() != null; case Recovery: diff --git a/core/src/test/java/org/elasticsearch/search/stats/SearchStatsUnitTests.java b/core/src/test/java/org/elasticsearch/search/stats/SearchStatsUnitTests.java index c423513ac7209..15fa7e64e3f67 100644 --- a/core/src/test/java/org/elasticsearch/search/stats/SearchStatsUnitTests.java +++ b/core/src/test/java/org/elasticsearch/search/stats/SearchStatsUnitTests.java @@ -32,9 +32,9 @@ public void testShardLevelSearchGroupStats() throws Exception { // let's create two dummy search stats with groups Map groupStats1 = new HashMap<>(); Map groupStats2 = new HashMap<>(); - groupStats2.put("group1", new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1)); - SearchStats searchStats1 = new SearchStats(new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1), 0, groupStats1); - SearchStats searchStats2 = new SearchStats(new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1), 0, groupStats2); + groupStats2.put("group1", new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)); + SearchStats searchStats1 = new SearchStats(new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 0, groupStats1); + SearchStats searchStats2 = new SearchStats(new Stats(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1), 0, groupStats2); // adding these two search stats and checking group stats are correct searchStats1.add(searchStats2); @@ -62,5 +62,8 @@ private void assertStats(Stats stats, long equalTo) { assertEquals(equalTo, stats.getScrollCount()); assertEquals(equalTo, stats.getScrollTimeInMillis()); assertEquals(equalTo, stats.getScrollCurrent()); + assertEquals(equalTo, stats.getSuggestCount()); + assertEquals(equalTo, stats.getSuggestTimeInMillis()); + assertEquals(equalTo, stats.getSuggestCurrent()); } } diff --git a/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java b/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java index ef0416df90dfe..152c450f6ecf4 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/CompletionSuggestSearchIT.java @@ -32,7 +32,8 @@ import org.elasticsearch.action.index.IndexRequestBuilder; import org.elasticsearch.action.percolate.PercolateResponse; import org.elasticsearch.action.search.SearchPhaseExecutionException; -import org.elasticsearch.action.suggest.SuggestResponse; +import org.elasticsearch.action.search.SearchRequest; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Requests; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; @@ -41,6 +42,7 @@ import org.elasticsearch.index.percolator.PercolatorFieldMapper; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregator.SubAggCollectionMode; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.sort.FieldSortBuilder; import org.elasticsearch.search.suggest.completion.CompletionStats; import org.elasticsearch.search.suggest.completion.CompletionSuggestion; @@ -198,9 +200,10 @@ public void testSuggestWithNumericPayload() throws Exception { CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg"). size(numDocs).payload(Collections.singletonList("count")); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", prefix).execute().actionGet(); - assertNoFailures(suggestResponse); - CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("foo"); + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", prefix)) + .execute().actionGet(); + assertNoFailures(searchResponse); + CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo"); CompletionSuggestion.Entry options = completionSuggestion.getEntries().get(0); assertThat(options.getOptions().size(), equalTo(numDocs)); for (CompletionSuggestion.Entry.Option option : options) { @@ -219,9 +222,10 @@ public void testMissingPayloadField() throws Exception { indexRandom(true, indexRequestBuilders); CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg") .payload(Collections.singletonList("test_field")); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", prefix).execute().actionGet(); - assertNoFailures(suggestResponse); - CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("foo"); + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", prefix)) + .execute().actionGet(); + assertNoFailures(searchResponse); + CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo"); CompletionSuggestion.Entry options = completionSuggestion.getEntries().get(0); assertThat(options.getOptions().size(), equalTo(2)); for (CompletionSuggestion.Entry.Option option : options.getOptions()) { @@ -257,9 +261,10 @@ public void testPayload() throws Exception { CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg") .payload(Arrays.asList("title", "count")); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", prefix).execute().actionGet(); - assertNoFailures(suggestResponse); - CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("foo"); + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", prefix)) + .execute().actionGet(); + assertNoFailures(searchResponse); + CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo"); List options = completionSuggestion.getEntries().get(0).getOptions(); assertThat(options.size(), equalTo(2)); assertThat(options.get(0).getText().toString(), equalTo("suggestion")); @@ -308,9 +313,10 @@ public void testSuggestWithPayload() throws Exception { CompletionSuggestionBuilder prefix = SuggestBuilders.completionSuggestion(FIELD).prefix("sugg") .size(suggestionSize).payload(payloadFields); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", prefix).execute().actionGet(); - assertNoFailures(suggestResponse); - CompletionSuggestion completionSuggestion = suggestResponse.getSuggest().getSuggestion("foo"); + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", prefix)) + .execute().actionGet(); + assertNoFailures(searchResponse); + CompletionSuggestion completionSuggestion = searchResponse.getSuggest().getSuggestion("foo"); CompletionSuggestion.Entry options = completionSuggestion.getEntries().get(0); assertThat(options.getOptions().size(), equalTo(suggestionSize)); int id = numDocs; @@ -406,12 +412,12 @@ public void testThatWeightCanBeAString() throws Exception { refresh(); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("testSuggestions", - new CompletionSuggestionBuilder(FIELD).text("test").size(10) + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("testSuggestions", new CompletionSuggestionBuilder(FIELD).text("test").size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, "testSuggestions", "testing"); - Suggest.Suggestion.Entry.Option option = suggestResponse.getSuggest().getSuggestion("testSuggestions").getEntries().get(0).getOptions().get(0); + assertSuggestions(searchResponse, "testSuggestions", "testing"); + Suggest.Suggestion.Entry.Option option = searchResponse.getSuggest().getSuggestion("testSuggestions").getEntries().get(0).getOptions().get(0); assertThat(option, is(instanceOf(CompletionSuggestion.Entry.Option.class))); CompletionSuggestion.Entry.Option prefixOption = (CompletionSuggestion.Entry.Option) option; @@ -607,16 +613,16 @@ public void testThatUpgradeToMultiFieldsWorks() throws Exception { .get(); assertThat(putMappingResponse.isAcknowledged(), is(true)); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("suggs", - SuggestBuilders.completionSuggestion(FIELD + ".suggest").text("f").size(10) + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("suggs", SuggestBuilders.completionSuggestion(FIELD + ".suggest").text("f").size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, "suggs"); + assertSuggestions(searchResponse, "suggs"); client().prepareIndex(INDEX, TYPE, "1").setRefresh(true).setSource(jsonBuilder().startObject().field(FIELD, "Foo Fighters").endObject()).get(); ensureGreen(INDEX); - SuggestResponse afterReindexingResponse = client().prepareSuggest(INDEX).addSuggestion("suggs", - SuggestBuilders.completionSuggestion(FIELD + ".suggest").text("f").size(10) + SearchResponse afterReindexingResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("suggs", SuggestBuilders.completionSuggestion(FIELD + ".suggest").text("f").size(10)) ).execute().actionGet(); assertSuggestions(afterReindexingResponse, "suggs", "Foo Fighters"); } @@ -632,15 +638,15 @@ public void testThatFuzzySuggesterWorks() throws Exception { refresh(); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Nirv").size(10) + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("Nirv").size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo", "Nirvana"); + assertSuggestions(searchResponse, false, "foo", "Nirvana"); - suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Nirw", Fuzziness.ONE).size(10) + searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("Nirw", Fuzziness.ONE).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo", "Nirvana"); + assertSuggestions(searchResponse, false, "foo", "Nirvana"); } public void testThatFuzzySuggesterSupportsEditDistances() throws Exception { @@ -655,16 +661,16 @@ public void testThatFuzzySuggesterSupportsEditDistances() throws Exception { refresh(); // edit distance 1 - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Norw", Fuzziness.ONE).size(10) + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("Norw", Fuzziness.ONE).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo"); + assertSuggestions(searchResponse, false, "foo"); // edit distance 2 - suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Norw", Fuzziness.TWO).size(10) + searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("Norw", Fuzziness.TWO).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo", "Nirvana"); + assertSuggestions(searchResponse, false, "foo", "Nirvana"); } public void testThatFuzzySuggesterSupportsTranspositions() throws Exception { @@ -678,15 +684,16 @@ public void testThatFuzzySuggesterSupportsTranspositions() throws Exception { refresh(); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Nriv", FuzzyOptions.builder().setTranspositions(false).build()).size(10) + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", + SuggestBuilders.completionSuggestion(FIELD).prefix("Nriv", FuzzyOptions.builder().setTranspositions(false).build()).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo"); + assertSuggestions(searchResponse, false, "foo"); - suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Nriv", Fuzziness.ONE).size(10) + searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("Nriv", Fuzziness.ONE).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo", "Nirvana"); + assertSuggestions(searchResponse, false, "foo", "Nirvana"); } public void testThatFuzzySuggesterSupportsMinPrefixLength() throws Exception { @@ -700,15 +707,17 @@ public void testThatFuzzySuggesterSupportsMinPrefixLength() throws Exception { refresh(); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Nriva", FuzzyOptions.builder().setFuzzyMinLength(6).build()).size(10) + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", + SuggestBuilders.completionSuggestion(FIELD).prefix("Nriva", FuzzyOptions.builder().setFuzzyMinLength(6).build()).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo"); + assertSuggestions(searchResponse, false, "foo"); - suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Nrivan", FuzzyOptions.builder().setFuzzyMinLength(6).build()).size(10) + searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", + SuggestBuilders.completionSuggestion(FIELD).prefix("Nrivan", FuzzyOptions.builder().setFuzzyMinLength(6).build()).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo", "Nirvana"); + assertSuggestions(searchResponse, false, "foo", "Nirvana"); } public void testThatFuzzySuggesterSupportsNonPrefixLength() throws Exception { @@ -722,15 +731,17 @@ public void testThatFuzzySuggesterSupportsNonPrefixLength() throws Exception { refresh(); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Nirw", FuzzyOptions.builder().setFuzzyPrefixLength(4).build()).size(10) + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", + SuggestBuilders.completionSuggestion(FIELD).prefix("Nirw", FuzzyOptions.builder().setFuzzyPrefixLength(4).build()).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo"); + assertSuggestions(searchResponse, false, "foo"); - suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", - SuggestBuilders.completionSuggestion(FIELD).prefix("Nirvo", FuzzyOptions.builder().setFuzzyPrefixLength(4).build()).size(10) + searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion("foo", + SuggestBuilders.completionSuggestion(FIELD).prefix("Nirvo", FuzzyOptions.builder().setFuzzyPrefixLength(4).build()).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo", "Nirvana"); + assertSuggestions(searchResponse, false, "foo", "Nirvana"); } public void testThatFuzzySuggesterIsUnicodeAware() throws Exception { @@ -748,18 +759,18 @@ public void testThatFuzzySuggesterIsUnicodeAware() throws Exception { org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder completionSuggestionBuilder = SuggestBuilders.completionSuggestion(FIELD).prefix("öööи", FuzzyOptions.builder().setUnicodeAware(true).build()).size(10); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", completionSuggestionBuilder).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo", "ööööö"); + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", completionSuggestionBuilder)).execute().actionGet(); + assertSuggestions(searchResponse, false, "foo", "ööööö"); // removing unicode awareness leads to no result completionSuggestionBuilder = SuggestBuilders.completionSuggestion(FIELD).prefix("öööи", FuzzyOptions.builder().setUnicodeAware(false).build()).size(10); - suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", completionSuggestionBuilder).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo"); + searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", completionSuggestionBuilder)).execute().actionGet(); + assertSuggestions(searchResponse, false, "foo"); // increasing edit distance instead of unicode awareness works again, as this is only a single character completionSuggestionBuilder = SuggestBuilders.completionSuggestion(FIELD).prefix("öööи", FuzzyOptions.builder().setUnicodeAware(false).setFuzziness(Fuzziness.TWO).build()).size(10); - suggestResponse = client().prepareSuggest(INDEX).addSuggestion("foo", completionSuggestionBuilder).execute().actionGet(); - assertSuggestions(suggestResponse, false, "foo", "ööööö"); + searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", completionSuggestionBuilder)).execute().actionGet(); + assertSuggestions(searchResponse, false, "foo", "ööööö"); } public void testThatStatsAreWorking() throws Exception { @@ -787,8 +798,8 @@ public void testThatStatsAreWorking() throws Exception { refresh(); ensureGreen(); // load the fst index into ram - client().prepareSuggest(INDEX).addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("f")).get(); - client().prepareSuggest(INDEX).addSuggestion("foo", SuggestBuilders.completionSuggestion(otherField).prefix("f")).get(); + client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(FIELD).prefix("f"))).get(); + client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion(otherField).prefix("f"))).get(); // Get all stats IndicesStatsResponse indicesStatsResponse = client().admin().indices().prepareStats(INDEX).setIndices(INDEX).setCompletion(true).get(); @@ -884,13 +895,11 @@ public void testThatIndexingInvalidFieldsInCompletionFieldResultsInException() t } } - public void assertSuggestions(String suggestionName, SuggestionBuilder suggestBuilder, String... suggestions) { - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion(suggestionName, suggestBuilder - ).execute().actionGet(); - assertSuggestions(suggestResponse, suggestionName, suggestions); - + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion(suggestionName, suggestBuilder)).execute().actionGet(); + assertSuggestions(searchResponse, suggestionName, suggestions); } + public void assertSuggestions(String suggestion, String... suggestions) { String suggestionName = RandomStrings.randomAsciiOfLength(random(), 10); CompletionSuggestionBuilder suggestionBuilder = SuggestBuilders.completionSuggestion(FIELD).text(suggestion).size(10); @@ -899,28 +908,29 @@ public void assertSuggestions(String suggestion, String... suggestions) { public void assertSuggestionsNotInOrder(String suggestString, String... suggestions) { String suggestionName = RandomStrings.randomAsciiOfLength(random(), 10); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion(suggestionName, - SuggestBuilders.completionSuggestion(FIELD).text(suggestString).size(10) + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion(suggestionName, + SuggestBuilders.completionSuggestion(FIELD).text(suggestString).size(10)) ).execute().actionGet(); - assertSuggestions(suggestResponse, false, suggestionName, suggestions); + assertSuggestions(searchResponse, false, suggestionName, suggestions); } - static void assertSuggestions(SuggestResponse suggestResponse, String name, String... suggestions) { - assertSuggestions(suggestResponse, true, name, suggestions); + static void assertSuggestions(SearchResponse searchResponse, String name, String... suggestions) { + assertSuggestions(searchResponse, true, name, suggestions); } - private static void assertSuggestions(SuggestResponse suggestResponse, boolean suggestionOrderStrict, String name, String... suggestions) { - assertAllSuccessful(suggestResponse); + private static void assertSuggestions(SearchResponse searchResponse, boolean suggestionOrderStrict, String name, String... suggestions) { + assertAllSuccessful(searchResponse); List suggestionNames = new ArrayList<>(); - for (Suggest.Suggestion> suggestion : iterableAsArrayList(suggestResponse.getSuggest())) { + for (Suggest.Suggestion> suggestion : iterableAsArrayList(searchResponse.getSuggest())) { suggestionNames.add(suggestion.getName()); } String expectFieldInResponseMsg = String.format(Locale.ROOT, "Expected suggestion named %s in response, got %s", name, suggestionNames); - assertThat(expectFieldInResponseMsg, suggestResponse.getSuggest().getSuggestion(name), is(notNullValue())); + assertThat(expectFieldInResponseMsg, searchResponse.getSuggest().getSuggestion(name), is(notNullValue())); - Suggest.Suggestion> suggestion = suggestResponse.getSuggest().getSuggestion(name); + Suggest.Suggestion> suggestion = searchResponse.getSuggest().getSuggestion(name); List suggestionList = getNames(suggestion.getEntries().get(0)); List options = suggestion.getEntries().get(0).getOptions(); diff --git a/core/src/test/java/org/elasticsearch/search/suggest/ContextCompletionSuggestSearchIT.java b/core/src/test/java/org/elasticsearch/search/suggest/ContextCompletionSuggestSearchIT.java index 58458c9d24495..ee45a9d03710f 100644 --- a/core/src/test/java/org/elasticsearch/search/suggest/ContextCompletionSuggestSearchIT.java +++ b/core/src/test/java/org/elasticsearch/search/suggest/ContextCompletionSuggestSearchIT.java @@ -23,7 +23,7 @@ import org.apache.lucene.spatial.util.GeoHashUtils; import org.apache.lucene.util.LuceneTestCase.SuppressCodecs; import org.elasticsearch.action.index.IndexRequestBuilder; -import org.elasticsearch.action.suggest.SuggestResponse; +import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.common.geo.GeoPoint; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.Fuzziness; @@ -624,16 +624,17 @@ public void testGeoField() throws Exception { String suggestionName = randomAsciiOfLength(10); CompletionSuggestionBuilder context = SuggestBuilders.completionSuggestion(FIELD).text("h").size(10) .contexts(Collections.singletonMap("st", Collections.singletonList(GeoQueryContext.builder().setGeoPoint(new GeoPoint(52.52, 13.4)).build()))); - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion(suggestionName, context).get(); + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest(new SuggestBuilder().addSuggestion(suggestionName, context)).get(); - assertEquals(suggestResponse.getSuggest().size(), 1); - assertEquals("Hotel Amsterdam in Berlin", suggestResponse.getSuggest().getSuggestion(suggestionName).iterator().next().getOptions().iterator().next().getText().string()); + assertEquals(searchResponse.getSuggest().size(), 1); + assertEquals("Hotel Amsterdam in Berlin", searchResponse.getSuggest().getSuggestion(suggestionName).iterator().next().getOptions().iterator().next().getText().string()); } public void assertSuggestions(String suggestionName, SuggestionBuilder suggestBuilder, String... suggestions) { - SuggestResponse suggestResponse = client().prepareSuggest(INDEX).addSuggestion(suggestionName, suggestBuilder + SearchResponse searchResponse = client().prepareSearch(INDEX).suggest( + new SuggestBuilder().addSuggestion(suggestionName, suggestBuilder) ).execute().actionGet(); - CompletionSuggestSearchIT.assertSuggestions(suggestResponse, suggestionName, suggestions); + CompletionSuggestSearchIT.assertSuggestions(searchResponse, suggestionName, suggestions); } private void createIndexAndMapping(CompletionMappingBuilder completionMappingBuilder) throws IOException { diff --git a/docs/reference/indices/stats.asciidoc b/docs/reference/indices/stats.asciidoc index 87eda44e10408..e990a7ff6bd63 100644 --- a/docs/reference/indices/stats.asciidoc +++ b/docs/reference/indices/stats.asciidoc @@ -34,7 +34,8 @@ specified as well in the URI. Those stats can be any of: `get`:: Get statistics, including missing stats. -`search`:: Search statistics. You can include statistics for custom groups by adding +`search`:: Search statistics including suggest statistics. + You can include statistics for custom groups by adding an extra `groups` parameter (search operations can be associated with one or more groups). The `groups` parameter accepts a comma separated list of group names. Use `_all` to return statistics for all groups. @@ -47,7 +48,6 @@ specified as well in the URI. Those stats can be any of: `merge`:: Merge statistics. `request_cache`:: <> statistics. `refresh`:: Refresh statistics. -`suggest`:: Suggest statistics. `warmer`:: Warmer statistics. `translog`:: Translog statistics. diff --git a/docs/reference/migration/migrate_5_0/index-apis.asciidoc b/docs/reference/migration/migrate_5_0/index-apis.asciidoc index 72651295bbcdc..b820194c2d06e 100644 --- a/docs/reference/migration/migrate_5_0/index-apis.asciidoc +++ b/docs/reference/migration/migrate_5_0/index-apis.asciidoc @@ -45,4 +45,7 @@ longer a double and is instead an object encapsulating the one-minute, five-minute and fifteen-minute load averages. Additionally, the recent CPU usage can be obtained from `OsStats.Cpu#getPercent`. +==== Suggest stats +Suggest stats exposed through `suggest` in indices stats has been merged +with `search` stats. `suggest` stats is exposed as part of `search` stats. diff --git a/docs/reference/migration/migrate_5_0/java.asciidoc b/docs/reference/migration/migrate_5_0/java.asciidoc index 59f0b1c15e25f..df526740f3378 100644 --- a/docs/reference/migration/migrate_5_0/java.asciidoc +++ b/docs/reference/migration/migrate_5_0/java.asciidoc @@ -22,6 +22,25 @@ can be replaced with client.prepareSearch(indices).setSource(new SearchSourceBuilder().size(0).query(query)).get(); ----- +==== Suggest api has been removed + +The suggest api has been removed from the Java api, use the suggest option in search api, it has been optimized +for suggest-only request. + +The following call + +[source,java] +----- +client.prepareSuggest(indices).addSuggestion("foo", SuggestBuilders.completionSuggestion("field").text("s")).get(); +----- + +can be replaced with + +[source,java] +----- +client.prepareSearch(indices).suggest(new SuggestBuilder().addSuggestion("foo", SuggestBuilders.completionSuggestion("field").text("s"))).get(); +----- + ==== Elasticsearch will no longer detect logging implementations Elasticsearch now logs only to log4j 1.2. Previously if log4j wasn't on the diff --git a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/IndicesRequestTests.java b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/IndicesRequestTests.java index 29bd7f47498da..c8b30a5e2de30 100644 --- a/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/IndicesRequestTests.java +++ b/modules/lang-groovy/src/test/java/org/elasticsearch/messy/tests/IndicesRequestTests.java @@ -70,8 +70,6 @@ import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; -import org.elasticsearch.action.suggest.SuggestAction; -import org.elasticsearch.action.suggest.SuggestRequest; import org.elasticsearch.action.termvectors.MultiTermVectorsAction; import org.elasticsearch.action.termvectors.MultiTermVectorsRequest; import org.elasticsearch.action.termvectors.TermVectorsAction; @@ -88,7 +86,6 @@ import org.elasticsearch.script.Script; import org.elasticsearch.script.groovy.GroovyPlugin; import org.elasticsearch.search.action.SearchTransportService; -import org.elasticsearch.search.suggest.SuggestBuilder; import org.elasticsearch.tasks.Task; import org.elasticsearch.test.ESIntegTestCase; import org.elasticsearch.test.ESIntegTestCase.ClusterScope; @@ -440,17 +437,6 @@ public void testIndicesStats() { assertSameIndices(indicesStatsRequest, indicesStats); } - public void testSuggest() { - String suggestAction = SuggestAction.NAME + "[s]"; - interceptTransportActions(suggestAction); - - SuggestRequest suggestRequest = new SuggestRequest(randomIndicesOrAliases()).suggest(new SuggestBuilder()); - internalCluster().clientNodeClient().suggest(suggestRequest).actionGet(); - - clearInterceptedActions(); - assertSameIndices(suggestRequest, suggestAction); - } - public void testValidateQuery() { String validateQueryShardAction = ValidateQueryAction.NAME + "[s]"; interceptTransportActions(validateQueryShardAction); diff --git a/modules/lang-mustache/src/test/java/org/elasticsearch/messy/tests/SuggestSearchTests.java b/modules/lang-mustache/src/test/java/org/elasticsearch/messy/tests/SuggestSearchTests.java index 94f60f8802c97..79cac3181ea02 100644 --- a/modules/lang-mustache/src/test/java/org/elasticsearch/messy/tests/SuggestSearchTests.java +++ b/modules/lang-mustache/src/test/java/org/elasticsearch/messy/tests/SuggestSearchTests.java @@ -60,8 +60,6 @@ import org.elasticsearch.action.search.SearchRequestBuilder; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; -import org.elasticsearch.action.suggest.SuggestRequestBuilder; -import org.elasticsearch.action.suggest.SuggestResponse; import org.elasticsearch.common.io.PathUtils; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; @@ -1269,34 +1267,17 @@ protected Suggest searchSuggest(String suggestText, String name, SuggestionBuild } protected Suggest searchSuggest(String suggestText, int expectShardsFailed, Map> suggestions) { - if (randomBoolean()) { - SearchRequestBuilder builder = client().prepareSearch().setSize(0); - SuggestBuilder suggestBuilder = new SuggestBuilder(); - if (suggestText != null) { - suggestBuilder.setGlobalText(suggestText); - } - for (Entry> suggestion : suggestions.entrySet()) { - suggestBuilder.addSuggestion(suggestion.getKey(), suggestion.getValue()); - } - builder.suggest(suggestBuilder); - SearchResponse actionGet = builder.execute().actionGet(); - assertThat(Arrays.toString(actionGet.getShardFailures()), actionGet.getFailedShards(), equalTo(expectShardsFailed)); - return actionGet.getSuggest(); - } else { - SuggestRequestBuilder builder = client().prepareSuggest(); - if (suggestText != null) { - builder.setSuggestText(suggestText); - } - for (Entry> suggestion : suggestions.entrySet()) { - builder.addSuggestion(suggestion.getKey(), suggestion.getValue()); - } - - SuggestResponse actionGet = builder.execute().actionGet(); - assertThat(Arrays.toString(actionGet.getShardFailures()), actionGet.getFailedShards(), equalTo(expectShardsFailed)); - if (expectShardsFailed > 0) { - throw new SearchPhaseExecutionException("suggest", "Suggest execution failed", new ShardSearchFailure[0]); - } - return actionGet.getSuggest(); + SearchRequestBuilder builder = client().prepareSearch().setSize(0); + SuggestBuilder suggestBuilder = new SuggestBuilder(); + if (suggestText != null) { + suggestBuilder.setGlobalText(suggestText); + } + for (Entry> suggestion : suggestions.entrySet()) { + suggestBuilder.addSuggestion(suggestion.getKey(), suggestion.getValue()); } + builder.suggest(suggestBuilder); + SearchResponse actionGet = builder.execute().actionGet(); + assertThat(Arrays.toString(actionGet.getShardFailures()), actionGet.getFailedShards(), equalTo(expectShardsFailed)); + return actionGet.getSuggest(); } } diff --git a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/11_metric.yaml b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/11_metric.yaml index 19598c7363efc..7b88ac570806f 100644 --- a/rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/11_metric.yaml +++ b/rest-api-spec/src/main/resources/rest-api-spec/test/indices.stats/11_metric.yaml @@ -34,7 +34,6 @@ setup: - is_true: _all.total.completion - is_true: _all.total.segments - is_true: _all.total.translog - - is_true: _all.total.suggest - is_true: _all.total.recovery --- @@ -56,7 +55,6 @@ setup: - is_true: _all.total.completion - is_true: _all.total.segments - is_true: _all.total.translog - - is_true: _all.total.suggest - is_true: _all.total.recovery --- @@ -78,7 +76,6 @@ setup: - is_false: _all.total.completion - is_false: _all.total.segments - is_false: _all.total.translog - - is_false: _all.total.suggest - is_false: _all.total.recovery --- @@ -100,7 +97,6 @@ setup: - is_false: _all.total.completion - is_false: _all.total.segments - is_false: _all.total.translog - - is_false: _all.total.suggest - is_false: _all.total.recovery @@ -123,6 +119,5 @@ setup: - is_false: _all.total.completion - is_false: _all.total.segments - is_false: _all.total.translog - - is_false: _all.total.suggest - is_true: _all.total.recovery