org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder. 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: AbstractESTest.java    From elasticsearch-migration with Apache License 2.0 6 votes vote down vote up
@SneakyThrows
protected void createIndex(final String index, final String definition) {
    final CreateIndexRequestBuilder createIndexRequestBuilder = new CreateIndexRequestBuilder(client, CreateIndexAction.INSTANCE)
            .setIndex(index)
            .setSource(definition, XContentType.JSON);

    assertThat(createIndexRequestBuilder.get().isAcknowledged(), is(true));
    client.admin().indices().prepareFlush(index).setWaitIfOngoing(true).setForce(true).execute().get();
}
 
Example #2
Source File: BaseMetricTransportClient.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
@Override
public BaseMetricTransportClient newIndex(String index, Settings settings, Map<String, String> mappings) {
    if (client == null) {
        logger.warn("no client for create index");
        return this;
    }
    if (index == null) {
        logger.warn("no index name given to create index");
        return this;
    }
    CreateIndexRequestBuilder createIndexRequestBuilder =
            new CreateIndexRequestBuilder(client(), CreateIndexAction.INSTANCE).setIndex(index);
    if (settings != null) {
        logger.info("settings = {}", settings.getAsStructuredMap());
        createIndexRequestBuilder.setSettings(settings);
    }
    if (mappings != null) {
        for (String type : mappings.keySet()) {
            logger.info("found mapping for {}", type);
            createIndexRequestBuilder.addMapping(type, mappings.get(type));
        }
    }
    createIndexRequestBuilder.execute().actionGet();
    logger.info("index {} created", index);
    return this;
}
 
Example #3
Source File: SetupIndexServiceImpl.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
private void createGivenIndex(final ElasticSearchIndexConfig config,
		final String indexName, final boolean createAlias) {
	final Client client = searchClientService.getClient();
	CreateIndexRequestBuilder createIndexRequestBuilder;
	try {
		createIndexRequestBuilder = client
				.admin()
				.indices()
				.prepareCreate(indexName)
				.setSettings(
						new IndexSchemaBuilder().getSettingForIndex(config));
	} catch (final IOException e) {
		throw new RuntimeException(
				"Error occurred while generating settings for index", e);
	}
	// update mapping on server
	createIndexRequestBuilder.execute().actionGet();
	if (createAlias) {
		createAlias(config.getIndexAliasName(), indexName);
	}
	logger.debug("Index {} created! ", indexName);
}
 
Example #4
Source File: SetupIndexServiceImpl.java    From elasticsearch-tutorial with MIT License 6 votes vote down vote up
private void createGivenIndex(ElasticSearchIndexConfig config, String indexName)
{
    Client client = searchClientService.getClient();
    
    CreateIndexRequestBuilder createIndexRequestBuilder;
    try
    {
        createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName ).setSettings(new IndexSchemaBuilder().getSettingForIndex(config));
    } catch (IOException e)
    {
        throw new RuntimeException("Error occurred while generating settings for index",e);
    }
    //update mapping on server
    createIndexRequestBuilder.execute().actionGet();
    
    createAlias(config.getIndexAliasName(), indexName);
    
    logger.debug("Index {} created! ",indexName);
}
 
Example #5
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * インデックスを作成する.
 * @param index インデックス名
 * @param mappings マッピング情報
 * @return 非同期応答
 */
public ActionFuture<CreateIndexResponse> createIndex(String index, Map<String, JSONObject> mappings) {
    this.fireEvent(Event.creatingIndex, index);
    CreateIndexRequestBuilder cirb =
            new CreateIndexRequestBuilder(esTransportClient.admin().indices()).setIndex(index);

    // cjkアナライザ設定
    ImmutableSettings.Builder indexSettings = ImmutableSettings.settingsBuilder();
    indexSettings.put("analysis.analyzer.default.type", "cjk");
    cirb.setSettings(indexSettings);

    if (mappings != null) {
        for (Map.Entry<String, JSONObject> ent : mappings.entrySet()) {
            cirb = cirb.addMapping(ent.getKey(), ent.getValue().toString());
        }
    }
    return cirb.execute();
}
 
Example #6
Source File: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * インデックスを作成する.
 * @param index インデックス名
 * @param mappings マッピング情報
 * @return 非同期応答
 */
public ActionFuture<CreateIndexResponse> createIndex(String index, Map<String, JSONObject> mappings) {
    this.fireEvent(Event.creatingIndex, index);
    CreateIndexRequestBuilder cirb =
            new CreateIndexRequestBuilder(esTransportClient.admin().indices(), 
            		CreateIndexAction.INSTANCE, index);

    // cjkアナライザ設定
    Settings.Builder indexSettings = Settings.settingsBuilder();
    indexSettings.put("analysis.analyzer.default.type", "cjk");
    cirb.setSettings(indexSettings);

    if (mappings != null) {
        for (Map.Entry<String, JSONObject> ent : mappings.entrySet()) {
            cirb = cirb.addMapping(ent.getKey(), ent.getValue().toString());
        }
    }
    return cirb.execute();
}
 
Example #7
Source File: PluginClient.java    From openshift-elasticsearch-plugin with Apache License 2.0 6 votes vote down vote up
public CreateIndexResponse copyIndex(final String index, final String target, Settings settings, String... types)
        throws InterruptedException, ExecutionException, IOException {
    return execute(new Callable<CreateIndexResponse>() {

        @Override
        public CreateIndexResponse call() throws Exception {
            LOGGER.trace("Copying {} index to {} for types {}", index, target, types);
            GetIndexResponse response = getIndices(index);
            CreateIndexRequestBuilder builder = client.admin().indices().prepareCreate(target);
            if(settings != null) {
                builder.setSettings(settings);
            }
            for (String type : types) {
                builder.addMapping(type, response.mappings().get(index).get(type).getSourceAsMap());
            }
            return builder.get();
        }
        
    });
}
 
Example #8
Source File: DefaultElasticSearchAdminService.java    From vertx-elasticsearch-service with Apache License 2.0 6 votes vote down vote up
@Override
public void createIndex(String index, JsonObject source, CreateIndexOptions options, Handler<AsyncResult<Void>> resultHandler) {

    final CreateIndexRequestBuilder builder = CreateIndexAction.INSTANCE.newRequestBuilder(service.getClient())
            .setIndex(index)
            .setSource(source.encode(), XContentType.JSON);

    builder.execute(new ActionListener<CreateIndexResponse>() {
        @Override
        public void onResponse(CreateIndexResponse createIndexResponse) {
            resultHandler.handle(Future.succeededFuture());
        }

        @Override
        public void onFailure(Exception e) {
            resultHandler.handle(Future.failedFuture(e));
        }
    });
}
 
Example #9
Source File: ElasticsearchClientTransport.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean createIndex(IndexMetadata indexMetadata, String mapping) {
  CreateIndexRequestBuilder builder = client.admin().indices().prepareCreate(indexMetadata.getName());
  builder.setSettings(Settings.builder()
      .put("number_of_shards", properties.getShardsPerIndex())
      .put("number_of_replicas", properties.getReplicasPerShard())
      .build());

  if (mapping != null) {
    builder.addMapping(TYPE, mapping, XContentType.JSON);
  }

  log.debug("Creating new index with name {}", indexMetadata.getName());

  try {
    CreateIndexResponse response = builder.get();
    return response.isAcknowledged();
  } catch (ResourceAlreadyExistsException e) {
    log.debug("Index already exists.", e);
  }

  return false;
}
 
Example #10
Source File: IndexCreate.java    From sfs with Apache License 2.0 6 votes vote down vote up
@Override
public Observable<Boolean> call(String index) {
    Elasticsearch elasticsearch = vertxContext.verticle().elasticsearch();
    CreateIndexRequestBuilder request = elasticsearch.get().admin().indices().prepareCreate(index);
    for (Map.Entry<String, String> entry : mappings.entrySet()) {
        String type = entry.getKey();
        String mapping = entry.getValue();
        request = request.addMapping(type, mapping);
    }
    if (settings != null) {
        request.setSettings(settings);
    }

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(format("Request %s", Jsonify.toString(request)));
    }

    return elasticsearch.execute(vertxContext, request, elasticsearch.getDefaultAdminTimeout())
            .map(Optional::get)
            .doOnNext(createIndexResponse -> {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug(format("Response %s", Jsonify.toString(createIndexResponse)));
                }
            })
            .map(AcknowledgedResponse::isAcknowledged);
}
 
Example #11
Source File: ESClient.java    From uavstack with Apache License 2.0 6 votes vote down vote up
public boolean creatIndex(String index, String type, Map<String, String> set,
        Map<String, Map<String, Object>> mapping) throws IOException {

    CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(index);
    if (type != null && mapping != null) {
        createIndexRequestBuilder.addMapping(type, createMapping(type, mapping));
    }
    if (set != null) {
        createIndexRequestBuilder.setSettings(createSetting(set));
    }
    CreateIndexResponse resp = createIndexRequestBuilder.execute().actionGet();

    if (resp.isAcknowledged()) {
        return true;
    }

    return false;
}
 
Example #12
Source File: ESConnector.java    From Siamese with GNU General Public License v3.0 5 votes vote down vote up
public boolean createIndex(String indexName, String typeName, String settingsStr, String mappingStr) {

		CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(indexName);
		Settings settings = Settings.builder()
                .loadFromSource(settingsStr)
                .build();
		createIndexRequestBuilder.setSettings(settings);
		createIndexRequestBuilder.addMapping(typeName, mappingStr);
		CreateIndexResponse response = createIndexRequestBuilder.execute().actionGet();

		return response.isAcknowledged();
	}
 
Example #13
Source File: HttpBulkNodeClient.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Override
public HttpBulkNodeClient newIndex(String index, Settings settings, Map<String, String> mappings) {
    if (closed) {
        throw new ElasticsearchException("client is closed");
    }
    if (client == null) {
        logger.warn("no client for create index");
        return this;
    }
    if (index == null) {
        logger.warn("no index name given to create index");
        return this;
    }
    CreateIndexRequestBuilder createIndexRequestBuilder =
            new CreateIndexRequestBuilder(client(), CreateIndexAction.INSTANCE).setIndex(index);
    if (settings != null) {
        logger.info("settings = {}", settings.getAsStructuredMap());
        createIndexRequestBuilder.setSettings(settings);
    }
    if (mappings != null) {
        for (String type : mappings.keySet()) {
            logger.info("found mapping for {}", type);
            createIndexRequestBuilder.addMapping(type, mappings.get(type));
        }
    }
    createIndexRequestBuilder.execute().actionGet();
    logger.info("index {} created", index);
    return this;
}
 
Example #14
Source File: BulkNodeClient.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@Override
public BulkNodeClient newIndex(String index, Settings settings, Map<String, String> mappings) {
    if (closed) {
        throw new ElasticsearchException("client is closed");
    }
    if (client == null) {
        logger.warn("no client for create index");
        return this;
    }
    if (index == null) {
        logger.warn("no index name given to create index");
        return this;
    }
    CreateIndexRequestBuilder createIndexRequestBuilder =
            new CreateIndexRequestBuilder(client(), CreateIndexAction.INSTANCE).setIndex(index);
    if (settings != null) {
        logger.info("settings = {}", settings.getAsStructuredMap());
        createIndexRequestBuilder.setSettings(settings);
    }
    if (mappings != null) {
        for (String type : mappings.keySet()) {
            logger.info("found mapping for {}", type);
            createIndexRequestBuilder.addMapping(type, mappings.get(type));
        }
    }
    createIndexRequestBuilder.execute().actionGet();
    logger.info("index {} created", index);
    return this;
}
 
Example #15
Source File: LangDetectBinaryTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 5 votes vote down vote up
public void testLangDetectBinary() throws Exception {
    try {
        CreateIndexRequestBuilder createIndexRequestBuilder =
                new CreateIndexRequestBuilder(client(), CreateIndexAction.INSTANCE).setIndex("test");
        createIndexRequestBuilder.addMapping("someType", jsonBuilder()
                .startObject()
                .startObject("properties")
                .startObject("content")
                .field("type", "binary")
                .startObject("fields")
                .startObject("language")
                .field("type", "langdetect")
                .field("binary", true)
                .endObject()
                .endObject()
                .endObject()
                .endObject()
                .endObject());
        createIndexRequestBuilder.execute().actionGet();
        IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client(), IndexAction.INSTANCE)
                .setIndex("test").setType("someType").setId("1")
                //\"God Save the Queen\" (alternatively \"God Save the King\"
                .setSource("content", "IkdvZCBTYXZlIHRoZSBRdWVlbiIgKGFsdGVybmF0aXZlbHkgIkdvZCBTYXZlIHRoZSBLaW5nIg==");
        indexRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
                .execute().actionGet();
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE)
                .setIndices("test")
                .setQuery(QueryBuilders.termQuery("content.language", "en"))
                .addStoredField("content.language");
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
        assertEquals(1L, searchResponse.getHits().getTotalHits());
        assertEquals("en", searchResponse.getHits().getAt(0).field("content.language").getValue());
    } finally {
        DeleteIndexRequestBuilder deleteIndexRequestBuilder =
                new DeleteIndexRequestBuilder(client(), DeleteIndexAction.INSTANCE, "test");
        deleteIndexRequestBuilder.execute().actionGet();
    }
}
 
Example #16
Source File: ESOpt.java    From common-project with Apache License 2.0 5 votes vote down vote up
/**
 * 创建索引+映射
 * @param indexName
 * @param type
 * @param c         模型class
 * @return
 * @throws Exception
 */
public static boolean createIndexAndMapping(String indexName, String type, Class c) throws IOException {
    Map<String, String> fieldType = ClassUtil.getClassFieldTypeMapping(c);
    CreateIndexRequestBuilder cib = client.admin().indices().prepareCreate(indexName);
    XContentBuilder mapping = XContentFactory.jsonBuilder()
            .startObject()
            .startObject("properties");
    fieldType.forEach((k, v) -> {
        if (v.equals("int")) {
            v = "integer";
        }
        if (v.equals("double")) {
            v = "float";
        }
        if (v.equals("String")) {
            v = "text";
        }

        //设置之定义字段
        try {
            mapping.startObject(k).field("type", v.toLowerCase()).endObject();
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
    mapping.endObject().endObject();
    cib.addMapping(type, mapping);
    try {
        CreateIndexResponse createIndexResponse = cib.execute().actionGet();
        logger.info("----------添加映射成功----------");
        return createIndexResponse.isAcknowledged();
    } catch (ResourceAlreadyExistsException existsException) {
        logger.error("index name : " + indexName + " already exists");
    }
    return false;
}
 
Example #17
Source File: ElasticsearchClusterRunner.java    From elasticsearch-cluster-runner with Apache License 2.0 5 votes vote down vote up
public CreateIndexResponse createIndex(final String index, final BuilderCallback<CreateIndexRequestBuilder> builder) {
    final CreateIndexResponse actionGet = builder.apply(client().admin().indices().prepareCreate(index)).execute().actionGet();
    if (!actionGet.isAcknowledged()) {
        onFailure("Failed to create " + index + ".", actionGet);
    }
    return actionGet;
}
 
Example #18
Source File: TransportWriterVariant.java    From incubator-gobblin with Apache License 2.0 5 votes vote down vote up
@Override
public TestClient getTestClient(Config config)
    throws IOException {
  final ElasticsearchTransportClientWriter transportClientWriter = new ElasticsearchTransportClientWriter(config);
  final TransportClient transportClient = transportClientWriter.getTransportClient();
  return new TestClient() {
    @Override
    public GetResponse get(GetRequest getRequest)
        throws IOException {
      try {
        return transportClient.get(getRequest).get();
      } catch (Exception e) {
        throw new IOException(e);
      }
    }

    @Override
    public void recreateIndex(String indexName)
        throws IOException {
      DeleteIndexRequestBuilder dirBuilder = transportClient.admin().indices().prepareDelete(indexName);
      try {
        DeleteIndexResponse diResponse = dirBuilder.execute().actionGet();
      } catch (IndexNotFoundException ie) {
        System.out.println("Index not found... that's ok");
      }

      CreateIndexRequestBuilder cirBuilder = transportClient.admin().indices().prepareCreate(indexName);
      CreateIndexResponse ciResponse = cirBuilder.execute().actionGet();
      Assert.assertTrue(ciResponse.isAcknowledged(), "Create index succeeeded");
    }

    @Override
    public void close()
        throws IOException {
      transportClientWriter.close();
    }
  };
}
 
Example #19
Source File: ElasticClient.java    From Stargraph with MIT License 5 votes vote down vote up
CreateIndexRequestBuilder prepareCreate() {
    logger.info(marker, "Creating {}", kbId);
    Config mappingCfg = getModelCfg().getConfig("elastic.mapping");
    // Search for matching mapping definition, fallback to the dynamic _default_.
    String targetType = mappingCfg.hasPath(kbId.getModel()) ? kbId.getModel() : "_default_";
    Config mapping = mappingCfg.withOnlyPath(targetType);
    CreateIndexRequestBuilder builder = client.admin().indices().prepareCreate(getIndexName());
    return builder.addMapping(targetType, mapping.root().unwrapped());
}
 
Example #20
Source File: ElasticsearchClient.java    From hawkular-apm with Apache License 2.0 5 votes vote down vote up
/**
 * Check if index is created. if not it will created it
 *
 * @param index The index
 * @return returns true if it just created the index. False if the index already existed
 */
private boolean createIndex(String index, Map<String, Object> defaultSettings) {
    IndicesExistsResponse res = client.admin().indices().prepareExists(index).execute().actionGet();
    boolean created = false;
    if (!res.isExists()) {
        CreateIndexRequestBuilder req = client.admin().indices().prepareCreate(index);
        req.setSettings(defaultSettings);
        created = req.execute().actionGet().isAcknowledged();
        if (!created) {
            throw new RuntimeException("Could not create index [" + index + "]");
        }
    }

    return created;
}
 
Example #21
Source File: ElasticSearchUtils.java    From ElasticUtils with MIT License 5 votes vote down vote up
private static CreateIndexResponse internalCreateIndex(Client client, String indexName) throws IOException {
    final CreateIndexRequestBuilder createIndexRequestBuilder = client
            .admin() // Get the Admin interface...
            .indices() // Get the Indices interface...
            .prepareCreate(indexName); // We want to create a new index ....

    final CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet();

    if(log.isDebugEnabled()) {
        log.debug("CreatedIndexResponse: isAcknowledged {}", indexResponse.isAcknowledged());
    }

    return indexResponse;
}
 
Example #22
Source File: ElasticsearchResource.java    From newsleak with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a new elasticsearch index and adds a mapping for the document type.
 * Previously existing indexes will be removed.
 *
 * @throws Exception
 *             the exception
 */
private void createIndex() throws Exception {

	boolean exists = client.admin().indices().prepareExists(mIndex).execute().actionGet().isExists();

	// remove preexisting index
	if (exists) {
		logger.log(Level.INFO, "Preexisting index " + mIndex + " will be removed.");
		DeleteIndexResponse deleteResponse = client.admin().indices().delete(new DeleteIndexRequest(mIndex))
				.actionGet();
		if (deleteResponse.isAcknowledged()) {
			logger.log(Level.INFO, "Preexisting index " + mIndex + " successfully removed.");
			exists = false;
		}
	}

	// create schema mapping from file
	logger.log(Level.INFO, "Index " + mIndex + " will be created.");
	String docMapping = new String(Files.readAllBytes(Paths.get(documentMappingFile)));

	XContentBuilder builder = XContentFactory.jsonBuilder();
	XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(docMapping.getBytes());
	parser.close();
	builder.copyCurrentStructure(parser);

	CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(mIndex);
	createIndexRequestBuilder.addMapping(DOCUMENT_TYPE, builder);
	createIndexRequestBuilder.execute().actionGet();

}
 
Example #23
Source File: ElasticSearchUtils.java    From ElasticUtils with MIT License 5 votes vote down vote up
private static CreateIndexResponse internalCreateIndex(Client client, String indexName) throws IOException {
    final CreateIndexRequestBuilder createIndexRequestBuilder = client
            .admin() // Get the Admin interface...
            .indices() // Get the Indices interface...
            .prepareCreate(indexName); // We want to create a new index ....

    final CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet();

    if(log.isDebugEnabled()) {
        log.debug("CreatedIndexResponse: isAcknowledged {}", indexResponse.isAcknowledged());
    }

    return indexResponse;
}
 
Example #24
Source File: ElasticSearchUtils.java    From ElasticUtils with MIT License 5 votes vote down vote up
private static CreateIndexResponse internalCreateIndex(Client client, String indexName) throws IOException {
    final CreateIndexRequestBuilder createIndexRequestBuilder = client
            .admin() // Get the Admin interface...
            .indices() // Get the Indices interface...
            .prepareCreate(indexName); // We want to create a new index ....

    final CreateIndexResponse indexResponse = createIndexRequestBuilder.execute().actionGet();

    if(log.isDebugEnabled()) {
        log.debug("CreatedIndexResponse: isAcknowledged {}", indexResponse.isAcknowledged());
    }

    return indexResponse;
}
 
Example #25
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public CreateIndexRequestBuilder prepareCreate(String index) {
    return new CreateIndexRequestBuilder(this, CreateIndexAction.INSTANCE, index);
}
 
Example #26
Source File: LangDetectChineseTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 4 votes vote down vote up
public void testChineseLanguageCode() throws Exception {
    try {
        XContentBuilder builder = jsonBuilder()
                .startObject()
                .startObject("properties")
                .startObject("content")
                .field("type", "text")
                .startObject("fields")
                .startObject("language")
                .field("type", "langdetect")
                .array("languages", "zh-cn", "en", "de")
                .endObject()
                .endObject()
                .endObject()
                .endObject()
                .endObject();
        CreateIndexRequestBuilder createIndexRequestBuilder =
                new CreateIndexRequestBuilder(client(), CreateIndexAction.INSTANCE);
        createIndexRequestBuilder.setIndex("test").addMapping("someType", builder).execute().actionGet();
        String source = "位于美国首都华盛顿都会圈的希望中文学校5日晚举办活动庆祝建立20周年。" +
                "从中国大陆留学生为子女学中文而自发建立的学习班,到学生规模在全美名列前茅的中文学校," +
                "这个平台的发展也折射出美国的中文教育热度逐步提升。\n" +
                "希望中文学校是大华盛顿地区最大中文学校,现有7个校区逾4000名学生," +
                "规模在美国东部数一数二。" +
                "不过,见证了希望中文学校20年发展的人们起初根本无法想象这个小小的中文教育平台能发展到今日之规模。";
        IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client(), IndexAction.INSTANCE)
                .setIndex("test").setType("someType").setId("1")
                .setSource("content", source);
        indexRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
                .execute().actionGet();
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE)
                .setIndices("test")
                .setQuery(QueryBuilders.termQuery("content.language", "zh-cn"))
                .addStoredField("content.language");
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
        assertEquals(1L, searchResponse.getHits().getTotalHits());
        assertEquals("zh-cn", searchResponse.getHits().getAt(0).field("content.language").getValue());
    } finally {
        DeleteIndexRequestBuilder deleteIndexRequestBuilder =
                new DeleteIndexRequestBuilder(client(), DeleteIndexAction.INSTANCE, "test");
        deleteIndexRequestBuilder.execute().actionGet();
    }
}
 
Example #27
Source File: LangDetectGermanTests.java    From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 4 votes vote down vote up
public void testGermanLanguageCode() throws Exception {
    try {
        XContentBuilder builder = jsonBuilder()
                .startObject()
                .startObject("properties")
                .startObject("content")
                .field("type", "text")
                .startObject("fields")
                .startObject("language")
                .field("type", "langdetect")
                .array("languages", "zh-cn", "en", "de")
                .endObject()
                .endObject()
                .endObject()
                .endObject()
                .endObject();
        CreateIndexRequestBuilder createIndexRequestBuilder =
                new CreateIndexRequestBuilder(client(), CreateIndexAction.INSTANCE);
        createIndexRequestBuilder.setIndex("test").addMapping("someType", builder).execute().actionGet();
        String source = "Einigkeit und Recht und Freiheit\n" +
                "für das deutsche Vaterland!\n" +
                "Danach lasst uns alle streben\n" +
                "brüderlich mit Herz und Hand!";
        IndexRequestBuilder indexRequestBuilder = new IndexRequestBuilder(client(), IndexAction.INSTANCE)
                .setIndex("test").setType("someType").setId("1")
                .setSource("content", source);
        indexRequestBuilder.setRefreshPolicy(WriteRequest.RefreshPolicy.IMMEDIATE)
                .execute().actionGet();
        SearchRequestBuilder searchRequestBuilder = new SearchRequestBuilder(client(), SearchAction.INSTANCE)
                .setIndices("test")
                .setQuery(QueryBuilders.termQuery("content.language", "de"))
                .addStoredField("content.language");
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
        assertEquals(1L, searchResponse.getHits().getTotalHits());
        assertEquals("de", searchResponse.getHits().getAt(0).field("content.language").getValue());
    } finally {
        DeleteIndexRequestBuilder deleteIndexRequestBuilder =
                new DeleteIndexRequestBuilder(client(), DeleteIndexAction.INSTANCE, "test");
        deleteIndexRequestBuilder.execute().actionGet();
    }
}
 
Example #28
Source File: ElasticsearchMailDestination.java    From elasticsearch-imap with Apache License 2.0 4 votes vote down vote up
private synchronized void createIndexIfNotExists() throws IOException {
    if (isError()) {
        if (logger.isTraceEnabled()) {
            logger.trace("error, not creating index");
        }
        return;
    }

    if(initialized) {
        return;
    }
    
    
    IMAPImporter.waitForYellowCluster(client);
    
    // create index if it doesn't already exist
    if (!client.admin().indices().prepareExists(index).execute().actionGet().isExists()) {

        final CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate(index);
        if (settings != null) {
            logger.debug("index settings are provided, will apply them {}", settings);
            createIndexRequestBuilder.setSettings(settings);
        } else {
            logger.debug("no settings given for index '{}'",index);
        }
        
        
        if (mapping != null) {
            logger.warn("mapping for type '{}' is provided, will apply {}", type, mapping);
            createIndexRequestBuilder.addMapping(type, mapping);
        } else {
            logger.debug("no mapping given for type '{}', will apply default mapping",type);
            createIndexRequestBuilder.addMapping(type, getDefaultTypeMapping());
        }
        
        final CreateIndexResponse res = createIndexRequestBuilder.get();
        
        if (!res.isAcknowledged()) {
            throw new IOException("Could not create index " + index);
        }
        
        IMAPImporter.waitForYellowCluster(client);
        
        logger.info("Index {} created", index);
        
    } else {
        logger.debug("Index {} already exists", index);
    }
    
    initialized = true;
}
 
Example #29
Source File: ElasticSearchService.java    From Mahuta with Apache License 2.0 4 votes vote down vote up
@Override
public void createIndex(String indexName, InputStream configuration) {
    log.debug("Create index in ElasticSearch [indexName: {}, configuration present: {}]", indexName,
            configuration != null);

    // Validation
    ValidatorUtils.rejectIfEmpty("indexName", indexName);

    try {
        // Format index
        indexName = indexName.toLowerCase();

        // Check existence
        boolean exists = indexExists(indexName);

        if (!exists) {
            CreateIndexRequestBuilder request = client.admin()
                    .indices()
                    .prepareCreate(indexName);
            
            if (configuration != null) {
                request.setSource(IOUtils.toString(configuration, StandardCharsets.UTF_8), XContentType.JSON);
            } else {
                request.addMapping(
                        DEFAULT_TYPE, HASH_INDEX_KEY, "type=keyword", 
                        CONTENT_TYPE_INDEX_KEY, "type=keyword", 
                        CONTENT_INDEX_KEY, "type=binary", 
                        PINNED_KEY, "type=boolean");
            }
            
            request.get();

            log.debug("Index [indexName: {}] created in ElasticSearch", indexName);

        } else {
            log.debug("Index [indexName: {}] already exists in ElasticSearch", indexName);
        }

    } catch (IOException ex) {
        log.error("Error whist reading configuration InputStream", ex);
        throw new TechnicalException("Error whist reading configuration InputStream", ex);
    }
}
 
Example #30
Source File: AbstractClient.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public CreateIndexRequestBuilder prepareCreate(String index) {
    return new CreateIndexRequestBuilder(this, CreateIndexAction.INSTANCE, index);
}