123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180 |
- /*
- * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
- * or more contributor license agreements. Licensed under the Elastic License
- * 2.0 and the Server Side Public License, v 1; you may not use this file except
- * in compliance with, at your election, the Elastic License 2.0 or the Server
- * Side Public License, v 1.
- */
- package org.elasticsearch.index.codec;
- import org.elasticsearch.cluster.metadata.IndexMetadata;
- import org.elasticsearch.common.compress.CompressedXContent;
- import org.elasticsearch.common.settings.Settings;
- import org.elasticsearch.common.util.BigArrays;
- import org.elasticsearch.index.IndexSettings;
- import org.elasticsearch.index.MapperTestUtils;
- import org.elasticsearch.index.codec.bloomfilter.ES87BloomFilterPostingsFormat;
- import org.elasticsearch.index.codec.postings.ES812PostingsFormat;
- import org.elasticsearch.index.mapper.MapperService;
- import org.elasticsearch.test.ESTestCase;
- import java.io.IOException;
- import static org.hamcrest.Matchers.instanceOf;
- import static org.hamcrest.Matchers.is;
- public class PerFieldMapperCodecTests extends ESTestCase {
- private static final String MAPPING_1 = """
- {
- "_data_stream_timestamp": {
- "enabled": true
- },
- "properties": {
- "@timestamp": {
- "type": "date"
- },
- "gauge": {
- "type": "long"
- }
- }
- }
- """;
- private static final String MAPPING_2 = """
- {
- "_data_stream_timestamp": {
- "enabled": true
- },
- "properties": {
- "@timestamp": {
- "type": "date"
- },
- "counter": {
- "type": "long"
- },
- "gauge": {
- "type": "long"
- }
- }
- }
- """;
- public void testUseBloomFilter() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(false, randomBoolean(), false);
- assertThat(perFieldMapperCodec.useBloomFilter("_id"), is(true));
- assertThat(perFieldMapperCodec.getPostingsFormatForField("_id"), instanceOf(ES87BloomFilterPostingsFormat.class));
- assertThat(perFieldMapperCodec.useBloomFilter("another_field"), is(false));
- assertThat(perFieldMapperCodec.getPostingsFormatForField("another_field"), instanceOf(ES812PostingsFormat.class));
- }
- public void testUseBloomFilterWithTimestampFieldEnabled() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, false);
- assertThat(perFieldMapperCodec.useBloomFilter("_id"), is(true));
- assertThat(perFieldMapperCodec.getPostingsFormatForField("_id"), instanceOf(ES87BloomFilterPostingsFormat.class));
- assertThat(perFieldMapperCodec.useBloomFilter("another_field"), is(false));
- assertThat(perFieldMapperCodec.getPostingsFormatForField("another_field"), instanceOf(ES812PostingsFormat.class));
- }
- public void testUseBloomFilterWithTimestampFieldEnabled_noTimeSeriesMode() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, false, false);
- assertThat(perFieldMapperCodec.useBloomFilter("_id"), is(false));
- assertThat(perFieldMapperCodec.getPostingsFormatForField("_id"), instanceOf(ES812PostingsFormat.class));
- }
- public void testUseBloomFilterWithTimestampFieldEnabled_disableBloomFilter() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, true);
- assertThat(perFieldMapperCodec.useBloomFilter("_id"), is(false));
- assertThat(perFieldMapperCodec.getPostingsFormatForField("_id"), instanceOf(ES812PostingsFormat.class));
- assertWarnings(
- "[index.bloom_filter_for_id_field.enabled] setting was deprecated in Elasticsearch and will be removed in a future release."
- );
- }
- public void testUseES87TSDBEncodingForTimestampField() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, true);
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(true));
- }
- public void testDoNotUseES87TSDBEncodingForTimestampFieldNonTimeSeriesIndex() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, false, true);
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(false));
- }
- public void testEnableES87TSDBCodec() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, MAPPING_1);
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(true));
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(true));
- }
- public void testDisableES87TSDBCodec() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(false, true, MAPPING_1);
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(false));
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(false));
- }
- private PerFieldFormatSupplier createFormatSupplier(boolean timestampField, boolean timeSeries, boolean disableBloomFilter)
- throws IOException {
- Settings.Builder settings = Settings.builder();
- if (timeSeries) {
- settings.put(IndexSettings.MODE.getKey(), "time_series");
- settings.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "field");
- }
- if (disableBloomFilter) {
- settings.put(IndexSettings.BLOOM_FILTER_ID_FIELD_ENABLED_SETTING.getKey(), false);
- }
- MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), settings.build(), "test");
- if (timestampField) {
- String mapping = """
- {
- "_data_stream_timestamp": {
- "enabled": true
- },
- "properties": {
- "@timestamp": {
- "type": "date"
- }
- }
- }
- """;
- mapperService.merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
- }
- return new PerFieldFormatSupplier(mapperService, BigArrays.NON_RECYCLING_INSTANCE);
- }
- public void testUseES87TSDBEncodingSettingDisabled() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(false, true, MAPPING_2);
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(false));
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("counter")), is(false));
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(false));
- }
- public void testUseTimeSeriesModeDisabledCodecDisabled() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, false, MAPPING_2);
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(false));
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("counter")), is(false));
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(false));
- }
- public void testUseTimeSeriesModeAndCodecEnabled() throws IOException {
- PerFieldFormatSupplier perFieldMapperCodec = createFormatSupplier(true, true, MAPPING_2);
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("@timestamp")), is(true));
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("counter")), is(true));
- assertThat((perFieldMapperCodec.useTSDBDocValuesFormat("gauge")), is(true));
- }
- private PerFieldFormatSupplier createFormatSupplier(boolean enableES87TSDBCodec, boolean timeSeries, String mapping)
- throws IOException {
- Settings.Builder settings = Settings.builder();
- if (timeSeries) {
- settings.put(IndexSettings.MODE.getKey(), "time_series");
- settings.put(IndexMetadata.INDEX_ROUTING_PATH.getKey(), "field");
- }
- settings.put(IndexSettings.TIME_SERIES_ES87TSDB_CODEC_ENABLED_SETTING.getKey(), enableES87TSDBCodec);
- MapperService mapperService = MapperTestUtils.newMapperService(xContentRegistry(), createTempDir(), settings.build(), "test");
- mapperService.merge("type", new CompressedXContent(mapping), MapperService.MergeReason.MAPPING_UPDATE);
- return new PerFieldFormatSupplier(mapperService, BigArrays.NON_RECYCLING_INSTANCE);
- }
- }
|