diff --git a/server/src/main/java/org/elasticsearch/index/query/CoordinatorRewriteContext.java b/server/src/main/java/org/elasticsearch/index/query/CoordinatorRewriteContext.java index c37fdc7987e16..f838b920e614a 100644 --- a/server/src/main/java/org/elasticsearch/index/query/CoordinatorRewriteContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/CoordinatorRewriteContext.java @@ -48,7 +48,7 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - String tierPreference = QueryRewriteContext.getTierPreference(context); + String tierPreference = context.getTierPreference(); return tierPreference == null ? ValueFetcher.EMPTY : ValueFetcher.singleton(tierPreference); } @@ -63,7 +63,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC pattern = Strings.toLowercaseAscii(pattern); } - String tierPreference = QueryRewriteContext.getTierPreference(context); + String tierPreference = context.getTierPreference(); if (tierPreference == null) { return false; } @@ -72,7 +72,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC @Override public Query existsQuery(SearchExecutionContext context) { - String tierPreference = QueryRewriteContext.getTierPreference(context); + String tierPreference = context.getTierPreference(); if (tierPreference == null) { return new MatchNoDocsQuery(); } @@ -158,6 +158,12 @@ public CoordinatorRewriteContext convertToCoordinatorRewriteContext() { return this; } + @Override + public String getTierPreference() { + // dominant branch first (tier preference is configured) + return tier.isEmpty() == false ? tier : null; + } + /** * We're holding on to the index tier in the context as otherwise we'd need * to re-parse it from the index settings when evaluating the _tier field. diff --git a/server/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java b/server/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java index 20dd0f8ef6dda..fce74aa60ab16 100644 --- a/server/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java +++ b/server/src/main/java/org/elasticsearch/index/query/QueryRewriteContext.java @@ -416,13 +416,8 @@ public PointInTimeBuilder getPointInTimeBuilder() { * present, then return null. */ @Nullable - public static String getTierPreference(QueryRewriteContext context) { - if (context instanceof CoordinatorRewriteContext) { - String tier = ((CoordinatorRewriteContext) context).tier(); - // dominant branch first (tier preference is configured) - return tier.isEmpty() == false ? tier : null; - } - Settings settings = context.getIndexSettings().getSettings(); + public String getTierPreference() { + Settings settings = getIndexSettings().getSettings(); String value = DataTier.TIER_PREFERENCE_SETTING.get(settings); if (Strings.hasText(value) == false) { diff --git a/server/src/test/java/org/elasticsearch/index/query/QueryRewriteContextTests.java b/server/src/test/java/org/elasticsearch/index/query/QueryRewriteContextTests.java index b8173aed73af4..a7b2a416dbefa 100644 --- a/server/src/test/java/org/elasticsearch/index/query/QueryRewriteContextTests.java +++ b/server/src/test/java/org/elasticsearch/index/query/QueryRewriteContextTests.java @@ -29,15 +29,13 @@ public class QueryRewriteContextTests extends ESTestCase { public void testGetTierPreference() { { // cold->hot tier preference - IndexMetadata metadata = newIndexMeta( - "index", + IndexMetadata metadata = newIndexMeta("index", Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) .put(DataTier.TIER_PREFERENCE, "data_cold,data_warm,data_hot") .build() ); - QueryRewriteContext context = new QueryRewriteContext( - parserConfig(), + QueryRewriteContext context = new QueryRewriteContext(parserConfig(), null, System::currentTimeMillis, null, @@ -54,17 +52,14 @@ public void testGetTierPreference() { null ); - assertThat(QueryRewriteContext.getTierPreference(context), is("data_cold")); + assertThat(context.getTierPreference(), is("data_cold")); } { // missing tier preference - IndexMetadata metadata = newIndexMeta( - "index", - Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()).build() - ); - QueryRewriteContext context = new QueryRewriteContext( - parserConfig(), + IndexMetadata metadata = + newIndexMeta("index", Settings.builder().put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()).build()); + QueryRewriteContext context = new QueryRewriteContext(parserConfig(), null, System::currentTimeMillis, null, @@ -81,46 +76,42 @@ public void testGetTierPreference() { null ); - assertThat(QueryRewriteContext.getTierPreference(context), is(nullValue())); + assertThat(context.getTierPreference(), is(nullValue())); } { // coordinator rewrite context - IndexMetadata metadata = newIndexMeta( - "index", + IndexMetadata metadata = newIndexMeta("index", Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) .put(DataTier.TIER_PREFERENCE, "data_cold,data_warm,data_hot") .build() ); - CoordinatorRewriteContext coordinatorRewriteContext = new CoordinatorRewriteContext( - parserConfig(), + CoordinatorRewriteContext coordinatorRewriteContext = new CoordinatorRewriteContext(parserConfig(), null, System::currentTimeMillis, new DateFieldRangeInfo(null, null, new DateFieldMapper.DateFieldType(IndexMetadata.EVENT_INGESTED_FIELD_NAME), null), "data_frozen" ); - assertThat(QueryRewriteContext.getTierPreference(coordinatorRewriteContext), is("data_frozen")); + assertThat(coordinatorRewriteContext.getTierPreference(), is("data_frozen")); } { // coordinator rewrite context empty tier - IndexMetadata metadata = newIndexMeta( - "index", + IndexMetadata metadata = newIndexMeta("index", Settings.builder() .put(IndexMetadata.SETTING_VERSION_CREATED, IndexVersion.current()) .put(DataTier.TIER_PREFERENCE, "data_cold,data_warm,data_hot") .build() ); - CoordinatorRewriteContext coordinatorRewriteContext = new CoordinatorRewriteContext( - parserConfig(), + CoordinatorRewriteContext coordinatorRewriteContext = new CoordinatorRewriteContext(parserConfig(), null, System::currentTimeMillis, new DateFieldRangeInfo(null, null, new DateFieldMapper.DateFieldType(IndexMetadata.EVENT_INGESTED_FIELD_NAME), null), "" ); - assertThat(QueryRewriteContext.getTierPreference(coordinatorRewriteContext), is(nullValue())); + assertThat(coordinatorRewriteContext.getTierPreference(), is(nullValue())); } } diff --git a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldMapper.java b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldMapper.java index a7a417d032593..0e185a90ed39b 100644 --- a/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldMapper.java +++ b/x-pack/plugin/core/src/main/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldMapper.java @@ -53,7 +53,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC pattern = Strings.toLowercaseAscii(pattern); } - String tierPreference = QueryRewriteContext.getTierPreference(context); + String tierPreference = context.getTierPreference(); if (tierPreference == null) { return false; } @@ -62,7 +62,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC @Override public Query existsQuery(SearchExecutionContext context) { - String tierPreference = QueryRewriteContext.getTierPreference(context); + String tierPreference = context.getTierPreference(); if (tierPreference == null) { return new MatchNoDocsQuery(); } @@ -75,7 +75,7 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format) throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); } - String tierPreference = QueryRewriteContext.getTierPreference(context); + String tierPreference = context.getTierPreference(); return tierPreference == null ? ValueFetcher.EMPTY : ValueFetcher.singleton(tierPreference); } }