Search in sources :

Example 76 with IndexSettings

use of org.elasticsearch.index.IndexSettings in project elasticsearch by elastic.

the class AnalysisRegistry method buildMapping.

private <T> Map<String, T> buildMapping(Component component, IndexSettings settings, Map<String, Settings> settingsMap, Map<String, AnalysisModule.AnalysisProvider<T>> providerMap, Map<String, AnalysisModule.AnalysisProvider<T>> defaultInstance) throws IOException {
    Settings defaultSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, settings.getIndexVersionCreated()).build();
    Map<String, T> factories = new HashMap<>();
    for (Map.Entry<String, Settings> entry : settingsMap.entrySet()) {
        String name = entry.getKey();
        Settings currentSettings = entry.getValue();
        String typeName = currentSettings.get("type");
        if (component == Component.ANALYZER) {
            T factory = null;
            if (typeName == null) {
                if (currentSettings.get("tokenizer") != null) {
                    factory = (T) new CustomAnalyzerProvider(settings, name, currentSettings);
                } else {
                    throw new IllegalArgumentException(component + " [" + name + "] must specify either an analyzer type, or a tokenizer");
                }
            } else if (typeName.equals("custom")) {
                factory = (T) new CustomAnalyzerProvider(settings, name, currentSettings);
            }
            if (factory != null) {
                factories.put(name, factory);
                continue;
            }
        } else if (component == Component.NORMALIZER) {
            if (typeName == null || typeName.equals("custom")) {
                T factory = (T) new CustomNormalizerProvider(settings, name, currentSettings);
                factories.put(name, factory);
                continue;
            }
        }
        AnalysisProvider<T> type = getAnalysisProvider(component, providerMap, name, typeName);
        if (type == null) {
            throw new IllegalArgumentException("Unknown " + component + " type [" + typeName + "] for [" + name + "]");
        }
        final T factory = type.get(settings, environment, name, currentSettings);
        factories.put(name, factory);
    }
    // go over the char filters in the bindings and register the ones that are not configured
    for (Map.Entry<String, AnalysisModule.AnalysisProvider<T>> entry : providerMap.entrySet()) {
        String name = entry.getKey();
        AnalysisModule.AnalysisProvider<T> provider = entry.getValue();
        // we don't want to re-register one that already exists
        if (settingsMap.containsKey(name)) {
            continue;
        }
        // check, if it requires settings, then don't register it, we know default has no settings...
        if (provider.requiresAnalysisSettings()) {
            continue;
        }
        AnalysisModule.AnalysisProvider<T> defaultProvider = defaultInstance.get(name);
        final T instance;
        if (defaultProvider == null) {
            instance = provider.get(settings, environment, name, defaultSettings);
        } else {
            instance = defaultProvider.get(settings, environment, name, defaultSettings);
        }
        factories.put(name, instance);
    }
    for (Map.Entry<String, AnalysisModule.AnalysisProvider<T>> entry : defaultInstance.entrySet()) {
        final String name = entry.getKey();
        final AnalysisModule.AnalysisProvider<T> provider = entry.getValue();
        if (factories.containsKey(name) == false) {
            final T instance = provider.get(settings, environment, name, defaultSettings);
            if (factories.containsKey(name) == false) {
                factories.put(name, instance);
            }
        }
    }
    return factories;
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) AnalysisModule(org.elasticsearch.indices.analysis.AnalysisModule) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) AnalysisProvider(org.elasticsearch.indices.analysis.AnalysisModule.AnalysisProvider) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 77 with IndexSettings

use of org.elasticsearch.index.IndexSettings in project elasticsearch by elastic.

the class AnalysisRegistry method getTokenFilterProvider.

/**
     * Returns a registered {@link TokenFilterFactory} provider by {@link IndexSettings}
     *  or a registered {@link TokenFilterFactory} provider by predefined name
     *  or <code>null</code> if the tokenFilter was not registered
     * @param tokenFilter global or defined tokenFilter name
     * @param indexSettings an index settings
     * @return {@link TokenFilterFactory} provider or <code>null</code>
     */
public AnalysisProvider<TokenFilterFactory> getTokenFilterProvider(String tokenFilter, IndexSettings indexSettings) {
    final Map<String, Settings> tokenFilterSettings = indexSettings.getSettings().getGroups("index.analysis.filter");
    if (tokenFilterSettings.containsKey(tokenFilter)) {
        Settings currentSettings = tokenFilterSettings.get(tokenFilter);
        String typeName = currentSettings.get("type");
        /*
             * synonym and synonym_graph are different than everything else since they need access to the tokenizer factories for the index.
             * instead of building the infrastructure for plugins we rather make it a real exception to not pollute the general interface and
             * hide internal data-structures as much as possible.
             */
        if ("synonym".equals(typeName)) {
            return requiresAnalysisSettings((is, env, name, settings) -> new SynonymTokenFilterFactory(is, env, this, name, settings));
        } else if ("synonym_graph".equals(typeName)) {
            return requiresAnalysisSettings((is, env, name, settings) -> new SynonymGraphTokenFilterFactory(is, env, this, name, settings));
        } else {
            return getAnalysisProvider(Component.FILTER, tokenFilters, tokenFilter, typeName);
        }
    } else {
        return getTokenFilterProvider(tokenFilter);
    }
}
Also used : ElasticsearchException(org.elasticsearch.ElasticsearchException) DeprecationLogger(org.elasticsearch.common.logging.DeprecationLogger) Environment(org.elasticsearch.env.Environment) HashMap(java.util.HashMap) Index(org.elasticsearch.index.Index) Settings(org.elasticsearch.common.settings.Settings) PreBuiltTokenizers(org.elasticsearch.indices.analysis.PreBuiltTokenizers) Locale(java.util.Locale) Map(java.util.Map) IndexSettings(org.elasticsearch.index.IndexSettings) PreBuiltCharFilters(org.elasticsearch.indices.analysis.PreBuiltCharFilters) AnalysisModule(org.elasticsearch.indices.analysis.AnalysisModule) AnalysisProvider(org.elasticsearch.indices.analysis.AnalysisModule.AnalysisProvider) Loggers(org.elasticsearch.common.logging.Loggers) PreBuiltTokenFilters(org.elasticsearch.indices.analysis.PreBuiltTokenFilters) Analyzer(org.apache.lucene.analysis.Analyzer) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOUtils(org.apache.lucene.util.IOUtils) Set(java.util.Set) IOException(java.io.IOException) TextFieldMapper(org.elasticsearch.index.mapper.TextFieldMapper) Collectors(java.util.stream.Collectors) Sets(org.elasticsearch.common.util.set.Sets) Logger(org.apache.logging.log4j.Logger) Version(org.elasticsearch.Version) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) PreBuiltAnalyzers(org.elasticsearch.indices.analysis.PreBuiltAnalyzers) Closeable(java.io.Closeable) Collections.unmodifiableMap(java.util.Collections.unmodifiableMap) Collections(java.util.Collections) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings)

Example 78 with IndexSettings

use of org.elasticsearch.index.IndexSettings in project elasticsearch by elastic.

the class IndexFolderUpgraderTests method testUpgradeIndices.

public void testUpgradeIndices() throws IOException {
    final Settings nodeSettings = Settings.builder().put(NodeEnvironment.ADD_NODE_LOCK_ID_TO_CUSTOM_PATH.getKey(), randomBoolean()).build();
    try (NodeEnvironment nodeEnv = newNodeEnvironment(nodeSettings)) {
        Map<IndexSettings, Tuple<Integer, Integer>> indexSettingsMap = new HashMap<>();
        for (int i = 0; i < randomIntBetween(2, 5); i++) {
            final Index index = new Index(randomAsciiOfLength(10), UUIDs.randomBase64UUID());
            Settings settings = Settings.builder().put(nodeSettings).put(IndexMetaData.SETTING_INDEX_UUID, index.getUUID()).put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_2_0_0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 5)).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).build();
            IndexMetaData indexState = IndexMetaData.builder(index.getName()).settings(settings).build();
            Tuple<Integer, Integer> fileCounts = new Tuple<>(randomIntBetween(1, 5), randomIntBetween(1, 5));
            IndexSettings indexSettings = new IndexSettings(indexState, nodeSettings);
            indexSettingsMap.put(indexSettings, fileCounts);
            writeIndex(nodeEnv, indexSettings, fileCounts.v1(), fileCounts.v2());
        }
        IndexFolderUpgrader.upgradeIndicesIfNeeded(nodeSettings, nodeEnv);
        for (Map.Entry<IndexSettings, Tuple<Integer, Integer>> entry : indexSettingsMap.entrySet()) {
            checkIndex(nodeEnv, entry.getKey(), entry.getValue().v1(), entry.getValue().v2());
        }
    }
}
Also used : NodeEnvironment(org.elasticsearch.env.NodeEnvironment) HashMap(java.util.HashMap) IndexSettings(org.elasticsearch.index.IndexSettings) Index(org.elasticsearch.index.Index) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) HashMap(java.util.HashMap) Map(java.util.Map) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) Tuple(org.elasticsearch.common.collect.Tuple)

Example 79 with IndexSettings

use of org.elasticsearch.index.IndexSettings in project elasticsearch by elastic.

the class IndexFolderUpgraderTests method testUpgrade.

public void testUpgrade() throws IOException {
    final Settings nodeSettings = Settings.builder().put(NodeEnvironment.ADD_NODE_LOCK_ID_TO_CUSTOM_PATH.getKey(), randomBoolean()).build();
    try (NodeEnvironment nodeEnv = newNodeEnvironment(nodeSettings)) {
        final Index index = new Index(randomAsciiOfLength(10), UUIDs.randomBase64UUID());
        Settings settings = Settings.builder().put(nodeSettings).put(IndexMetaData.SETTING_INDEX_UUID, index.getUUID()).put(IndexMetaData.SETTING_VERSION_CREATED, Version.V_2_0_0).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, randomIntBetween(1, 5)).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0).build();
        IndexMetaData indexState = IndexMetaData.builder(index.getName()).settings(settings).build();
        int numIdxFiles = randomIntBetween(1, 5);
        int numTranslogFiles = randomIntBetween(1, 5);
        IndexSettings indexSettings = new IndexSettings(indexState, nodeSettings);
        writeIndex(nodeEnv, indexSettings, numIdxFiles, numTranslogFiles);
        IndexFolderUpgrader helper = new IndexFolderUpgrader(settings, nodeEnv);
        helper.upgrade(indexSettings.getIndex().getName());
        checkIndex(nodeEnv, indexSettings, numIdxFiles, numTranslogFiles);
    }
}
Also used : NodeEnvironment(org.elasticsearch.env.NodeEnvironment) IndexSettings(org.elasticsearch.index.IndexSettings) Index(org.elasticsearch.index.Index) Settings(org.elasticsearch.common.settings.Settings) IndexSettings(org.elasticsearch.index.IndexSettings) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData)

Example 80 with IndexSettings

use of org.elasticsearch.index.IndexSettings in project elasticsearch by elastic.

the class IndexFolderUpgraderTests method testUpgradeRealIndex.

/**
     * Run upgrade on a real bwc index
     */
public void testUpgradeRealIndex() throws IOException, URISyntaxException {
    List<Path> indexes = new ArrayList<>();
    try (DirectoryStream<Path> stream = Files.newDirectoryStream(getBwcIndicesPath(), "index-*.zip")) {
        for (Path path : stream) {
            indexes.add(path);
        }
    }
    CollectionUtil.introSort(indexes, (o1, o2) -> o1.getFileName().compareTo(o2.getFileName()));
    final Path path = randomFrom(indexes);
    final String indexName = path.getFileName().toString().replace(".zip", "").toLowerCase(Locale.ROOT);
    try (NodeEnvironment nodeEnvironment = newNodeEnvironment()) {
        Path unzipDir = createTempDir();
        Path unzipDataDir = unzipDir.resolve("data");
        // decompress the index
        try (InputStream stream = Files.newInputStream(path)) {
            TestUtil.unzip(stream, unzipDir);
        }
        // check it is unique
        assertTrue(Files.exists(unzipDataDir));
        Path[] list = FileSystemUtils.files(unzipDataDir);
        if (list.length != 1) {
            throw new IllegalStateException("Backwards index must contain exactly one cluster but was " + list.length);
        }
        // the bwc scripts packs the indices under this path
        Path src = OldIndexUtils.getIndexDir(logger, indexName, path.getFileName().toString(), list[0]);
        assertTrue("[" + path + "] missing index dir: " + src.toString(), Files.exists(src));
        final Path indicesPath = randomFrom(nodeEnvironment.nodePaths()).indicesPath;
        logger.info("--> injecting index [{}] into [{}]", indexName, indicesPath);
        OldIndexUtils.copyIndex(logger, src, src.getFileName().toString(), indicesPath);
        IndexFolderUpgrader.upgradeIndicesIfNeeded(Settings.EMPTY, nodeEnvironment);
        // ensure old index folder is deleted
        Set<String> indexFolders = nodeEnvironment.availableIndexFolders();
        assertEquals(indexFolders.size(), 1);
        // ensure index metadata is moved
        IndexMetaData indexMetaData = IndexMetaData.FORMAT.loadLatestState(logger, NamedXContentRegistry.EMPTY, nodeEnvironment.resolveIndexFolder(indexFolders.iterator().next()));
        assertNotNull(indexMetaData);
        Index index = indexMetaData.getIndex();
        assertEquals(index.getName(), indexName);
        Set<ShardId> shardIds = nodeEnvironment.findAllShardIds(index);
        // ensure all shards are moved
        assertEquals(shardIds.size(), indexMetaData.getNumberOfShards());
        for (ShardId shardId : shardIds) {
            final ShardPath shardPath = ShardPath.loadShardPath(logger, nodeEnvironment, shardId, new IndexSettings(indexMetaData, Settings.EMPTY));
            final Path translog = shardPath.resolveTranslog();
            final Path idx = shardPath.resolveIndex();
            final Path state = shardPath.getShardStatePath().resolve(MetaDataStateFormat.STATE_DIR_NAME);
            assertTrue(shardPath.exists());
            assertTrue(Files.exists(translog));
            assertTrue(Files.exists(idx));
            assertTrue(Files.exists(state));
        }
    }
}
Also used : Path(java.nio.file.Path) ShardPath(org.elasticsearch.index.shard.ShardPath) NodeEnvironment(org.elasticsearch.env.NodeEnvironment) InputStream(java.io.InputStream) IndexSettings(org.elasticsearch.index.IndexSettings) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) IndexMetaData(org.elasticsearch.cluster.metadata.IndexMetaData) ShardId(org.elasticsearch.index.shard.ShardId) ShardPath(org.elasticsearch.index.shard.ShardPath)

Aggregations

IndexSettings (org.elasticsearch.index.IndexSettings)83 Settings (org.elasticsearch.common.settings.Settings)53 IndexMetaData (org.elasticsearch.cluster.metadata.IndexMetaData)28 Index (org.elasticsearch.index.Index)25 ShardId (org.elasticsearch.index.shard.ShardId)13 Environment (org.elasticsearch.env.Environment)12 IOException (java.io.IOException)11 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)11 Path (java.nio.file.Path)9 ShardPath (org.elasticsearch.index.shard.ShardPath)9 AnalysisModule (org.elasticsearch.indices.analysis.AnalysisModule)9 Directory (org.apache.lucene.store.Directory)8 NodeEnvironment (org.elasticsearch.env.NodeEnvironment)7 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 HashMap (java.util.HashMap)5 Version (org.elasticsearch.Version)5 IndexService (org.elasticsearch.index.IndexService)5 Store (org.elasticsearch.index.store.Store)5 StringReader (java.io.StringReader)4 ArrayList (java.util.ArrayList)4