|
12 | 12 | import org.apache.lucene.search.Query; |
13 | 13 | import org.elasticsearch.common.Strings; |
14 | 14 | import org.elasticsearch.common.regex.Regex; |
| 15 | +import org.elasticsearch.common.settings.Settings; |
15 | 16 | import org.elasticsearch.index.mapper.ConstantFieldType; |
16 | 17 | import org.elasticsearch.index.mapper.KeywordFieldMapper; |
17 | 18 | import org.elasticsearch.index.mapper.MetadataFieldMapper; |
|
20 | 21 | import org.elasticsearch.xpack.cluster.routing.allocation.DataTierAllocationDecider; |
21 | 22 |
|
22 | 23 | import java.util.Collections; |
| 24 | +import java.util.List; |
23 | 25 |
|
24 | 26 | public class DataTierFieldMapper extends MetadataFieldMapper { |
25 | 27 |
|
@@ -52,28 +54,50 @@ protected boolean matches(String pattern, boolean caseInsensitive, SearchExecuti |
52 | 54 | if (caseInsensitive) { |
53 | 55 | pattern = Strings.toLowercaseAscii(pattern); |
54 | 56 | } |
55 | | - String tierPreference = DataTierAllocationDecider.INDEX_ROUTING_PREFER_SETTING.get(context.getIndexSettings().getSettings()); |
56 | | - if (Strings.hasText(tierPreference) == false) { |
| 57 | + |
| 58 | + String tierPreference = getTierPreference(context); |
| 59 | + if (tierPreference == null) { |
57 | 60 | return false; |
58 | 61 | } |
59 | | - // Tier preference can be a comma-delimited list of tiers, ordered by preference |
60 | | - // It was decided we should only test the first of these potentially multiple preferences. |
61 | | - String firstPreference = tierPreference.split(",")[0].trim(); |
62 | | - return Regex.simpleMatch(pattern, firstPreference); |
| 62 | + return Regex.simpleMatch(pattern, tierPreference); |
63 | 63 | } |
64 | 64 |
|
65 | 65 | @Override |
66 | 66 | public Query existsQuery(SearchExecutionContext context) { |
67 | | - String tierPreference = DataTierAllocationDecider.INDEX_ROUTING_PREFER_SETTING.get(context.getIndexSettings().getSettings()); |
68 | | - if (Strings.hasText(tierPreference) == false) { |
| 67 | + String tierPreference = getTierPreference(context); |
| 68 | + if (tierPreference == null) { |
69 | 69 | return new MatchNoDocsQuery(); |
70 | 70 | } |
71 | 71 | return new MatchAllDocsQuery(); |
72 | 72 | } |
73 | 73 |
|
74 | 74 | @Override |
75 | 75 | public ValueFetcher valueFetcher(SearchExecutionContext context, String format) { |
76 | | - throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "]."); |
| 76 | + if (format != null) { |
| 77 | + throw new IllegalArgumentException("Field [" + name() + "] of type [" + typeName() + "] doesn't support formats."); |
| 78 | + } |
| 79 | + |
| 80 | + String tierPreference = getTierPreference(context); |
| 81 | + return tierPreference == null |
| 82 | + ? lookup -> List.of() |
| 83 | + : lookup -> List.of(tierPreference); |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Retrieve the first tier preference from the index setting. If the setting is not |
| 88 | + * present, then return null. |
| 89 | + */ |
| 90 | + private String getTierPreference(SearchExecutionContext context) { |
| 91 | + Settings settings = context.getIndexSettings().getSettings(); |
| 92 | + String value = DataTierAllocationDecider.INDEX_ROUTING_PREFER_SETTING.get(settings); |
| 93 | + |
| 94 | + if (Strings.hasText(value) == false) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + // Tier preference can be a comma-delimited list of tiers, ordered by preference |
| 99 | + // It was decided we should only test the first of these potentially multiple preferences. |
| 100 | + return value.split(",")[0].trim(); |
77 | 101 | } |
78 | 102 | } |
79 | 103 |
|
|
0 commit comments