From 230c6684a9b27b83856aeaea708645225c44609c Mon Sep 17 00:00:00 2001 From: Adrien Grand Date: Fri, 17 Oct 2014 13:24:04 +0200 Subject: [PATCH] Search: Remove partial fields. Partial fields have been deprecated since 1.0.0Beta1 in favor of _source filtering. They will be removed in 2.0. --- docs/reference/migration/migrate_2_0.asciidoc | 6 +- .../action/search/SearchRequestBuilder.java | 34 ------- .../percolator/PercolateContext.java | 11 --- .../elasticsearch/search/SearchModule.java | 2 - .../metrics/tophits/TopHitsContext.java | 15 --- .../search/builder/SearchSourceBuilder.java | 64 ------------- .../search/fetch/FetchPhase.java | 18 ++-- .../fetch/partial/PartialFieldsContext.java | 67 ------------- .../partial/PartialFieldsFetchSubPhase.java | 84 ---------------- .../partial/PartialFieldsParseElement.java | 95 ------------------- .../search/internal/DefaultSearchContext.java | 13 --- .../search/internal/SearchContext.java | 5 - .../search/fields/SearchFieldsTests.java | 17 ---- .../search/source/SourceFetchingTests.java | 3 - .../elasticsearch/test/TestSearchContext.java | 11 --- 15 files changed, 11 insertions(+), 434 deletions(-) delete mode 100644 src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsContext.java delete mode 100644 src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsFetchSubPhase.java delete mode 100644 src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsParseElement.java diff --git a/docs/reference/migration/migrate_2_0.asciidoc b/docs/reference/migration/migrate_2_0.asciidoc index e0ff8e9f671..450122c65cd 100644 --- a/docs/reference/migration/migrate_2_0.asciidoc +++ b/docs/reference/migration/migrate_2_0.asciidoc @@ -9,4 +9,8 @@ your application to Elasticsearch 2.0. The <> will, by default produce an error response if a requested index does not exist. This change brings the defaults for this API in line with the other Indices APIs. The <> options can be used on a request -to change this behavior \ No newline at end of file +to change this behavior + +=== Partial fields + +Partial fields were deprecated since 1.0.0beta1 in favor of <>. diff --git a/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java b/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java index 1afcd6b9425..16b45bf34d6 100644 --- a/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java +++ b/src/main/java/org/elasticsearch/action/search/SearchRequestBuilder.java @@ -444,40 +444,6 @@ public class SearchRequestBuilder extends ActionRequestBuilder fieldNames; private FieldDataFieldsContext fieldDataFields; private ScriptFieldsContext scriptFields; - private PartialFieldsContext partialFields; private FetchSourceContext fetchSourceContext; private SearchContextHighlight highlight; @@ -247,19 +245,6 @@ public class TopHitsContext extends SearchContext { return this.scriptFields; } - @Override - public boolean hasPartialFields() { - return partialFields != null; - } - - @Override - public PartialFieldsContext partialFields() { - if (partialFields == null) { - partialFields = new PartialFieldsContext(); - } - return this.partialFields; - } - @Override public boolean sourceRequested() { return fetchSourceContext != null && fetchSourceContext.fetchSource(); diff --git a/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java b/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java index 0144668cb88..ba188df5e48 100644 --- a/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java +++ b/src/main/java/org/elasticsearch/search/builder/SearchSourceBuilder.java @@ -103,7 +103,6 @@ public class SearchSourceBuilder implements ToXContent { private List fieldNames; private List fieldDataFields; private List scriptFields; - private List partialFields; private FetchSourceContext fetchSourceContext; private List aggregations; @@ -592,46 +591,6 @@ public class SearchSourceBuilder implements ToXContent { return this; } - /** - * Adds a partial field based on _source, with an "include" and/or "exclude" set which can include simple wildcard - * elements. - * - * @deprecated since 1.0.0 - * use {@link SearchSourceBuilder#fetchSource(String, String)} instead - * - * @param name The name of the field - * @param include An optional include (optionally wildcarded) pattern from _source - * @param exclude An optional exclude (optionally wildcarded) pattern from _source - */ - @Deprecated - public SearchSourceBuilder partialField(String name, @Nullable String include, @Nullable String exclude) { - if (partialFields == null) { - partialFields = Lists.newArrayList(); - } - partialFields.add(new PartialField(name, include, exclude)); - return this; - } - - /** - * Adds a partial field based on _source, with an "includes" and/or "excludes set which can include simple wildcard - * elements. - * - * @deprecated since 1.0.0 - * use {@link SearchSourceBuilder#fetchSource(String[], String[])} instead - * - * @param name The name of the field - * @param includes An optional list of includes (optionally wildcarded) patterns from _source - * @param excludes An optional list of excludes (optionally wildcarded) patterns from _source - */ - @Deprecated - public SearchSourceBuilder partialField(String name, @Nullable String[] includes, @Nullable String[] excludes) { - if (partialFields == null) { - partialFields = Lists.newArrayList(); - } - partialFields.add(new PartialField(name, includes, excludes)); - return this; - } - /** * Sets the boost a specific index will receive when the query is executeed against it. * @@ -768,29 +727,6 @@ public class SearchSourceBuilder implements ToXContent { builder.endArray(); } - if (partialFields != null) { - builder.startObject("partial_fields"); - for (PartialField partialField : partialFields) { - builder.startObject(partialField.name()); - if (partialField.includes() != null) { - if (partialField.includes().length == 1) { - builder.field("include", partialField.includes()[0]); - } else { - builder.field("include", partialField.includes()); - } - } - if (partialField.excludes() != null) { - if (partialField.excludes().length == 1) { - builder.field("exclude", partialField.excludes()[0]); - } else { - builder.field("exclude", partialField.excludes()); - } - } - builder.endObject(); - } - builder.endObject(); - } - if (scriptFields != null) { builder.startObject("script_fields"); for (ScriptField scriptField : scriptFields) { diff --git a/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java b/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java index fbbc024b0c5..0beedf208f7 100644 --- a/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java +++ b/src/main/java/org/elasticsearch/search/fetch/FetchPhase.java @@ -47,7 +47,6 @@ import org.elasticsearch.search.SearchPhase; import org.elasticsearch.search.fetch.explain.ExplainFetchSubPhase; import org.elasticsearch.search.fetch.fielddata.FieldDataFieldsFetchSubPhase; import org.elasticsearch.search.fetch.matchedqueries.MatchedQueriesFetchSubPhase; -import org.elasticsearch.search.fetch.partial.PartialFieldsFetchSubPhase; import org.elasticsearch.search.fetch.script.ScriptFieldsFetchSubPhase; import org.elasticsearch.search.fetch.source.FetchSourceContext; import org.elasticsearch.search.fetch.source.FetchSourceSubPhase; @@ -72,10 +71,10 @@ public class FetchPhase implements SearchPhase { private final FetchSubPhase[] fetchSubPhases; @Inject - public FetchPhase(HighlightPhase highlightPhase, ScriptFieldsFetchSubPhase scriptFieldsPhase, PartialFieldsFetchSubPhase partialFieldsPhase, + public FetchPhase(HighlightPhase highlightPhase, ScriptFieldsFetchSubPhase scriptFieldsPhase, MatchedQueriesFetchSubPhase matchedQueriesPhase, ExplainFetchSubPhase explainPhase, VersionFetchSubPhase versionPhase, FetchSourceSubPhase fetchSourceSubPhase, FieldDataFieldsFetchSubPhase fieldDataFieldsFetchSubPhase) { - this.fetchSubPhases = new FetchSubPhase[]{scriptFieldsPhase, partialFieldsPhase, matchedQueriesPhase, explainPhase, highlightPhase, + this.fetchSubPhases = new FetchSubPhase[]{scriptFieldsPhase, matchedQueriesPhase, explainPhase, highlightPhase, fetchSourceSubPhase, versionPhase, fieldDataFieldsFetchSubPhase}; } @@ -100,16 +99,11 @@ public class FetchPhase implements SearchPhase { boolean loadAllStored = false; if (!context.hasFieldNames()) { - if (context.hasPartialFields()) { - // partial fields need the source, so fetch it - fieldsVisitor = new UidAndSourceFieldsVisitor(); - } else { - // no fields specified, default to return source if no explicit indication - if (!context.hasScriptFields() && !context.hasFetchSourceContext()) { - context.fetchSourceContext(new FetchSourceContext(true)); - } - fieldsVisitor = context.sourceRequested() ? new UidAndSourceFieldsVisitor() : new JustUidFieldsVisitor(); + // no fields specified, default to return source if no explicit indication + if (!context.hasScriptFields() && !context.hasFetchSourceContext()) { + context.fetchSourceContext(new FetchSourceContext(true)); } + fieldsVisitor = context.sourceRequested() ? new UidAndSourceFieldsVisitor() : new JustUidFieldsVisitor(); } else if (context.fieldNames().isEmpty()) { if (context.sourceRequested()) { fieldsVisitor = new UidAndSourceFieldsVisitor(); diff --git a/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsContext.java b/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsContext.java deleted file mode 100644 index 8f1d5b3fd59..00000000000 --- a/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsContext.java +++ /dev/null @@ -1,67 +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.search.fetch.partial; - -import com.google.common.collect.Lists; - -import java.util.List; - -/** - */ -public class PartialFieldsContext { - - public static class PartialField { - private final String name; - private final String[] includes; - private final String[] excludes; - - public PartialField(String name, String[] includes, String[] excludes) { - this.name = name; - this.includes = includes; - this.excludes = excludes; - } - - public String name() { - return this.name; - } - - public String[] includes() { - return this.includes; - } - - public String[] excludes() { - return this.excludes; - } - } - - private final List fields = Lists.newArrayList(); - - public PartialFieldsContext() { - - } - - public void add(PartialField field) { - fields.add(field); - } - - public List fields() { - return this.fields; - } -} diff --git a/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsFetchSubPhase.java b/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsFetchSubPhase.java deleted file mode 100644 index 697d4e8a9d8..00000000000 --- a/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsFetchSubPhase.java +++ /dev/null @@ -1,84 +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.search.fetch.partial; - -import com.google.common.collect.ImmutableMap; -import org.elasticsearch.ElasticsearchException; -import org.elasticsearch.common.inject.Inject; -import org.elasticsearch.search.SearchHitField; -import org.elasticsearch.search.SearchParseElement; -import org.elasticsearch.search.fetch.FetchSubPhase; -import org.elasticsearch.search.internal.InternalSearchHit; -import org.elasticsearch.search.internal.InternalSearchHitField; -import org.elasticsearch.search.internal.SearchContext; - -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; - -/** - */ -public class PartialFieldsFetchSubPhase implements FetchSubPhase { - - @Inject - public PartialFieldsFetchSubPhase() { - - } - - @Override - public Map parseElements() { - ImmutableMap.Builder parseElements = ImmutableMap.builder(); - parseElements.put("partial_fields", new PartialFieldsParseElement()) - .put("partialFields", new PartialFieldsParseElement()); - return parseElements.build(); - } - - @Override - public boolean hitsExecutionNeeded(SearchContext context) { - return false; - } - - @Override - public void hitsExecute(SearchContext context, InternalSearchHit[] hits) throws ElasticsearchException { - } - - @Override - public boolean hitExecutionNeeded(SearchContext context) { - return context.hasPartialFields(); - } - - @Override - public void hitExecute(SearchContext context, HitContext hitContext) throws ElasticsearchException { - for (PartialFieldsContext.PartialField field : context.partialFields().fields()) { - Object value = context.lookup().source().filter(field.includes(), field.excludes()); - - if (hitContext.hit().fieldsOrNull() == null) { - hitContext.hit().fields(new HashMap(2)); - } - - SearchHitField hitField = hitContext.hit().fields().get(field.name()); - if (hitField == null) { - hitField = new InternalSearchHitField(field.name(), new ArrayList<>(2)); - hitContext.hit().fields().put(field.name(), hitField); - } - hitField.values().add(value); - } - } -} diff --git a/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsParseElement.java b/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsParseElement.java deleted file mode 100644 index dc52d32f011..00000000000 --- a/src/main/java/org/elasticsearch/search/fetch/partial/PartialFieldsParseElement.java +++ /dev/null @@ -1,95 +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.search.fetch.partial; - -import org.elasticsearch.common.Strings; -import org.elasticsearch.common.xcontent.XContentParser; -import org.elasticsearch.search.SearchParseElement; -import org.elasticsearch.search.internal.SearchContext; - -import java.util.ArrayList; -import java.util.List; - -/** - *
- * "partial_fields" : {
- *  "test1" : {
- *      "includes" : "doc['field_name'].value"
- *  },
- *  "test2" : {
- *      "excludes" : "..."
- *  }
- * }
- * 
- */ -public class PartialFieldsParseElement implements SearchParseElement { - - @Override - public void parse(XContentParser parser, SearchContext context) throws Exception { - XContentParser.Token token; - String currentFieldName = null; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else if (token == XContentParser.Token.START_OBJECT) { - String fieldName = currentFieldName; - List includes = null; - List excludes = null; - while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) { - if (token == XContentParser.Token.FIELD_NAME) { - currentFieldName = parser.currentName(); - } else if (token == XContentParser.Token.START_ARRAY) { - if ("includes".equals(currentFieldName) || "include".equals(currentFieldName)) { - if (includes == null) { - includes = new ArrayList<>(2); - } - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - includes.add(parser.text()); - } - } else if ("excludes".equals(currentFieldName) || "exclude".equals(currentFieldName)) { - if (excludes == null) { - excludes = new ArrayList<>(2); - } - while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) { - excludes.add(parser.text()); - } - } - } else if (token.isValue()) { - if ("include".equals(currentFieldName)) { - if (includes == null) { - includes = new ArrayList<>(2); - } - includes.add(parser.text()); - } else if ("exclude".equals(currentFieldName)) { - if (excludes == null) { - excludes = new ArrayList<>(2); - } - excludes.add(parser.text()); - } - } - } - PartialFieldsContext.PartialField field = new PartialFieldsContext.PartialField(fieldName, - includes == null ? Strings.EMPTY_ARRAY : includes.toArray(new String[includes.size()]), - excludes == null ? Strings.EMPTY_ARRAY : excludes.toArray(new String[excludes.size()])); - context.partialFields().add(field); - } - } - } -} \ No newline at end of file diff --git a/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java b/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java index 15ff00c5d5c..fc76f4d468b 100644 --- a/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -59,7 +59,6 @@ import org.elasticsearch.search.aggregations.SearchContextAggregations; import org.elasticsearch.search.dfs.DfsSearchResult; import org.elasticsearch.search.fetch.FetchSearchResult; import org.elasticsearch.search.fetch.fielddata.FieldDataFieldsContext; -import org.elasticsearch.search.fetch.partial.PartialFieldsContext; import org.elasticsearch.search.fetch.script.ScriptFieldsContext; import org.elasticsearch.search.fetch.source.FetchSourceContext; import org.elasticsearch.search.highlight.SearchContextHighlight; @@ -131,7 +130,6 @@ public class DefaultSearchContext extends SearchContext { private List fieldNames; private FieldDataFieldsContext fieldDataFields; private ScriptFieldsContext scriptFields; - private PartialFieldsContext partialFields; private FetchSourceContext fetchSourceContext; private int from = -1; @@ -379,17 +377,6 @@ public class DefaultSearchContext extends SearchContext { return this.scriptFields; } - public boolean hasPartialFields() { - return partialFields != null; - } - - public PartialFieldsContext partialFields() { - if (partialFields == null) { - partialFields = new PartialFieldsContext(); - } - return this.partialFields; - } - /** * A shortcut function to see whether there is a fetchSourceContext and it says the source is requested. * diff --git a/src/main/java/org/elasticsearch/search/internal/SearchContext.java b/src/main/java/org/elasticsearch/search/internal/SearchContext.java index ac0ab04ed05..a0350280078 100644 --- a/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -52,7 +52,6 @@ import org.elasticsearch.search.aggregations.SearchContextAggregations; import org.elasticsearch.search.dfs.DfsSearchResult; import org.elasticsearch.search.fetch.FetchSearchResult; import org.elasticsearch.search.fetch.fielddata.FieldDataFieldsContext; -import org.elasticsearch.search.fetch.partial.PartialFieldsContext; import org.elasticsearch.search.fetch.script.ScriptFieldsContext; import org.elasticsearch.search.fetch.source.FetchSourceContext; import org.elasticsearch.search.highlight.SearchContextHighlight; @@ -176,10 +175,6 @@ public abstract class SearchContext implements Releasable { public abstract ScriptFieldsContext scriptFields(); - public abstract boolean hasPartialFields(); - - public abstract PartialFieldsContext partialFields(); - /** * A shortcut function to see whether there is a fetchSourceContext and it says the source is requested. * diff --git a/src/test/java/org/elasticsearch/search/fields/SearchFieldsTests.java b/src/test/java/org/elasticsearch/search/fields/SearchFieldsTests.java index 21cfa8350ae..769d26619a2 100644 --- a/src/test/java/org/elasticsearch/search/fields/SearchFieldsTests.java +++ b/src/test/java/org/elasticsearch/search/fields/SearchFieldsTests.java @@ -317,23 +317,6 @@ public class SearchFieldsTests extends ElasticsearchIntegrationTest { .execute().actionGet(); client().admin().indices().prepareRefresh().execute().actionGet(); - - SearchResponse response = client().prepareSearch("test") - .addPartialField("partial1", "obj1.arr1.*", null) - .addPartialField("partial2", null, "obj1") - .execute().actionGet(); - assertThat("Failures " + Arrays.toString(response.getShardFailures()), response.getShardFailures().length, equalTo(0)); - - Map partial1 = response.getHits().getAt(0).field("partial1").value(); - assertThat(partial1, notNullValue()); - assertThat(partial1.containsKey("field1"), equalTo(false)); - assertThat(partial1.containsKey("obj1"), equalTo(true)); - assertThat(((Map) partial1.get("obj1")).get("arr1"), instanceOf(List.class)); - - Map partial2 = response.getHits().getAt(0).field("partial2").value(); - assertThat(partial2, notNullValue()); - assertThat(partial2.containsKey("obj1"), equalTo(false)); - assertThat(partial2.containsKey("field1"), equalTo(true)); } @Test diff --git a/src/test/java/org/elasticsearch/search/source/SourceFetchingTests.java b/src/test/java/org/elasticsearch/search/source/SourceFetchingTests.java index 88cc4d8589a..ce8b3a534b7 100644 --- a/src/test/java/org/elasticsearch/search/source/SourceFetchingTests.java +++ b/src/test/java/org/elasticsearch/search/source/SourceFetchingTests.java @@ -46,9 +46,6 @@ public class SourceFetchingTests extends ElasticsearchIntegrationTest { response = client().prepareSearch("test").addField("_source").get(); assertThat(response.getHits().getAt(0).getSourceAsString(), notNullValue()); - response = client().prepareSearch("test").addPartialField("test", "field", null).get(); - assertThat(response.getHits().getAt(0).getSourceAsString(), nullValue()); - } @Test diff --git a/src/test/java/org/elasticsearch/test/TestSearchContext.java b/src/test/java/org/elasticsearch/test/TestSearchContext.java index 1c240f7d0c6..f4a020a1e49 100644 --- a/src/test/java/org/elasticsearch/test/TestSearchContext.java +++ b/src/test/java/org/elasticsearch/test/TestSearchContext.java @@ -47,7 +47,6 @@ import org.elasticsearch.search.aggregations.SearchContextAggregations; import org.elasticsearch.search.dfs.DfsSearchResult; import org.elasticsearch.search.fetch.FetchSearchResult; import org.elasticsearch.search.fetch.fielddata.FieldDataFieldsContext; -import org.elasticsearch.search.fetch.partial.PartialFieldsContext; import org.elasticsearch.search.fetch.script.ScriptFieldsContext; import org.elasticsearch.search.fetch.source.FetchSourceContext; import org.elasticsearch.search.highlight.SearchContextHighlight; @@ -238,16 +237,6 @@ public class TestSearchContext extends SearchContext { return null; } - @Override - public boolean hasPartialFields() { - return false; - } - - @Override - public PartialFieldsContext partialFields() { - return null; - } - @Override public boolean sourceRequested() { return false;