From 5bcd1f38cd29478a0040ac36bc57a5066202b9aa Mon Sep 17 00:00:00 2001 From: Julie Tibshirani Date: Thu, 8 Apr 2021 10:04:44 -0700 Subject: [PATCH] Make sure _tier field handles missing setting (#71439) When `_tier_preference` isn't set, getting it returns an empty string "". This PR updates the _tier metadata field to detect this case correctly. --- .../mapper/DataTierFieldMapper.java | 4 +-- .../mapper/DataTierFieldTypeTests.java | 29 +++++++++++++++++++ 2 files changed, 31 insertions(+), 2 deletions(-) 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 e56ebc55f5ee0..33324f43b1ff0 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, SearchExecuti pattern = Strings.toLowercaseAscii(pattern); } String tierPreference = DataTierAllocationDecider.INDEX_ROUTING_PREFER_SETTING.get(context.getIndexSettings().getSettings()); - if (tierPreference == null) { + if (Strings.hasText(tierPreference) == false) { return false; } // Tier preference can be a comma-delimited list of tiers, ordered by preference @@ -65,7 +65,7 @@ protected boolean matches(String pattern, boolean caseInsensitive, SearchExecuti @Override public Query existsQuery(SearchExecutionContext context) { String tierPreference = DataTierAllocationDecider.INDEX_ROUTING_PREFER_SETTING.get(context.getIndexSettings().getSettings()); - if (tierPreference == null) { + if (Strings.hasText(tierPreference) == false) { return new MatchNoDocsQuery(); } return new MatchAllDocsQuery(); diff --git a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldTypeTests.java b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldTypeTests.java index 9f318bec9b4e7..298ea5e23e2ec 100644 --- a/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldTypeTests.java +++ b/x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/cluster/routing/allocation/mapper/DataTierFieldTypeTests.java @@ -42,6 +42,9 @@ public void testWildcardQuery() { assertEquals(new MatchAllDocsQuery(), ft.wildcardQuery("Data_Warm", null, true, createContext())); assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("Data_Warm", null, false, createContext())); assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("noSuchRole", null, createContext())); + + assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("data_*", null, createContextWithoutSetting())); + assertEquals(new MatchNoDocsQuery(), ft.wildcardQuery("*", null, createContextWithoutSetting())); } public void testTermQuery() { @@ -49,12 +52,24 @@ public void testTermQuery() { assertEquals(new MatchAllDocsQuery(), ft.termQuery("data_warm", createContext())); assertEquals(new MatchNoDocsQuery(), ft.termQuery("data_hot", createContext())); assertEquals(new MatchNoDocsQuery(), ft.termQuery("noSuchRole", createContext())); + + assertEquals(new MatchNoDocsQuery(), ft.termQuery("data_warm", createContextWithoutSetting())); + assertEquals(new MatchNoDocsQuery(), ft.termQuery("", createContextWithoutSetting())); } public void testTermsQuery() { MappedFieldType ft = DataTierFieldMapper.DataTierFieldType.INSTANCE; assertEquals(new MatchAllDocsQuery(), ft.termsQuery(Arrays.asList("data_warm"), createContext())); assertEquals(new MatchNoDocsQuery(), ft.termsQuery(Arrays.asList("data_cold", "data_frozen"), createContext())); + + assertEquals(new MatchNoDocsQuery(), ft.termsQuery(Arrays.asList("data_warm"), createContextWithoutSetting())); + assertEquals(new MatchNoDocsQuery(), ft.termsQuery(Arrays.asList(""), createContextWithoutSetting())); + } + + public void testExistsQuery() { + MappedFieldType ft = DataTierFieldMapper.DataTierFieldType.INSTANCE; + assertEquals(new MatchAllDocsQuery(), ft.existsQuery(createContext())); + assertEquals(new MatchNoDocsQuery(), ft.existsQuery(createContextWithoutSetting())); } public void testRegexpQuery() { @@ -102,4 +117,18 @@ private SearchExecutionContext createContext() { emptyMap() ); } + + private SearchExecutionContext createContextWithoutSetting() { + IndexMetadata indexMetadata = IndexMetadata.builder("index") + .settings(Settings.builder() + .put(IndexMetadata.SETTING_VERSION_CREATED, Version.CURRENT) + .build()) + .numberOfShards(1) + .numberOfReplicas(0) + .build(); + IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY); + return new SearchExecutionContext(0, 0, indexSettings, null, null, null, null, null, null, + xContentRegistry(), writableRegistry(), null, null, System::currentTimeMillis, null, + value -> true, () -> true, null, emptyMap()); + } }