org.elasticsearch.client.indices.CreateIndexRequest Java Examples

The following examples show how to use org.elasticsearch.client.indices.CreateIndexRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Test.java    From dht-spider with MIT License 6 votes vote down vote up
public static void createIndex() throws Exception{
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("message");
            {
                builder.field("type", "text");
                builder.field("analyzer", "ik_max_word");
                builder.field("search_analyzer", "ik_max_word");

            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    CreateIndexRequest request = new CreateIndexRequest("twitter");
    request.mapping(builder);
    client.indices().create(request, RequestOptions.DEFAULT);

}
 
Example #2
Source File: FactSearchManager.java    From act-platform with ISC License 6 votes vote down vote up
private void createIndex() {
  CreateIndexResponse response;

  try (InputStream payload = FactSearchManager.class.getClassLoader().getResourceAsStream(MAPPINGS_JSON);
       InputStreamReader reader = new InputStreamReader(payload)) {
    CreateIndexRequest request = new CreateIndexRequest(INDEX_NAME)
            .source(CharStreams.toString(reader), XContentType.JSON);
    response = clientFactory.getClient().indices().create(request, RequestOptions.DEFAULT);
  } catch (ElasticsearchException | IOException ex) {
    throw logAndExit(ex, "Could not perform request to create index.");
  }

  if (!response.isAcknowledged()) {
    String msg = String.format("Could not create index '%s'.", INDEX_NAME);
    LOGGER.error(msg);
    throw new IllegalStateException(msg);
  }

  LOGGER.info("Successfully created index '%s'.", INDEX_NAME);
}
 
Example #3
Source File: KibanaImporter.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
public void importJson(String jsonString) throws JsonParseException, JsonMappingException, IOException {
    Objects.requireNonNull(jsonString);

    // delete .kibana index
    try {
        client.delete(new DeleteRequest(kibanaIndexName), RequestOptions.DEFAULT);
    } catch (ElasticsearchException ex) {
        LOG.debug("Tried to delete kibana index " + kibanaIndexName + " but it is not exists", ex);
    }

    ObjectMapper mapper = new ObjectMapper();
    KibanaConfigHolderDto holder = mapper.readValue(jsonString, KibanaConfigHolderDto.class);

    for (KibanaConfigEntryDto dto : holder.getEntries()) {
        processDto(dto);
        LOG.debug("Importing {}", dto);
        CreateIndexResponse response =
                client.indices().create(new CreateIndexRequest(kibanaIndexName), RequestOptions.DEFAULT);
        // client.prepareIndex(kibanaIndexName, dto.getType(), dto.getId())
        // .setSource(dto.getSource()).get();
    }
}
 
Example #4
Source File: IndicesClientCreateMethodsInterceptor.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public void beforeMethod(EnhancedInstance objInst, Method method, Object[] allArguments, Class<?>[] argumentsTypes,
    MethodInterceptResult result) throws Throwable {
    CreateIndexRequest createIndexRequest = (CreateIndexRequest) (allArguments[0]);

    RestClientEnhanceInfo restClientEnhanceInfo = (RestClientEnhanceInfo) (objInst.getSkyWalkingDynamicField());
    if (restClientEnhanceInfo != null) {
        AbstractSpan span = ContextManager.createExitSpan(Constants.CREATE_OPERATOR_NAME, restClientEnhanceInfo.getPeers());
        span.setComponent(ComponentsDefine.REST_HIGH_LEVEL_CLIENT);

        Tags.DB_TYPE.set(span, DB_TYPE);
        Tags.DB_INSTANCE.set(span, createIndexRequest.index());
        if (TRACE_DSL) {
            //Store es mapping parameters
            Tags.DB_STATEMENT.set(span, createIndexRequest.mappings().utf8ToString());
        }
        SpanLayer.asDB(span);
    }
}
 
Example #5
Source File: ElasticsearchAdminHandlerIT.java    From arctic-sea with Apache License 2.0 6 votes vote down vote up
@Test
    public void failOnVersionMismatch()
            throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InterruptedException, IOException {
        Map<String, Object> data = new HashMap<>();
        data.put(MetadataDataMapping.METADATA_VERSION_FIELD.getName(), 123456);
        getEmbeddedClient().indices().create(new CreateIndexRequest(clientSettings.getIndexId()).source(data), RequestOptions.DEFAULT);

//
//        (clientSettings.getIndexId(), MetadataDataMapping.METADATA_TYPE_NAME, MetadataDataMapping.METADATA_ROW_ID)
//                .setSource(data).get();

        Thread.sleep(1500);

        assertThrows(ConfigurationError.class, () -> {
            adminHandler.createSchema();
        });
    }
 
Example #6
Source File: ElasticSearchImpl.java    From core-ng-project with Apache License 2.0 6 votes vote down vote up
@Override
public void putIndex(String index, String source) {
    var watch = new StopWatch();
    try {
        IndicesClient client = client().indices();
        CreateIndexRequest request = new CreateIndexRequest(index).source(new BytesArray(source), XContentType.JSON);
        boolean exists = client.exists(new GetIndexRequest(index), RequestOptions.DEFAULT);
        if (!exists) {
            client.create(request, RequestOptions.DEFAULT);
        } else {
            // only try to update mappings, as for settings it generally requires to close index first then open after update
            logger.info("index already exists, update mapping, index={}", index);
            client.putMapping(new PutMappingRequest(index).source(request.mappings(), XContentType.JSON), RequestOptions.DEFAULT);
        }
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    } finally {
        logger.info("put index, index={}, source={}, elapsed={}", index, source, watch.elapsed());
    }
}
 
Example #7
Source File: GeoQueriesManualTest.java    From tutorials with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
    String jsonObject = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":false},\"region\":{\"type\":\"geo_shape\"},\"location\":{\"type\":\"geo_point\"}}}";

    CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD);
    req.mapping(jsonObject, XContentType.JSON);

    client.indices()
        .create(req, RequestOptions.DEFAULT);
}
 
Example #8
Source File: ElasticExporter.java    From LoggerPlusPlus with GNU Affero General Public License v3.0 5 votes vote down vote up
private void createIndices() throws IOException {
    GetIndexRequest request = new GetIndexRequest(this.indexName);

    boolean exists = httpClient.indices().exists(request, RequestOptions.DEFAULT);

    if(!exists) {
        CreateIndexRequest _request = new CreateIndexRequest(this.indexName);
        httpClient.indices().create(_request, RequestOptions.DEFAULT);
    }
}
 
Example #9
Source File: RestHighLevelClientCase.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void createIndex(RestHighLevelClient client, String indexName) throws IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);

    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("author");
            {
                builder.field("type", "keyword");
            }
            builder.endObject();
            builder.startObject("title");
            {
                builder.field("type", "keyword");
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    request.mapping(builder);

    request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));

    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    if (createIndexResponse.isAcknowledged() == false) {
        String message = "elasticsearch create index fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #10
Source File: CaseController.java    From skywalking with Apache License 2.0 5 votes vote down vote up
private void createIndex(String indexName) throws IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexName);

    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("author");
            {
                builder.field("type", "keyword");
            }
            builder.endObject();
            builder.startObject("title");
            {
                builder.field("type", "keyword");
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    request.mapping(builder);

    request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0));

    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    if (createIndexResponse.isAcknowledged() == false) {
        String message = "elasticsearch create index fail.";
        logger.error(message);
        throw new RuntimeException(message);
    }
}
 
Example #11
Source File: ElasticSearch7Client.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public boolean createIndex(String indexName, Map<String, Object> settings,
                           Map<String, Object> mapping) throws IOException {
    indexName = formatIndexName(indexName);
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    request.settings(settings);
    request.mapping(mapping);
    CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
    log.debug("create {} index finished, isAcknowledged: {}", indexName, response.isAcknowledged());
    return response.isAcknowledged();
}
 
Example #12
Source File: ElasticSearch7Client.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public boolean createIndex(String indexName) throws IOException {
    indexName = formatIndexName(indexName);

    CreateIndexRequest request = new CreateIndexRequest(indexName);
    CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
    log.debug("create {} index finished, isAcknowledged: {}", indexName, response.isAcknowledged());
    return response.isAcknowledged();
}
 
Example #13
Source File: BaseElasticsearchService.java    From spring-boot-demo with MIT License 5 votes vote down vote up
/**
 * create elasticsearch index (asyc)
 *
 * @param index elasticsearch index
 * @author fxbin
 */
protected void createIndexRequest(String index) {
    try {
        CreateIndexRequest request = new CreateIndexRequest(index);
        // Settings for this index
        request.settings(Settings.builder().put("index.number_of_shards", elasticsearchProperties.getIndex().getNumberOfShards()).put("index.number_of_replicas", elasticsearchProperties.getIndex().getNumberOfReplicas()));

        CreateIndexResponse createIndexResponse = client.indices().create(request, COMMON_OPTIONS);

        log.info(" whether all of the nodes have acknowledged the request : {}", createIndexResponse.isAcknowledged());
        log.info(" Indicates whether the requisite number of shard copies were started for each shard in the index before timing out :{}", createIndexResponse.isShardsAcknowledged());
    } catch (IOException e) {
        throw new ElasticsearchException("创建索引 {" + index + "} 失败");
    }
}
 
Example #14
Source File: ElasticsearchAdminHandler.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
private void createMetadataType(int version) throws IOException {
    Map<String, Object> data = new HashMap<>();
    Calendar time = Calendar.getInstance(DateTimeZone.UTC.toTimeZone());
    data.put(MetadataDataMapping.METADATA_CREATION_TIME_FIELD.getName(), time);
    data.put(MetadataDataMapping.METADATA_UPDATE_TIME_FIELD.getName(), time);
    data.put(MetadataDataMapping.METADATA_VERSION_FIELD.getName(), version);
    data.put(MetadataDataMapping.METADATA_UUIDS_FIELD.getName(), settings.getUuid());
    getClient().indices().create(new CreateIndexRequest(settings.getIndexId()).mapping(data),
            RequestOptions.DEFAULT);
    // getClient().prepareIndex(settings.getIndexId(),
    // MetadataDataMapping.METADATA_TYPE_NAME,
    // MetadataDataMapping.METADATA_ROW_ID).setSource(data).get();
    logger.info("Initial metadata is created ceated in {}/{}", settings.getIndexId(),
            MetadataDataMapping.METADATA_TYPE_NAME);
}
 
Example #15
Source File: ElasticsearchAdminHandler.java    From arctic-sea with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void createSchema() throws IOException {
    IndicesClient indices = getClient().indices();

    if (indices.exists(new GetIndexRequest(settings.getIndexId()), RequestOptions.DEFAULT)) {
        logger.info("Index {} already exists", settings.getIndexId());

        // update mapping
        Integer version = getCurrentVersion();
        logger.info("Elasticsearch schema version is {}", version);
        if (version == null) {
            throw new ConfigurationError("Database inconsistency. Metadata version not found in type %s",
                    MetadataDataMapping.METADATA_TYPE_NAME);
        }
        if (version != schemas.getSchemaVersion()) {
            throw new ConfigurationError(
                    "Database schema version inconsistency. Version numbers don't match. "
                            + "Database version number %d <-> Application version number %d",
                    version, schemas.getSchemaVersion());
        }
        addUuidToMetadataIfNeeded(settings.getUuid());
    } else {
        logger.info("Index {} not exists creating a new one now.", settings.getIndexId());
        // create metadata table and index table table
        Map<String, Object> mapping = new HashMap<>();
        mapping.put(MetadataDataMapping.METADATA_TYPE_NAME, schemas.getMetadataSchema());
        mapping.put(settings.getTypeId(), schemas.getSchema());
        CreateIndexResponse response = indices
                .create(new CreateIndexRequest(settings.getIndexId()).mapping(mapping), RequestOptions.DEFAULT);
        logger.debug("Created indices: {}", response);
        // insert metadata values
        createMetadataType(schemas.getSchemaVersion());
    }
}
 
Example #16
Source File: DataServiceImpl.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<CreateIndexResponse> createIndex(String indexName, XContentBuilder mapping) {
    try {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
        createIndexRequest.mapping(mapping);
        CreateIndexResponse createIndexResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
        return Optional.of(createIndexResponse);
    } catch (IOException e) {
        return Optional.empty();
    }
}
 
Example #17
Source File: ElasticRequestUtils.java    From vind with Apache License 2.0 4 votes vote down vote up
public static CreateIndexRequest getCreateIndexRequest(String index) {
    final CreateIndexRequest request = new CreateIndexRequest(index);
    request.settings(ElasticMappingUtils.getDefaultSettings(), XContentType.JSON);
    request.mapping(ElasticMappingUtils.getDefaultMapping(), XContentType.JSON);
    return request;
}
 
Example #18
Source File: ElasticsearchIndexManager.java    From syncope with Apache License 2.0 4 votes vote down vote up
public void createIndex(final String domain, final AnyTypeKind kind)
        throws InterruptedException, ExecutionException, IOException {

    XContentBuilder settings = XContentFactory.jsonBuilder().
            startObject().
            startObject("analysis").
            startObject("analyzer").
            startObject("string_lowercase").
            field("type", "custom").
            field("tokenizer", "standard").
            field("filter").
            startArray().
            value("lowercase").
            endArray().
            endObject().
            endObject().
            endObject().
            startObject("index").
            field("number_of_shards", elasticsearchUtils.getNumberOfShards()).
            field("number_of_replicas", elasticsearchUtils.getNumberOfReplicas()).
            endObject().
            endObject();

    XContentBuilder mapping = XContentFactory.jsonBuilder().
            startObject().
            startArray("dynamic_templates").
            startObject().
            startObject("strings").
            field("match_mapping_type", "string").
            startObject("mapping").
            field("type", "keyword").
            field("analyzer", "string_lowercase").
            endObject().
            endObject().
            endObject().
            endArray().
            endObject();

    CreateIndexResponse response = client.indices().create(
            new CreateIndexRequest(ElasticsearchUtils.getContextDomainName(domain, kind)).
                    settings(settings).
                    mapping(mapping), RequestOptions.DEFAULT);
    LOG.debug("Successfully created {} for {}: {}",
            ElasticsearchUtils.getContextDomainName(domain, kind), kind.name(), response);
}
 
Example #19
Source File: ESClient.java    From dht-spider with MIT License 4 votes vote down vote up
public  void createIndex() throws Exception{
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("name");
            {
                builder.field("type", "text");
                builder.field("analyzer", "ik_max_word");
                builder.field("search_analyzer", "ik_max_word");

            }
            builder.endObject();

            builder.startObject("infoHash");
            {
                builder.field("type", "keyword");

            }
            builder.endObject();

            builder.startObject("length");
            {
                builder.field("type", "long");

            }
            builder.endObject();

            builder.startObject("nameInfo");
            {
                builder.field("type", "text");

            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    CreateIndexRequest request = new CreateIndexRequest("torrent");
    request.mapping(builder);
    client.indices().create(request, RequestOptions.DEFAULT);

}