Skip to content

Commit

Permalink
minor refactoring and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andreidan committed Oct 17, 2024
1 parent b44c9b2 commit 4717b82
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.query.CoordinatorRewriteContext;
import org.elasticsearch.index.query.QueryRewriteContext;
import org.elasticsearch.index.query.SearchExecutionContext;
Expand Down Expand Up @@ -54,10 +55,8 @@ protected boolean matches(String pattern, boolean caseInsensitive, QueryRewriteC
pattern = Strings.toLowercaseAscii(pattern);
}

String tierPreference = context instanceof CoordinatorRewriteContext
? ((CoordinatorRewriteContext) context).tier()
: getTierPreference(context);
if (tierPreference == null || tierPreference.isEmpty()) {
String tierPreference = getTierPreference(context);
if (tierPreference == null) {
return false;
}
return Regex.simpleMatch(pattern, tierPreference);
Expand Down Expand Up @@ -86,7 +85,13 @@ public ValueFetcher valueFetcher(SearchExecutionContext context, String format)
* Retrieve the first tier preference from the index setting. If the setting is not
* present, then return null.
*/
@Nullable
static String getTierPreference(QueryRewriteContext context) {
if (context instanceof CoordinatorRewriteContext) {
String tier = ((CoordinatorRewriteContext) context).tier();
// dominant branch first (tier preference is configured) first
return tier.isEmpty() == false ? tier : null;
}
Settings settings = context.getIndexSettings().getSettings();
String value = DataTier.TIER_PREFERENCE_SETTING.get(settings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.core.Nullable;
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.ConstantFieldType;
import org.elasticsearch.index.mapper.DataTierFieldMapper;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.indices.TermsLookup;
import org.elasticsearch.xcontent.XContentBuilder;
Expand Down Expand Up @@ -347,6 +348,23 @@ protected Query doToQuery(SearchExecutionContext context) throws IOException {
return fieldType.termsQuery(values, context);
}

@Override
protected QueryBuilder doCoordinatorRewrite(CoordinatorRewriteContext coordinatorRewriteContext) {
if (fieldName.equals(DataTierFieldMapper.NAME) == false) {
return this;
}
final MappedFieldType fieldType = coordinatorRewriteContext.getFieldType(DataTierFieldMapper.NAME);
if (fieldType instanceof final DataTierFieldMapper.DataTierFieldType tierFieldType) {
for (Object value : values) {
Query tierFieldQuery = tierFieldType.innerTermsQuery(value, coordinatorRewriteContext);
if (tierFieldQuery instanceof MatchNoDocsQuery) {
return new MatchNoneQueryBuilder("The \"" + getName() + "\" query was rewritten to a \"match_none\" query.");
}
}
}
return this;
}

private static void fetch(TermsLookup termsLookup, Client client, ActionListener<List<Object>> actionListener) {
GetRequest getRequest = new GetRequest(termsLookup.index(), termsLookup.id());
getRequest.preference("_local").routing(termsLookup.routing());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.index.query.TermsQueryBuilder;
import org.elasticsearch.index.shard.IndexLongFieldRange;
import org.elasticsearch.indices.DateFieldRangeInfo;
import org.elasticsearch.indices.IndicesService;
Expand Down Expand Up @@ -1052,17 +1053,35 @@ public void testCanMatchSkipsPartiallyMountedIndicesWhenFrozenNodesUnavailable()
final IndexMetadata partiallyMountedIndexMetadata = getIndexMetadata(partiallyMountedIndex);
waitUntilAllShardsAreUnassigned(partiallyMountedIndexMetadata.getIndex());

TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("_tier", "data_content");
List<String> indicesToSearch = List.of(regularIndex, partiallyMountedIndex);
SearchRequest request = new SearchRequest().indices(indicesToSearch.toArray(new String[0]))
.source(new SearchSourceBuilder().query(termQueryBuilder));
{
// term query
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("_tier", "data_content");
List<String> indicesToSearch = List.of(regularIndex, partiallyMountedIndex);
SearchRequest request = new SearchRequest().indices(indicesToSearch.toArray(new String[0]))
.source(new SearchSourceBuilder().query(termQueryBuilder));

assertResponse(client().search(request), searchResponse -> {
// as we excluded the frozen tier we shouldn't get any failures
assertThat(searchResponse.getFailedShards(), equalTo(0));
// we should be receiving all the hits from the index that's in the data_content tier
assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) numDocsRegularIndex));
});
assertResponse(client().search(request), searchResponse -> {
// as we excluded the frozen tier we shouldn't get any failures
assertThat(searchResponse.getFailedShards(), equalTo(0));
// we should be receiving all the hits from the index that's in the data_content tier
assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) numDocsRegularIndex));
});
}

{
// termS query
TermsQueryBuilder termsQueryBuilder = QueryBuilders.termsQuery("_tier", "data_hot", "data_content");
List<String> indicesToSearch = List.of(regularIndex, partiallyMountedIndex);
SearchRequest request = new SearchRequest().indices(indicesToSearch.toArray(new String[0]))
.source(new SearchSourceBuilder().query(termsQueryBuilder));

assertResponse(client().search(request), searchResponse -> {
// as we excluded the frozen tier we shouldn't get any failures
assertThat(searchResponse.getFailedShards(), equalTo(0));
// we should be receiving all the hits from the index that's in the data_content tier
assertThat(searchResponse.getHits().getTotalHits().value, equalTo((long) numDocsRegularIndex));
});
}
}

private void createIndexWithOnlyOneTimestampField(String timestampField, String index, int numShards, Settings extraSettings)
Expand Down

0 comments on commit 4717b82

Please sign in to comment.