Java Code Examples for org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder#addMapping()
The following examples show how to use
org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder#addMapping() .
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: ESClient.java From uavstack with Apache License 2.0 | 6 votes |
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 2
Source File: IndexCreate.java From sfs with Apache License 2.0 | 6 votes |
@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 3
Source File: ElasticsearchClientTransport.java From c2mon with GNU Lesser General Public License v3.0 | 6 votes |
@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 4
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * インデックスを作成する. * @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 5
Source File: InternalEsClient.java From io with Apache License 2.0 | 6 votes |
/** * インデックスを作成する. * @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: BaseMetricTransportClient.java From elasticsearch-helper with Apache License 2.0 | 6 votes |
@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 7
Source File: ESOpt.java From common-project with Apache License 2.0 | 5 votes |
/** * 创建索引+映射 * @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 8
Source File: ElasticClient.java From Stargraph with MIT License | 5 votes |
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 9
Source File: ESConnector.java From Siamese with GNU General Public License v3.0 | 5 votes |
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 10
Source File: ElasticsearchResource.java From newsleak with GNU Affero General Public License v3.0 | 5 votes |
/** * 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 11
Source File: LangDetectBinaryTests.java From elasticsearch-plugin-bundle with GNU Affero General Public License v3.0 | 5 votes |
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 12
Source File: BulkNodeClient.java From elasticsearch-helper with Apache License 2.0 | 5 votes |
@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 13
Source File: HttpBulkNodeClient.java From elasticsearch-helper with Apache License 2.0 | 5 votes |
@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: ElasticSearchService.java From Mahuta with Apache License 2.0 | 4 votes |
@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 15
Source File: ElasticsearchMailDestination.java From elasticsearch-imap with Apache License 2.0 | 4 votes |
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; }