Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support fetching _tier field value #71379

Merged
merged 7 commits into from
Apr 8, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions docs/reference/mapping/fields.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ fields can be customized when a mapping is created.

The index to which the document belongs.

<<mapping-tier-field,`_tier`>>::

The current data tier preference of the index to which the document belongs.


<<mapping-id-field,`_id`>>::

The document's ID.
Expand Down Expand Up @@ -67,6 +62,10 @@ fields can be customized when a mapping is created.

Application specific metadata.

<<mapping-tier-field,`_tier`>>::

The current data tier preference of the index to which the document belongs.

include::fields/doc-count-field.asciidoc[]

include::fields/field-names-field.asciidoc[]
Expand All @@ -77,10 +76,10 @@ include::fields/id-field.asciidoc[]

include::fields/index-field.asciidoc[]

include::fields/tier-field.asciidoc[]

include::fields/meta-field.asciidoc[]

include::fields/routing-field.asciidoc[]

include::fields/source-field.asciidoc[]

include::fields/tier-field.asciidoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.apache.lucene.search.Query;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.regex.Regex;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.mapper.ConstantFieldType;
import org.elasticsearch.index.mapper.KeywordFieldMapper;
import org.elasticsearch.index.mapper.MetadataFieldMapper;
Expand All @@ -21,6 +22,8 @@

import java.util.Collections;

import static java.util.Collections.singletonList;

public class DataTierFieldMapper extends MetadataFieldMapper {

public static final String NAME = "_tier";
Expand Down Expand Up @@ -52,19 +55,16 @@ protected boolean matches(String pattern, boolean caseInsensitive, SearchExecuti
if (caseInsensitive) {
pattern = Strings.toLowercaseAscii(pattern);
}
String tierPreference = DataTierAllocationDecider.INDEX_ROUTING_PREFER_SETTING.get(context.getIndexSettings().getSettings());
String tierPreference = getTierPreference(context);
if (tierPreference == null) {
return false;
}
// Tier preference can be a comma-delimited list of tiers, ordered by preference
// It was decided we should only test the first of these potentially multiple preferences.
String firstPreference = tierPreference.split(",")[0].trim();
return Regex.simpleMatch(pattern, firstPreference);
return Regex.simpleMatch(pattern, tierPreference);
}

@Override
public Query existsQuery(SearchExecutionContext context) {
String tierPreference = DataTierAllocationDecider.INDEX_ROUTING_PREFER_SETTING.get(context.getIndexSettings().getSettings());
String tierPreference = getTierPreference(context);
if (tierPreference == null) {
return new MatchNoDocsQuery();
}
Expand All @@ -73,7 +73,21 @@ public Query existsQuery(SearchExecutionContext context) {

@Override
public ValueFetcher valueFetcher(SearchExecutionContext context, String format) {
throw new UnsupportedOperationException("Cannot fetch values for internal field [" + name() + "].");
String tierPreference = getTierPreference(context);
return lookup -> singletonList(tierPreference);
Copy link
Contributor Author

@jtibshirani jtibshirani Apr 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just noticed this doesn't handle a missing/ empty setting correctly. I'll push a fix.

Update: while writing tests I noticed we don't handle this correctly and opened #71439. I'll return to this PR once that is merged.

}

private String getTierPreference(SearchExecutionContext context) {
Settings settings = context.getIndexSettings().getSettings();
String value = DataTierAllocationDecider.INDEX_ROUTING_PREFER_SETTING.get(context.getIndexSettings().getSettings());

if (value == null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this will change after #71439 is in, so keeping this comment here for now just to recheck

return null;
}

// Tier preference can be a comma-delimited list of tiers, ordered by preference
// It was decided we should only test the first of these potentially multiple preferences.
return value.split(",")[0].trim();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
import org.elasticsearch.index.IndexSettings;
import org.elasticsearch.index.mapper.MappedFieldType;
import org.elasticsearch.index.mapper.MapperServiceTestCase;
import org.elasticsearch.index.mapper.ValueFetcher;
import org.elasticsearch.index.query.QueryShardException;
import org.elasticsearch.index.query.SearchExecutionContext;
import org.elasticsearch.search.lookup.SourceLookup;
import org.elasticsearch.xpack.cluster.routing.allocation.DataTierAllocationDecider;

import java.io.IOException;
Expand Down Expand Up @@ -66,6 +68,15 @@ public void testRegexpQuery() {
assertThat(e.getMessage(), containsString("Can only use regexp queries on keyword and text fields"));
}

public void testFetchValue() throws IOException {
MappedFieldType ft = DataTierFieldMapper.DataTierFieldType.INSTANCE;
SearchExecutionContext context = createContext();
SourceLookup lookup = new SourceLookup();

ValueFetcher valueFetcher = ft.valueFetcher(context, null);
assertEquals(Arrays.asList("data_warm"), valueFetcher.fetchValues(lookup));
}

private SearchExecutionContext createContext() {
IndexMetadata indexMetadata = IndexMetadata.builder("index")
.settings(
Expand Down