From 5f0f66908e85f2b63f5e3491d44e436f3b53f4a2 Mon Sep 17 00:00:00 2001 From: Britta Weber Date: Wed, 22 Jul 2015 17:42:49 +0200 Subject: [PATCH] make fielddata fields a plugin --- .../percolator/PercolateContext.java | 10 ---------- .../fielddata/FieldDataFieldsContext.java | 3 ++- .../FieldDataFieldsFetchSubPhase.java | 19 +++++++++++++++++-- .../FieldDataFieldsParseElement.java | 5 +++-- .../search/internal/DefaultSearchContext.java | 13 ------------- .../internal/FilteredSearchContext.java | 10 ---------- .../search/internal/SearchContext.java | 4 ---- .../search/internal/SubSearchContext.java | 13 ------------- .../elasticsearch/test/TestSearchContext.java | 10 ---------- 9 files changed, 22 insertions(+), 65 deletions(-) diff --git a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java index 1fbcc0b9c0f..1a18f021597 100644 --- a/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java +++ b/core/src/main/java/org/elasticsearch/percolator/PercolateContext.java @@ -395,16 +395,6 @@ public class PercolateContext extends SearchContext { throw new UnsupportedOperationException(); } - @Override - public boolean hasFieldDataFields() { - throw new UnsupportedOperationException(); - } - - @Override - public FieldDataFieldsContext fieldDataFields() { - throw new UnsupportedOperationException(); - } - @Override public boolean hasScriptFields() { throw new UnsupportedOperationException(); diff --git a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsContext.java b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsContext.java index aebed3956a0..14c389785b8 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsContext.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsContext.java @@ -19,13 +19,14 @@ package org.elasticsearch.search.fetch.fielddata; import com.google.common.collect.Lists; +import org.elasticsearch.search.fetch.FetchSubPhaseContext; import java.util.List; /** * All the required context to pull a field from the field data cache. */ -public class FieldDataFieldsContext { +public class FieldDataFieldsContext implements FetchSubPhaseContext{ public static class FieldDataField { private final String name; diff --git a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsFetchSubPhase.java b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsFetchSubPhase.java index 7d8bb51e94b..0ff61e7bb3e 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsFetchSubPhase.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsFetchSubPhase.java @@ -27,6 +27,7 @@ import org.elasticsearch.index.mapper.MappedFieldType; import org.elasticsearch.search.SearchHitField; import org.elasticsearch.search.SearchParseElement; import org.elasticsearch.search.fetch.FetchSubPhase; +import org.elasticsearch.search.fetch.FetchSubPhaseContext; import org.elasticsearch.search.internal.InternalSearchHit; import org.elasticsearch.search.internal.InternalSearchHitField; import org.elasticsearch.search.internal.SearchContext; @@ -43,6 +44,20 @@ import java.util.Map; */ public class FieldDataFieldsFetchSubPhase implements FetchSubPhase { + public static final String[] NAMES = {"fielddata_fields", "fielddataFields"}; + public static final ContextFactory CONTEXT_FACTORY = new ContextFactory() { + + @Override + public String getName() { + return NAMES[0]; + } + + @Override + public FetchSubPhaseContext newContextInstance() { + return new FieldDataFieldsContext(); + } + }; + @Inject public FieldDataFieldsFetchSubPhase() { } @@ -66,12 +81,12 @@ public class FieldDataFieldsFetchSubPhase implements FetchSubPhase { @Override public boolean hitExecutionNeeded(SearchContext context) { - return context.hasFieldDataFields(); + return context.hasFetchSubPhaseContext(CONTEXT_FACTORY); } @Override public void hitExecute(SearchContext context, HitContext hitContext) { - for (FieldDataFieldsContext.FieldDataField field : context.fieldDataFields().fields()) { + for (FieldDataFieldsContext.FieldDataField field : ((FieldDataFieldsContext)context.getFetchSubPhaseContext(CONTEXT_FACTORY)).fields()) { if (hitContext.hit().fieldsOrNull() == null) { hitContext.hit().fields(new HashMap(2)); } diff --git a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsParseElement.java b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsParseElement.java index 3c45148cdfa..2088de6aa86 100644 --- a/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsParseElement.java +++ b/core/src/main/java/org/elasticsearch/search/fetch/fielddata/FieldDataFieldsParseElement.java @@ -36,15 +36,16 @@ import org.elasticsearch.search.internal.SearchContext; public class FieldDataFieldsParseElement implements SearchParseElement { @Override public void parse(XContentParser parser, SearchContext context) throws Exception { + FieldDataFieldsContext fieldDataFieldsContext = (FieldDataFieldsContext)context.getFetchSubPhaseContext(FieldDataFieldsFetchSubPhase.CONTEXT_FACTORY); XContentParser.Token token = parser.currentToken(); if (token == XContentParser.Token.START_ARRAY) { while (parser.nextToken() != XContentParser.Token.END_ARRAY) { String fieldName = parser.text(); - context.fieldDataFields().add(new FieldDataFieldsContext.FieldDataField(fieldName)); + fieldDataFieldsContext.add(new FieldDataFieldsContext.FieldDataField(fieldName)); } } else if (token == XContentParser.Token.VALUE_STRING) { String fieldName = parser.text(); - context.fieldDataFields().add(new FieldDataFieldsContext.FieldDataField(fieldName)); + fieldDataFieldsContext.add(new FieldDataFieldsContext.FieldDataField(fieldName)); } else { throw new IllegalStateException("Expected either a VALUE_STRING or an START_ARRAY but got " + token); } 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 f22c889f254..f178f94bb00 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/DefaultSearchContext.java @@ -351,19 +351,6 @@ public class DefaultSearchContext extends SearchContext { this.rescore.add(rescore); } - @Override - public boolean hasFieldDataFields() { - return fieldDataFields != null; - } - - @Override - public FieldDataFieldsContext fieldDataFields() { - if (fieldDataFields == null) { - fieldDataFields = new FieldDataFieldsContext(); - } - return this.fieldDataFields; - } - @Override public boolean hasScriptFields() { return scriptFields != null; diff --git a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java index 12ed9f41342..ac17aca1ec1 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/FilteredSearchContext.java @@ -211,16 +211,6 @@ public abstract class FilteredSearchContext extends SearchContext { in.addRescore(rescore); } - @Override - public boolean hasFieldDataFields() { - return in.hasFieldDataFields(); - } - - @Override - public FieldDataFieldsContext fieldDataFields() { - return in.fieldDataFields(); - } - @Override public boolean hasScriptFields() { return in.hasScriptFields(); 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 0d96ae16412..8829c697d72 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SearchContext.java @@ -186,10 +186,6 @@ public abstract class SearchContext implements Releasable, HasContextAndHeaders public abstract void addRescore(RescoreSearchContext rescore); - public abstract boolean hasFieldDataFields(); - - public abstract FieldDataFieldsContext fieldDataFields(); - public abstract boolean hasScriptFields(); public abstract ScriptFieldsContext scriptFields(); diff --git a/core/src/main/java/org/elasticsearch/search/internal/SubSearchContext.java b/core/src/main/java/org/elasticsearch/search/internal/SubSearchContext.java index f1162a98e86..b8e8344494d 100644 --- a/core/src/main/java/org/elasticsearch/search/internal/SubSearchContext.java +++ b/core/src/main/java/org/elasticsearch/search/internal/SubSearchContext.java @@ -132,19 +132,6 @@ public class SubSearchContext extends FilteredSearchContext { throw new UnsupportedOperationException("Not supported"); } - @Override - public boolean hasFieldDataFields() { - return fieldDataFields != null; - } - - @Override - public FieldDataFieldsContext fieldDataFields() { - if (fieldDataFields == null) { - fieldDataFields = new FieldDataFieldsContext(); - } - return this.fieldDataFields; - } - @Override public boolean hasScriptFields() { return scriptFields != null; diff --git a/core/src/test/java/org/elasticsearch/test/TestSearchContext.java b/core/src/test/java/org/elasticsearch/test/TestSearchContext.java index c0a64ca2ea2..fc0d2e0fba3 100644 --- a/core/src/test/java/org/elasticsearch/test/TestSearchContext.java +++ b/core/src/test/java/org/elasticsearch/test/TestSearchContext.java @@ -245,16 +245,6 @@ public class TestSearchContext extends SearchContext { public void addRescore(RescoreSearchContext rescore) { } - @Override - public boolean hasFieldDataFields() { - return false; - } - - @Override - public FieldDataFieldsContext fieldDataFields() { - return null; - } - @Override public boolean hasScriptFields() { return false;