PerFieldMapperCodecTests.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
  3. * or more contributor license agreements. Licensed under the Elastic License
  4. * 2.0 and the Server Side Public License, v 1; you may not use this file except
  5. * in compliance with, at your election, the Elastic License 2.0 or the Server
  6. * Side Public License, v 1.
  7. */
  8. package org.elasticsearch.index.codec;
  9. import org.elasticsearch.cluster.metadata.IndexMetadata;
  10. import org.elasticsearch.common.compress.CompressedXContent;
  11. import org.elasticsearch.common.settings.Settings;
  12. import org.elasticsearch.common.util.BigArrays;
  13. import org.elasticsearch.index.IndexSettings;
  14. import org.elasticsearch.index.MapperTestUtils;
  15. import org.elasticsearch.index.codec.bloomfilter.ES87BloomFilterPostingsFormat;
  16. import org.elasticsearch.index.codec.postings.ES812PostingsFormat;
  17. import org.elasticsearch.index.mapper.MapperService;
  18. import org.elasticsearch.test.ESTestCase;
  19. import java.io.IOException;
  20. import static org.hamcrest.Matchers.instanceOf;
  21. import static org.hamcrest.Matchers.is;
  22. public class PerFieldMapperCodecTests extends ESTestCase {
  23. private static final String MAPPING_1 = """
  24. {
  25. "_data_stream_timestamp": {
  26. "enabled": true
  27. },
  28. "properties": {
  29. "@timestamp": {
  30. "type": "date"
  31. },
  32. "gauge": {
  33. "type": "long"
  34. }
  35. }
  36. }
  37. """;
  38. private static final String MAPPING_2 = """
  39. {
  40. "_data_stream_timestamp": {
  41. "enabled": true
  42. },
  43. "properties": {
  44. "@timestamp": {
  45. "type": "date"
  46. },
  47. "counter": {
  48. "type": "long"
  49. },
  50. "gauge": {
  51. "type": "long"
  52. }
  53. }
  54. }
  55. """;
  56. public void testUseBloomFilter() throws IOException {
  57. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(false, randomBoolean(), false);
  58. assertThat(perFieldMapperCodec.useBloomFilter("_id"), is(true));
  59. assertThat(perFieldMapperCodec.getPostingsFormatForField("_id"), instanceOf(ES87BloomFilterPostingsFormat.class));
  60. assertThat(perFieldMapperCodec.useBloomFilter("another_field"), is(false));
  61. assertThat(perFieldMapperCodec.getPostingsFormatForField("another_field"), instanceOf(ES812PostingsFormat.class));
  62. }
  63. public void testUseBloomFilterWithTimestampFieldEnabled() throws IOException {
  64. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, false);
  65. assertThat(perFieldMapperCodec.useBloomFilter("_id"), is(true));
  66. assertThat(perFieldMapperCodec.getPostingsFormatForField("_id"), instanceOf(ES87BloomFilterPostingsFormat.class));
  67. assertThat(perFieldMapperCodec.useBloomFilter("another_field"), is(false));
  68. assertThat(perFieldMapperCodec.getPostingsFormatForField("another_field"), instanceOf(ES812PostingsFormat.class));
  69. }
  70. public void testUseBloomFilterWithTimestampFieldEnabled_noTimeSeriesMode() throws IOException {
  71. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, false, false);
  72. assertThat(perFieldMapperCodec.useBloomFilter("_id"), is(false));
  73. assertThat(perFieldMapperCodec.getPostingsFormatForField("_id"), instanceOf(ES812PostingsFormat.class));
  74. }
  75. public void testUseBloomFilterWithTimestampFieldEnabled_disableBloomFilter() throws IOException {
  76. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, true);
  77. assertThat(perFieldMapperCodec.useBloomFilter("_id"), is(false));
  78. assertThat(perFieldMapperCodec.getPostingsFormatForField("_id"), instanceOf(ES812PostingsFormat.class));
  79. assertWarnings(
  80. "[index.bloom_filter_for_id_field.enabled] setting was deprecated in Elasticsearch and will be removed in a future release."
  81. );
  82. }
  83. public void testUseES87TSDBEncodingForTimestampField() throws IOException {
  84. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, true);
  85. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(true));
  86. }
  87. public void testDoNotUseES87TSDBEncodingForTimestampFieldNonTimeSeriesIndex() throws IOException {
  88. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, false, true);
  89. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(false));
  90. }
  91. public void testEnableES87TSDBCodec() throws IOException {
  92. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, MAPPING_1);
  93. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(true));
  94. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(true));
  95. }
  96. public void testDisableES87TSDBCodec() throws IOException {
  97. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(false, true, MAPPING_1);
  98. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(false));
  99. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(false));
  100. }
  101. private PerFieldFormatSupplier createFormatSupplier(boolean timestampField, boolean timeSeries, boolean disableBloomFilter)
  102. throws IOException {
  103. Settings.Builder settings = Settings.builder();
  104. if (timeSeries) {
  105. settings.put(IndexSettings.MODE.getKey(), "time_series");
  106. settings.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "field");
  107. }
  108. if (disableBloomFilter) {
  109. settings.put(IndexSettings.BLOOM_FILTER_ID_FIELD_ENABLED_SETTING.getKey(), false);
  110. }
  111. MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), settings.build(), "test");
  112. if (timestampField) {
  113. String mapping = """
  114. {
  115. "_data_stream_timestamp": {
  116. "enabled": true
  117. },
  118. "properties": {
  119. "@timestamp": {
  120. "type": "date"
  121. }
  122. }
  123. }
  124. """;
  125. mapperService.merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
  126. }
  127. return new PerFieldFormatSupplier(mapperService, BigArrays.NON_RECYCLING_INSTANCE);
  128. }
  129. public void testUseES87TSDBEncodingSettingDisabled() throws IOException {
  130. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(false, true, MAPPING_2);
  131. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(false));
  132. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("counter")), is(false));
  133. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(false));
  134. }
  135. public void testUseTimeSeriesModeDisabledCodecDisabled() throws IOException {
  136. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, false, MAPPING_2);
  137. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(false));
  138. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("counter")), is(false));
  139. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(false));
  140. }
  141. public void testUseTimeSeriesModeAndCodecEnabled() throws IOException {
  142. PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, MAPPING_2);
  143. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(true));
  144. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("counter")), is(true));
  145. assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(true));
  146. }
  147. private PerFieldFormatSupplier createFormatSupplier(boolean enableES87TSDBCodec, boolean timeSeries, String mapping)
  148. throws IOException {
  149. Settings.Builder settings = Settings.builder();
  150. if (timeSeries) {
  151. settings.put(IndexSettings.MODE.getKey(), "time_series");
  152. settings.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "field");
  153. }
  154. settings.put(IndexSettings.TIME_SERIES_ES87TSDB_CODEC_ENABLED_SETTING.getKey(), enableES87TSDBCodec);
  155. MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), settings.build(), "test");
  156. mapperService.merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
  157. return new PerFieldFormatSupplier(mapperService, BigArrays.NON_RECYCLING_INSTANCE);
  158. }
  159. }