|
@@ -48,6 +48,7 @@ import org.apache.lucene.util.BytesRef;
|
|
|
import org.elasticsearch.cluster.metadata.IndexMetadata;
|
|
|
import org.elasticsearch.common.Strings;
|
|
|
import org.elasticsearch.common.lucene.search.MultiPhrasePrefixQuery;
|
|
|
+import org.elasticsearch.common.settings.Settings;
|
|
|
import org.elasticsearch.index.IndexMode;
|
|
|
import org.elasticsearch.index.IndexSettings;
|
|
|
import org.elasticsearch.index.IndexVersion;
|
|
@@ -79,6 +80,8 @@ import org.elasticsearch.xcontent.XContentFactory;
|
|
|
import org.junit.AssumptionViolatedException;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.time.Instant;
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
import java.util.Arrays;
|
|
|
import java.util.Collections;
|
|
|
import java.util.HashMap;
|
|
@@ -1432,4 +1435,143 @@ public class TextFieldMapperTests extends MapperTestCase {
|
|
|
assertFalse(dv.advanceExact(3));
|
|
|
});
|
|
|
}
|
|
|
+
|
|
|
+ public void testNormalizeByDefault() throws IOException {
|
|
|
+ // given
|
|
|
+ Settings.Builder indexSettingsBuilder = getIndexSettingsBuilder();
|
|
|
+ indexSettingsBuilder.put(IndexSettings.MODE.getKey(), IndexMode.STANDARD.getName());
|
|
|
+ Settings indexSettings = indexSettingsBuilder.build();
|
|
|
+
|
|
|
+ XContentBuilder mapping = mapping(b -> {
|
|
|
+ b.startObject("potato");
|
|
|
+ b.field("type", "text");
|
|
|
+ b.endObject();
|
|
|
+ });
|
|
|
+
|
|
|
+ var source = source(b -> b.field("potato", "a potato flew around my room"));
|
|
|
+
|
|
|
+ // when
|
|
|
+ DocumentMapper mapper = createMapperService(indexSettings, mapping).documentMapper();
|
|
|
+ ParsedDocument doc = mapper.parse(source);
|
|
|
+
|
|
|
+ List<IndexableField> fields = doc.rootDoc().getFields("potato");
|
|
|
+ IndexableFieldType fieldType = fields.get(0).fieldType();
|
|
|
+
|
|
|
+ // then
|
|
|
+ assertThat(fieldType.omitNorms(), is(false));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testNormalizeWhenIndexModeIsNotGiven() throws IOException {
|
|
|
+ // given
|
|
|
+ Settings.Builder indexSettingsBuilder = getIndexSettingsBuilder();
|
|
|
+ Settings indexSettings = indexSettingsBuilder.build();
|
|
|
+
|
|
|
+ XContentBuilder mapping = mapping(b -> {
|
|
|
+ b.startObject("potato");
|
|
|
+ b.field("type", "text");
|
|
|
+ b.endObject();
|
|
|
+ });
|
|
|
+
|
|
|
+ var source = source(b -> b.field("potato", "a potato flew around my room"));
|
|
|
+
|
|
|
+ // when
|
|
|
+ DocumentMapper mapper = createMapperService(indexSettings, mapping).documentMapper();
|
|
|
+ ParsedDocument doc = mapper.parse(source);
|
|
|
+
|
|
|
+ List<IndexableField> fields = doc.rootDoc().getFields("potato");
|
|
|
+ IndexableFieldType fieldType = fields.get(0).fieldType();
|
|
|
+
|
|
|
+ // then
|
|
|
+ assertThat(fieldType.omitNorms(), is(false));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testNormalizeWhenIndexModeIsNull() throws IOException {
|
|
|
+ // given
|
|
|
+ Settings.Builder indexSettingsBuilder = getIndexSettingsBuilder();
|
|
|
+ indexSettingsBuilder.put(IndexSettings.MODE.getKey(), (String) null);
|
|
|
+ Settings indexSettings = indexSettingsBuilder.build();
|
|
|
+
|
|
|
+ XContentBuilder mapping = mapping(b -> {
|
|
|
+ b.startObject("potato");
|
|
|
+ b.field("type", "text");
|
|
|
+ b.endObject();
|
|
|
+ });
|
|
|
+
|
|
|
+ var source = source(b -> b.field("potato", "a potato flew around my room"));
|
|
|
+
|
|
|
+ // when
|
|
|
+ DocumentMapper mapper = createMapperService(indexSettings, mapping).documentMapper();
|
|
|
+ ParsedDocument doc = mapper.parse(source);
|
|
|
+
|
|
|
+ List<IndexableField> fields = doc.rootDoc().getFields("potato");
|
|
|
+ IndexableFieldType fieldType = fields.get(0).fieldType();
|
|
|
+
|
|
|
+ // then
|
|
|
+ assertThat(fieldType.omitNorms(), is(false));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testDontNormalizeWhenIndexModeIsLogsDB() throws IOException {
|
|
|
+ // given
|
|
|
+ Settings.Builder indexSettingsBuilder = getIndexSettingsBuilder();
|
|
|
+ indexSettingsBuilder.put(IndexSettings.MODE.getKey(), IndexMode.LOGSDB.getName());
|
|
|
+ Settings indexSettings = indexSettingsBuilder.build();
|
|
|
+
|
|
|
+ XContentBuilder mapping = mapping(b -> {
|
|
|
+ b.startObject("potato");
|
|
|
+ b.field("type", "text");
|
|
|
+ b.endObject();
|
|
|
+ });
|
|
|
+
|
|
|
+ var source = source(b -> {
|
|
|
+ b.field("@timestamp", Instant.now());
|
|
|
+ b.field("potato", "a potato flew around my room");
|
|
|
+ });
|
|
|
+
|
|
|
+ // when
|
|
|
+ DocumentMapper mapper = createMapperService(indexSettings, mapping).documentMapper();
|
|
|
+ ParsedDocument doc = mapper.parse(source);
|
|
|
+
|
|
|
+ List<IndexableField> fields = doc.rootDoc().getFields("potato");
|
|
|
+ IndexableFieldType fieldType = fields.get(0).fieldType();
|
|
|
+
|
|
|
+ // then
|
|
|
+ assertThat(fieldType.omitNorms(), is(true));
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testDontNormalizeWhenIndexModeIsTSDB() throws IOException {
|
|
|
+ // given
|
|
|
+ Instant currentTime = Instant.now();
|
|
|
+ Settings.Builder indexSettingsBuilder = getIndexSettingsBuilder();
|
|
|
+ indexSettingsBuilder.put(IndexSettings.MODE.getKey(), IndexMode.TIME_SERIES.getName())
|
|
|
+ .put(IndexSettings.TIME_SERIES_START_TIME.getKey(), currentTime.minus(1, ChronoUnit.HOURS).toEpochMilli())
|
|
|
+ .put(IndexSettings.TIME_SERIES_END_TIME.getKey(), currentTime.plus(1, ChronoUnit.HOURS).toEpochMilli())
|
|
|
+ .put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "dimension");
|
|
|
+ Settings indexSettings = indexSettingsBuilder.build();
|
|
|
+
|
|
|
+ XContentBuilder mapping = mapping(b -> {
|
|
|
+ b.startObject("potato");
|
|
|
+ b.field("type", "text");
|
|
|
+ b.endObject();
|
|
|
+
|
|
|
+ b.startObject("@timestamp");
|
|
|
+ b.field("type", "date");
|
|
|
+ b.endObject();
|
|
|
+ });
|
|
|
+
|
|
|
+ var source = source(TimeSeriesRoutingHashFieldMapper.DUMMY_ENCODED_VALUE, b -> {
|
|
|
+ b.field("@timestamp", Instant.now());
|
|
|
+ b.field("potato", "a potato flew around my room");
|
|
|
+ }, null);
|
|
|
+
|
|
|
+ // when
|
|
|
+ DocumentMapper mapper = createMapperService(indexSettings, mapping).documentMapper();
|
|
|
+ ParsedDocument doc = mapper.parse(source);
|
|
|
+
|
|
|
+ List<IndexableField> fields = doc.rootDoc().getFields("potato");
|
|
|
+ IndexableFieldType fieldType = fields.get(0).fieldType();
|
|
|
+
|
|
|
+ // then
|
|
|
+ assertThat(fieldType.omitNorms(), is(true));
|
|
|
+ }
|
|
|
+
|
|
|
}
|