Search in sources :

Example 96 with IndexMetaData

use of org.elasticsearch.cluster.metadata.IndexMetaData in project elasticsearch by elastic.

the class IndexSettingsTests method testMergedSettingsArePassed.

public void testMergedSettingsArePassed() {
    Version version = VersionUtils.getPreviousVersion();
    Settings theSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).put(IndexMetaData.SETTING_INDEX_UUID, "0xdeadbeef").build();
    final AtomicInteger integer = new AtomicInteger(0);
    final StringBuilder builder = new StringBuilder();
    Setting<Integer> integerSetting = Setting.intSetting("index.test.setting.int", -1, Property.Dynamic, Property.IndexScope);
    Setting<String> notUpdated = new Setting<>("index.not.updated", "", Function.identity(), Property.Dynamic, Property.IndexScope);
    IndexSettings settings = newIndexSettings(newIndexMeta("index", theSettings), Settings.EMPTY, integerSetting, notUpdated);
    settings.getScopedSettings().addSettingsUpdateConsumer(integerSetting, integer::set);
    settings.getScopedSettings().addSettingsUpdateConsumer(notUpdated, builder::append);
    assertEquals(0, integer.get());
    assertEquals("", builder.toString());
    IndexMetaData newMetaData = newIndexMeta("index", Settings.builder().put(settings.getIndexMetaData().getSettings()).put("index.test.setting.int", 42).build());
    assertTrue(settings.updateIndexMetaData(newMetaData));
    assertSame(settings.getIndexMetaData(), newMetaData);
    assertEquals(42, integer.get());
    assertEquals("", builder.toString());
    integer.set(0);
    assertTrue(settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(settings.getIndexMetaData().getSettings()).put("index.not.updated", "boom").build())));
    assertEquals("boom", builder.toString());
    assertEquals("not updated - we preserve the old settings", 0, integer.get());
}
Also used : Setting(org.elasticsearch.common.settings.Setting) HasToString.hasToString(org.hamcrest.object.HasToString.hasToString) StringContains.containsString(org.hamcrest.core.StringContains.containsString) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Version(org.elasticsearch.Version) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexScopedSettings(org.elasticsearch.common.settings.IndexScopedSettings) Settings(org.elasticsearch.common.settings.Settings)

Example 97 with IndexMetaData

use of org.elasticsearch.cluster.metadata.IndexMetaData in project elasticsearch by elastic.

the class SearchSlowLogTests method testLevelSetting.

public void testLevelSetting() {
    SlowLogLevel level = randomFrom(SlowLogLevel.values());
    IndexMetaData metaData = newIndexMeta("index", Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_LEVEL.getKey(), level).build());
    IndexSettings settings = new IndexSettings(metaData, Settings.EMPTY);
    SearchSlowLog log = new SearchSlowLog(settings);
    assertEquals(level, log.getLevel());
    level = randomFrom(SlowLogLevel.values());
    settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_LEVEL.getKey(), level).build()));
    assertEquals(level, log.getLevel());
    level = randomFrom(SlowLogLevel.values());
    settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_LEVEL.getKey(), level).build()));
    assertEquals(level, log.getLevel());
    settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_LEVEL.getKey(), level).build()));
    assertEquals(level, log.getLevel());
    settings.updateIndexMetaData(newIndexMeta("index", Settings.EMPTY));
    assertEquals(SlowLogLevel.TRACE, log.getLevel());
    metaData = newIndexMeta("index", Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build());
    settings = new IndexSettings(metaData, Settings.EMPTY);
    log = new SearchSlowLog(settings);
    try {
        settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_LEVEL.getKey(), "NOT A LEVEL").build()));
        fail();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "No enum constant org.elasticsearch.index.SlowLogLevel.NOT A LEVEL");
    }
    assertEquals(SlowLogLevel.TRACE, log.getLevel());
}
Also used : IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 98 with IndexMetaData

use of org.elasticsearch.cluster.metadata.IndexMetaData in project elasticsearch by elastic.

the class SearchSlowLogTests method testSetFetchLevels.

public void testSetFetchLevels() {
    IndexMetaData metaData = newIndexMeta("index", Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_TRACE_SETTING.getKey(), "100ms").put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_DEBUG_SETTING.getKey(), "200ms").put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_INFO_SETTING.getKey(), "300ms").put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_WARN_SETTING.getKey(), "400ms").build());
    IndexSettings settings = new IndexSettings(metaData, Settings.EMPTY);
    SearchSlowLog log = new SearchSlowLog(settings);
    assertEquals(TimeValue.timeValueMillis(100).nanos(), log.getFetchTraceThreshold());
    assertEquals(TimeValue.timeValueMillis(200).nanos(), log.getFetchDebugThreshold());
    assertEquals(TimeValue.timeValueMillis(300).nanos(), log.getFetchInfoThreshold());
    assertEquals(TimeValue.timeValueMillis(400).nanos(), log.getFetchWarnThreshold());
    settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_TRACE_SETTING.getKey(), "120ms").put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_DEBUG_SETTING.getKey(), "220ms").put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_INFO_SETTING.getKey(), "320ms").put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_WARN_SETTING.getKey(), "420ms").build()));
    assertEquals(TimeValue.timeValueMillis(120).nanos(), log.getFetchTraceThreshold());
    assertEquals(TimeValue.timeValueMillis(220).nanos(), log.getFetchDebugThreshold());
    assertEquals(TimeValue.timeValueMillis(320).nanos(), log.getFetchInfoThreshold());
    assertEquals(TimeValue.timeValueMillis(420).nanos(), log.getFetchWarnThreshold());
    metaData = newIndexMeta("index", Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).build());
    settings.updateIndexMetaData(metaData);
    assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchTraceThreshold());
    assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchDebugThreshold());
    assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchInfoThreshold());
    assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchWarnThreshold());
    settings = new IndexSettings(metaData, Settings.EMPTY);
    log = new SearchSlowLog(settings);
    assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchTraceThreshold());
    assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchDebugThreshold());
    assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchInfoThreshold());
    assertEquals(TimeValue.timeValueMillis(-1).nanos(), log.getFetchWarnThreshold());
    try {
        settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_TRACE_SETTING.getKey(), "NOT A TIME VALUE").build()));
        fail();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "failed to parse setting [index.search.slowlog.threshold.fetch.trace] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized");
    }
    try {
        settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_DEBUG_SETTING.getKey(), "NOT A TIME VALUE").build()));
        fail();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "failed to parse setting [index.search.slowlog.threshold.fetch.debug] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized");
    }
    try {
        settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_INFO_SETTING.getKey(), "NOT A TIME VALUE").build()));
        fail();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "failed to parse setting [index.search.slowlog.threshold.fetch.info] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized");
    }
    try {
        settings.updateIndexMetaData(newIndexMeta("index", Settings.builder().put(SearchSlowLog.INDEX_SEARCH_SLOWLOG_THRESHOLD_FETCH_WARN_SETTING.getKey(), "NOT A TIME VALUE").build()));
        fail();
    } catch (IllegalArgumentException ex) {
        assertEquals(ex.getMessage(), "failed to parse setting [index.search.slowlog.threshold.fetch.warn] with value [NOT A TIME VALUE] as a time value: unit is missing or unrecognized");
    }
}
Also used : IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 99 with IndexMetaData

use of org.elasticsearch.cluster.metadata.IndexMetaData in project elasticsearch by elastic.

the class ScaledFloatFieldTypeTests method testFieldData.

public void testFieldData() throws IOException {
    ScaledFloatFieldMapper.ScaledFloatFieldType ft = new ScaledFloatFieldMapper.ScaledFloatFieldType();
    ft.setScalingFactor(0.1 + randomDouble() * 100);
    Directory dir = newDirectory();
    IndexWriter w = new IndexWriter(dir, new IndexWriterConfig(null));
    Document doc = new Document();
    doc.add(new SortedNumericDocValuesField("scaled_float1", 10));
    doc.add(new SortedNumericDocValuesField("scaled_float2", 5));
    doc.add(new SortedNumericDocValuesField("scaled_float2", 12));
    w.addDocument(doc);
    try (DirectoryReader reader = DirectoryReader.open(w)) {
        IndexMetaData indexMetadata = new IndexMetaData.Builder("index").settings(Settings.builder().put("index.version.created", Version.CURRENT).put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build()).build();
        IndexSettings indexSettings = new IndexSettings(indexMetadata, Settings.EMPTY);
        // single-valued
        ft.setName("scaled_float1");
        IndexNumericFieldData fielddata = (IndexNumericFieldData) ft.fielddataBuilder().build(indexSettings, ft, null, null, null);
        assertEquals(fielddata.getNumericType(), IndexNumericFieldData.NumericType.DOUBLE);
        AtomicNumericFieldData leafFieldData = fielddata.load(reader.leaves().get(0));
        SortedNumericDoubleValues values = leafFieldData.getDoubleValues();
        values.setDocument(0);
        assertEquals(1, values.count());
        assertEquals(10 / ft.getScalingFactor(), values.valueAt(0), 10e-5);
        // multi-valued
        ft.setName("scaled_float2");
        fielddata = (IndexNumericFieldData) ft.fielddataBuilder().build(indexSettings, ft, null, null, null);
        leafFieldData = fielddata.load(reader.leaves().get(0));
        values = leafFieldData.getDoubleValues();
        values.setDocument(0);
        assertEquals(2, values.count());
        assertEquals(5 / ft.getScalingFactor(), values.valueAt(0), 10e-5);
        assertEquals(12 / ft.getScalingFactor(), values.valueAt(1), 10e-5);
    }
    IOUtils.close(w, dir);
}
Also used : DirectoryReader(org.apache.lucene.index.DirectoryReader) IndexSettings(org.elasticsearch.index.IndexSettings) IndexNumericFieldData(org.elasticsearch.index.fielddata.IndexNumericFieldData) Document(org.apache.lucene.document.Document) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) SortedNumericDocValuesField(org.apache.lucene.document.SortedNumericDocValuesField) IndexWriter(org.apache.lucene.index.IndexWriter) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) AtomicNumericFieldData(org.elasticsearch.index.fielddata.AtomicNumericFieldData) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 100 with IndexMetaData

use of org.elasticsearch.cluster.metadata.IndexMetaData in project elasticsearch by elastic.

the class QueryShardContextTests method testFailIfFieldMappingNotFound.

public void testFailIfFieldMappingNotFound() {
    IndexMetaData.Builder indexMetadata = new IndexMetaData.Builder("index");
    indexMetadata.settings(Settings.builder().put("index.version.created", Version.CURRENT).put("index.number_of_shards", 1).put("index.number_of_replicas", 1));
    IndexSettings indexSettings = new IndexSettings(indexMetadata.build(), Settings.EMPTY);
    MapperService mapperService = mock(MapperService.class);
    when(mapperService.getIndexSettings()).thenReturn(indexSettings);
    final long nowInMillis = randomNonNegativeLong();
    QueryShardContext context = new QueryShardContext(0, indexSettings, null, null, mapperService, null, null, xContentRegistry(), null, null, () -> nowInMillis);
    context.setAllowUnmappedFields(false);
    MappedFieldType fieldType = new TextFieldMapper.TextFieldType();
    MappedFieldType result = context.failIfFieldMappingNotFound("name", fieldType);
    assertThat(result, sameInstance(fieldType));
    QueryShardException e = expectThrows(QueryShardException.class, () -> context.failIfFieldMappingNotFound("name", null));
    assertEquals("No field mapping can be found for the field with name [name]", e.getMessage());
    context.setAllowUnmappedFields(true);
    result = context.failIfFieldMappingNotFound("name", fieldType);
    assertThat(result, sameInstance(fieldType));
    result = context.failIfFieldMappingNotFound("name", null);
    assertThat(result, nullValue());
    context.setAllowUnmappedFields(false);
    context.setMapUnmappedFieldAsString(true);
    result = context.failIfFieldMappingNotFound("name", fieldType);
    assertThat(result, sameInstance(fieldType));
    result = context.failIfFieldMappingNotFound("name", null);
    assertThat(result, notNullValue());
    assertThat(result, instanceOf(TextFieldMapper.TextFieldType.class));
    assertThat(result.name(), equalTo("name"));
}
Also used : IndexSettings(org.elasticsearch.index.IndexSettings) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) MapperService(org.elasticsearch.index.mapper.MapperService) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Aggregations

IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)253 IndexMetadata (org.elasticsearch.cluster.metadata.IndexMetadata)194 ClusterState (org.elasticsearch.cluster.ClusterState)124 Settings (org.elasticsearch.common.settings.Settings)104 Index (org.elasticsearch.index.Index)100 Test (org.junit.Test)90 ShardId (org.elasticsearch.index.shard.ShardId)71 ShardRouting (org.elasticsearch.cluster.routing.ShardRouting)68 IOException (java.io.IOException)65 Metadata (org.elasticsearch.cluster.metadata.Metadata)62 IndexSettings (org.elasticsearch.index.IndexSettings)62 RoutingTable (org.elasticsearch.cluster.routing.RoutingTable)60 MetaData (org.elasticsearch.cluster.metadata.MetaData)58 HashSet (java.util.HashSet)56 HashMap (java.util.HashMap)54 IndexShardRoutingTable (org.elasticsearch.cluster.routing.IndexShardRoutingTable)54 Map (java.util.Map)50 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)49 ArrayList (java.util.ArrayList)47 DiscoveryNode (org.elasticsearch.cluster.node.DiscoveryNode)44