Search in sources :

Example 1 with PutMapping

use of io.searchbox.indices.mapping.PutMapping in project graylog2-server by Graylog2.

the class IndicesAdapterES6 method updateIndexMapping.

@Override
public void updateIndexMapping(@Nonnull String indexName, @Nonnull String mappingType, @Nonnull Map<String, Object> mapping) {
    final PutMapping request = new PutMapping.Builder(indexName, mappingType, mapping).build();
    final JestResult jestResult;
    try {
        jestResult = jestClient.execute(request);
    } catch (IOException e) {
        throw new ElasticsearchException("Couldn't update index mapping " + indexName + "/" + mappingType, e);
    }
    if (!jestResult.isSucceeded()) {
        throw new ElasticsearchException(jestResult.getErrorMessage());
    }
}
Also used : PutMapping(io.searchbox.indices.mapping.PutMapping) IOException(java.io.IOException) ElasticsearchException(org.graylog2.indexer.ElasticsearchException) JestResult(io.searchbox.client.JestResult)

Example 2 with PutMapping

use of io.searchbox.indices.mapping.PutMapping in project jmxtrans by jmxtrans.

the class ElasticWriter method createMappingIfNeeded.

private static void createMappingIfNeeded(JestClient jestClient, String indexName, String typeName) throws ElasticWriterException, IOException {
    synchronized (CREATE_MAPPING_LOCK) {
        IndicesExists indicesExists = new IndicesExists.Builder(indexName).build();
        boolean indexExists = jestClient.execute(indicesExists).isSucceeded();
        if (!indexExists) {
            CreateIndex createIndex = new CreateIndex.Builder(indexName).build();
            JestResult result = jestClient.execute(createIndex);
            if (!result.isSucceeded()) {
                throw new ElasticWriterException(String.format("Failed to create index: %s", result.getErrorMessage()));
            } else {
                log.info("Created index {}", indexName);
            }
            URL url = ElasticWriter.class.getResource("/elastic-mapping.json");
            String mapping = Resources.toString(url, Charsets.UTF_8);
            PutMapping putMapping = new PutMapping.Builder(indexName, typeName, mapping).build();
            result = jestClient.execute(putMapping);
            if (!result.isSucceeded()) {
                throw new ElasticWriterException(String.format("Failed to create mapping: %s", result.getErrorMessage()));
            } else {
                log.info("Created mapping for index {}", indexName);
            }
        }
    }
}
Also used : CreateIndex(io.searchbox.indices.CreateIndex) PutMapping(io.searchbox.indices.mapping.PutMapping) JestResult(io.searchbox.client.JestResult) URL(java.net.URL) IndicesExists(io.searchbox.indices.IndicesExists)

Example 3 with PutMapping

use of io.searchbox.indices.mapping.PutMapping in project xwiki-platform by xwiki.

the class DefaultPingSender method sendPing.

@Override
public void sendPing() throws Exception {
    JestClient client = this.jestClientManager.getClient();
    // Step 1: Create index (if already exists then it'll just be ignored)
    client.execute(new CreateIndex.Builder(JestClientManager.INDEX).build());
    // Step 2: Create a mapping so that we can search distribution versions containing hyphens (otherwise they
    // are removed by the default tokenizer/analyzer). If mapping already exists then it'll just be ignored.
    PutMapping putMapping = new PutMapping.Builder(JestClientManager.INDEX, JestClientManager.TYPE, constructJSONMapping()).build();
    client.execute(putMapping);
    // Step 3: Index the data
    Index index = new Index.Builder(constructIndexJSON()).index(JestClientManager.INDEX).type(JestClientManager.TYPE).build();
    JestResult result = client.execute(index);
    if (!result.isSucceeded()) {
        throw new Exception(result.getErrorMessage());
    }
}
Also used : PutMapping(io.searchbox.indices.mapping.PutMapping) CreateIndex(io.searchbox.indices.CreateIndex) Index(io.searchbox.core.Index) JestClient(io.searchbox.client.JestClient) JestResult(io.searchbox.client.JestResult)

Example 4 with PutMapping

use of io.searchbox.indices.mapping.PutMapping in project herd by FINRAOS.

the class IndexFunctionsDaoImpl method createIndex.

/**
 * The create index function will take as arguments the index name, document type, and mapping and will create a new index.
 */
@Override
public final void createIndex(String indexName, String documentType, String mapping, String settings, String alias) {
    LOGGER.info("Creating Elasticsearch index, indexName={}, documentType={}.", indexName, documentType);
    CreateIndex createIndex = new CreateIndex.Builder(indexName).settings(Settings.builder().loadFromSource(settings).build()).build();
    PutMapping putMapping = new PutMapping.Builder(indexName, documentType, mapping).build();
    ModifyAliases modifyAliases = new ModifyAliases.Builder(new AddAliasMapping.Builder(indexName, alias).build()).build();
    JestResult jestResult = jestClientHelper.executeAction(createIndex);
    LOGGER.info("Creating Elasticsearch index, indexName={}, documentType={} successful={}", indexName, documentType, jestResult.isSucceeded());
    jestResult = jestClientHelper.executeAction(putMapping);
    LOGGER.info("Creating Elasticsearch index put mappings, indexName={}, documentType={} successful={}", indexName, documentType, jestResult.isSucceeded());
    jestResult = jestClientHelper.executeAction(modifyAliases);
    LOGGER.info("Creating Elasticsearch index alias, indexName={}, alias={}", indexName, alias, jestResult.isSucceeded());
    // If there are failures log them
    if (!jestResult.isSucceeded()) {
        LOGGER.error("Error in index creation= {}", jestResult.getErrorMessage());
    }
}
Also used : CreateIndex(io.searchbox.indices.CreateIndex) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) PutMapping(io.searchbox.indices.mapping.PutMapping) ModifyAliases(io.searchbox.indices.aliases.ModifyAliases) AddAliasMapping(io.searchbox.indices.aliases.AddAliasMapping) JestResult(io.searchbox.client.JestResult)

Example 5 with PutMapping

use of io.searchbox.indices.mapping.PutMapping in project unipop by unipop-graph.

the class ElasticClient method validateIndex.

public void validateIndex(String indexName) {
    try {
        IndicesExists indicesExistsRequest = new IndicesExists.Builder(indexName).build();
        logger.debug("created indexExistsRequests: {}", indicesExistsRequest);
        JestResult existsResult = client.execute(indicesExistsRequest);
        logger.debug("indexExistsRequests result: {}", existsResult);
        if (!existsResult.isSucceeded()) {
            Settings settings = Settings.builder().put("index.store.type", "mmapfs").build();
            CreateIndex createIndexRequest = new CreateIndex.Builder(indexName).settings(settings).build();
            execute(createIndexRequest);
            // TODO: Make this work. Using the above "keyword" configuration in the meantime.
            PutMapping putMapping = new PutMapping.Builder(indexName, "_default_", STRING_NOT_ANALYZED).build();
            execute(putMapping);
            logger.info("created index with settings: {}, indexName: {}, putMapping: {}", settings, indexName, putMapping);
        }
    } catch (IOException e) {
        logger.error("failed to connect to elastic cluster", e);
    }
}
Also used : CreateIndex(io.searchbox.indices.CreateIndex) PutMapping(io.searchbox.indices.mapping.PutMapping) IOException(java.io.IOException) JestResult(io.searchbox.client.JestResult) Settings(org.elasticsearch.common.settings.Settings) IndicesExists(io.searchbox.indices.IndicesExists)

Aggregations

PutMapping (io.searchbox.indices.mapping.PutMapping)8 JestResult (io.searchbox.client.JestResult)7 CreateIndex (io.searchbox.indices.CreateIndex)4 IndicesExists (io.searchbox.indices.IndicesExists)2 IOException (java.io.IOException)2 JestClient (io.searchbox.client.JestClient)1 Index (io.searchbox.core.Index)1 AddAliasMapping (io.searchbox.indices.aliases.AddAliasMapping)1 ModifyAliases (io.searchbox.indices.aliases.ModifyAliases)1 URL (java.net.URL)1 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)1 Settings (org.elasticsearch.common.settings.Settings)1 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)1 ElasticsearchException (org.graylog2.indexer.ElasticsearchException)1 Test (org.junit.Test)1