org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest Java Examples
The following examples show how to use
org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest.
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: ElasticsearchUtil.java From adaptive-alerting with Apache License 2.0 | 6 votes |
@Generated public void updateIndexMappings(Set<String> newFieldMappings, String indexName, String docType) { PutMappingRequest request = new PutMappingRequest(indexName); Map<String, Object> type = new HashMap<>(); type.put("type", "keyword"); Map<String, Object> properties = new HashMap<>(); newFieldMappings.forEach(field -> { properties.put(field, type); }); Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("properties", properties); request.source(jsonMap); request.type(docType); try { legacyElasticSearchClient.indices().putMapping(request, RequestOptions.DEFAULT); } catch (IOException e) { log.error("Error updating mappings", e); throw new RuntimeException(e); } }
Example #2
Source File: SearchDemo.java From javabase with Apache License 2.0 | 6 votes |
private static SearchResponse zkfc(String indexName, String zkType, TransportClient client) throws IOException { //返回一个可以执行管理性操作的客户端 //1) cluster(),产生一个允许从集群中执行action或操作的client; //2) indices(),产生一个允许从索引中执行action或操作的client。 //创建zk分词 PutMappingRequest mapping = Requests.putMappingRequest(indexName).type(zkType).source(createIKMapping(zkType).string()); client.admin().indices().putMapping(mapping).actionGet(); Goods goodsOne= new Goods( 1,"iphone7 iphone7plus 钢化膜 玻璃膜 苹果 苹果7/7plus 贴膜 买就送清水","http://m.ule.com/item/detail/1771161"); Goods goodsTwo=new Goods( 2,"苹果 (Apple) iPhone 7 移动联通电信4G手机 土豪金 32G 标配","http://m.ule.com/item/detail/1799356"); Goods goodsThree=new Goods( 3,"苹果 Apple iPhone 7 (A1660) 128G 金色 移动联通电信 全网通 4G手机","http://m.ule.com/item/detail/1781429"); client.prepareIndex(indexName,zkType).setId(1+"").setSource(JSONObject.toJSONString(goodsOne)).execute().actionGet(); client.prepareIndex(indexName,zkType).setId(2+"").setSource(JSONObject.toJSONString(goodsTwo)).execute().actionGet(); client.prepareIndex(indexName,zkType).setId(3+"").setSource(JSONObject.toJSONString(goodsThree)).execute().actionGet(); SearchResponse response = client.prepareSearch(indexName) .setTypes(zkType) .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setQuery( QueryBuilders.matchQuery("title", "苹果") ).execute().actionGet(); return response; }
Example #3
Source File: ElasticSearchUtils.java From ElasticUtils with MIT License | 6 votes |
private static PutMappingResponse internalPutMapping(Client client, String indexName, IElasticSearchMapping mapping) throws IOException { final PutMappingRequest putMappingRequest = new PutMappingRequest(indexName) .type(mapping.getIndexType()) .source(mapping.getMapping().string()); final PutMappingResponse putMappingResponse = client .admin() .indices() .putMapping(putMappingRequest) .actionGet(); if(log.isDebugEnabled()) { log.debug("PutMappingResponse: isAcknowledged {}", putMappingResponse.isAcknowledged()); } return putMappingResponse; }
Example #4
Source File: ElasticSearchUtils.java From ElasticUtils with MIT License | 6 votes |
private static PutMappingResponse internalPutMapping(Client client, String indexName, IElasticSearchMapping mapping) throws IOException { final PutMappingRequest putMappingRequest = new PutMappingRequest(indexName) .type(mapping.getIndexType()) .source(mapping.getMapping().string()); final PutMappingResponse putMappingResponse = client .admin() .indices() .putMapping(putMappingRequest) .actionGet(); if(log.isDebugEnabled()) { log.debug("PutMappingResponse: isAcknowledged {}", putMappingResponse.isAcknowledged()); } return putMappingResponse; }
Example #5
Source File: ElasticSearchUtils.java From ElasticUtils with MIT License | 6 votes |
private static AcknowledgedResponse internalPutMapping(Client client, String indexName, IElasticSearchMapping mapping) throws IOException { String json = Strings.toString(mapping.getMapping()); final PutMappingRequest putMappingRequest = new PutMappingRequest(indexName) .type(mapping.getIndexType()) .source(json, XContentType.JSON); final AcknowledgedResponse putMappingResponse = client .admin() .indices() .putMapping(putMappingRequest) .actionGet(); if(log.isDebugEnabled()) { log.debug("PutMappingResponse: isAcknowledged {}", putMappingResponse.isAcknowledged()); } return putMappingResponse; }
Example #6
Source File: ElasticSearch.java From javabase with Apache License 2.0 | 5 votes |
@Override public void afterPropertiesSet() throws Exception { Map<String, String> map = new HashMap(); //基础名称 map.put("cluster.name", "my-application-A"); Settings.Builder settings = Settings.builder().put(map); try { transportClient = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300)); IndicesAdminClient indicesAdminClient = transportClient.admin().indices(); //查看索引是否存在,不存在就创建索引 if(!checkExistsIndex(indicesAdminClient,INDEX_NAME)){ indicesAdminClient.prepareCreate(INDEX_NAME).setSettings().execute().actionGet(); } //查询mapping是否存在,已存在就不创建了 GetMappingsResponse getMappingsResponse = indicesAdminClient.getMappings(new GetMappingsRequest().indices(INDEX_NAME)).actionGet(); ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> indexToMappings = getMappingsResponse.getMappings(); if(indexToMappings.get(INDEX_NAME).get(TIEABA_CONTENT_TYPE)==null) { //创建zk分词mapping PutMappingRequest mapping = Requests.putMappingRequest(INDEX_NAME).type(TIEABA_CONTENT_TYPE).source(createIKMapping(TIEABA_CONTENT_TYPE, TIEABA_CONTENT_FIELD).string()); mapping.updateAllTypes(true); indicesAdminClient.putMapping(mapping).actionGet(); } } catch (Exception e) { log.error("初始化 elasticsearch cliet error"+e.getLocalizedMessage()); } }
Example #7
Source File: TransportSchemaUpdateAction.java From crate with Apache License 2.0 | 5 votes |
private CompletableFuture<AcknowledgedResponse> updateMapping(Index index, TimeValue timeout, String mappingSource) { FutureActionListener<AcknowledgedResponse, AcknowledgedResponse> putMappingListener = FutureActionListener.newInstance(); PutMappingRequest putMappingRequest = new PutMappingRequest() .indices(new String[0]) .setConcreteIndex(index) .type(Constants.DEFAULT_MAPPING_TYPE) .source(mappingSource, XContentType.JSON) .timeout(timeout) .masterNodeTimeout(timeout); nodeClient.execute(PutMappingAction.INSTANCE, putMappingRequest, putMappingListener); return putMappingListener; }
Example #8
Source File: PutMappingRequestBuilder.java From elasticshell with Apache License 2.0 | 5 votes |
@Override protected XContentBuilder toXContent(PutMappingRequest request, PutMappingResponse response, XContentBuilder builder) throws IOException { builder.startObject() .field(Fields.OK, true) .field(Fields.ACKNOWLEDGED, response.isAcknowledged()); builder.endObject(); return builder; }
Example #9
Source File: BaseClient.java From elasticsearch-helper with Apache License 2.0 | 5 votes |
public void putMapping(String index) { if (client() == null) { return; } if (!mappings().isEmpty()) { for (Map.Entry<String, String> me : mappings().entrySet()) { client().execute(PutMappingAction.INSTANCE, new PutMappingRequest(index).type(me.getKey()).source(me.getValue())).actionGet(); } } }
Example #10
Source File: NodeMappingFactory.java From james-project with Apache License 2.0 | 5 votes |
public static void createMapping(ReactorElasticSearchClient client, IndexName indexName, XContentBuilder mappingsSources) throws IOException { client.indices().putMapping( new PutMappingRequest(indexName.getValue()) .type(NodeMappingFactory.DEFAULT_MAPPING_NAME) .source(mappingsSources), RequestOptions.DEFAULT); }
Example #11
Source File: CrudDemo.java From javabase with Apache License 2.0 | 5 votes |
/** * * @param indicesAdminClient * @param indexName * @param typeName * @throws IOException */ private static void indexMapping(IndicesAdminClient indicesAdminClient, String indexName, String typeName) throws IOException { //type就相当于表的 String typeSource=getIndexTypeSource(typeName,FIELD_NAME); //typetwo PutMappingRequest mapping = Requests.putMappingRequest(indexName).type(typeName).source("typeSource"); PutMappingResponse putMappingResponseTwo = indicesAdminClient.putMapping(mapping).actionGet(); }
Example #12
Source File: ElasticsearchUtil.java From SpringBootLearn with Apache License 2.0 | 5 votes |
/** * 创建索引以及映射mapping,并给索引某些字段指定iK分词,以后向该索引中查询时,就会用ik分词。 * @Author lihaodong * @Description * @Date 20:19 2018/12/21 * @Param [indexName, esType] * @return boolean **/ public static boolean createIndex(String indexName, String esType) { if (!isIndexExist(indexName)) { log.info("Index is not exits!"); } //创建映射 XContentBuilder mapping = null; try { mapping = XContentFactory.jsonBuilder() .startObject() .startObject("properties") // .startObject("m_id").field("type","keyword").endObject() // title:字段名, type:文本类型 analyzer :分词器类型 .startObject("id").field("type", "text").field("analyzer", "standard").endObject() //该字段添加的内容,查询时将会使用ik_smart分词 .startObject("name").field("type", "text").field("analyzer", "standard").endObject() //ik_smart ik_max_word standard .startObject("message").field("type", "text").field("analyzer", "standard").endObject() .startObject("price").field("type", "float").endObject() .startObject("creatDate").field("type", "date").endObject() .endObject() .endObject(); } catch (IOException e) { log.error("执行建立失败:{}",e.getMessage()); } //index:索引名 type:类型名 PutMappingRequest putmap = Requests.putMappingRequest(indexName).type(esType).source(mapping); //创建索引 client.admin().indices().prepareCreate(indexName).execute().actionGet(); //为索引添加映射 PutMappingResponse indexresponse = client.admin().indices().putMapping(putmap).actionGet(); log.info("执行建立成功?" + indexresponse.isAcknowledged()); return indexresponse.isAcknowledged(); }
Example #13
Source File: IndexDemo.java From elasticsearch-full with Apache License 2.0 | 5 votes |
@Test public void testCreateMapping() throws Exception { PutMappingRequest putMappingRequest = new PutMappingRequest(); putMappingRequest.indices("blog"); putMappingRequest.type(""); client.admin().indices().putMapping(putMappingRequest); }
Example #14
Source File: ElasticsearchTemplate.java From summerframework with Apache License 2.0 | 5 votes |
public boolean createMapping(String index, String type, XContentBuilder mapping) throws IOException { log.info("mapping is:{}", mapping.toString()); PutMappingRequest mappingRequest = Requests.putMappingRequest(index).source(mapping).type(type); AcknowledgedResponse putMappingResponse = esClient.admin().indices().putMapping(mappingRequest).actionGet(); return putMappingResponse.isAcknowledged(); }
Example #15
Source File: RestPutMappingAction.java From Elasticsearch with Apache License 2.0 | 5 votes |
@Override public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) { PutMappingRequest putMappingRequest = putMappingRequest(Strings.splitStringByCommaToArray(request.param("index"))); putMappingRequest.type(request.param("type")); putMappingRequest.source(request.content().toUtf8()); putMappingRequest.updateAllTypes(request.paramAsBoolean("update_all_types", false)); putMappingRequest.reindex(request.paramAsBoolean("reindex", false)); putMappingRequest.timeout(request.paramAsTime("timeout", putMappingRequest.timeout())); putMappingRequest.masterNodeTimeout(request.paramAsTime("master_timeout", putMappingRequest.masterNodeTimeout())); putMappingRequest.indicesOptions(IndicesOptions.fromRequest(request, putMappingRequest.indicesOptions())); client.admin().indices().putMapping(putMappingRequest, new AcknowledgedRestListener<PutMappingResponse>(channel)); }
Example #16
Source File: ElasticsearchTemplate.java From summerframework with Apache License 2.0 | 5 votes |
public boolean createMapping(String index, String type, XContentBuilder mapping) throws IOException { log.info("mapping is:{}", mapping.toString()); PutMappingRequest mappingRequest = Requests.putMappingRequest(index).source(mapping).type(type); AcknowledgedResponse putMappingResponse = esClient.admin().indices().putMapping(mappingRequest).actionGet(); return putMappingResponse.isAcknowledged(); }
Example #17
Source File: AbstractClient.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public ActionFuture<PutMappingResponse> putMapping(final PutMappingRequest request) { return execute(PutMappingAction.INSTANCE, request); }
Example #18
Source File: PutIndexTemplateRequest.java From crate with Apache License 2.0 | 4 votes |
/** * A specialized simplified mapping source method, takes the form of simple properties definition: * ("field1", "type=string,store=true"). */ public PutIndexTemplateRequest mapping(String type, Object... source) { mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source)); return this; }
Example #19
Source File: CreateIndexRequest.java From crate with Apache License 2.0 | 4 votes |
/** * A specialized simplified mapping source method, takes the form of simple properties definition: * ("field1", "type=string,store=true"). */ public CreateIndexRequest mapping(String type, Object... source) { mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source)); return this; }
Example #20
Source File: AbstractClient.java From crate with Apache License 2.0 | 4 votes |
@Override public void putMapping(final PutMappingRequest request, final ActionListener<AcknowledgedResponse> listener) { execute(PutMappingAction.INSTANCE, request, listener); }
Example #21
Source File: AbstractClient.java From crate with Apache License 2.0 | 4 votes |
@Override public ActionFuture<AcknowledgedResponse> putMapping(final PutMappingRequest request) { return execute(PutMappingAction.INSTANCE, request); }
Example #22
Source File: PutMappingRequestBuilder.java From elasticshell with Apache License 2.0 | 4 votes |
@Override protected ActionFuture<PutMappingResponse> doExecute(PutMappingRequest request) { return client.admin().indices().putMapping(request); }
Example #23
Source File: PutMappingRequestBuilder.java From elasticshell with Apache License 2.0 | 4 votes |
public PutMappingRequestBuilder(Client client, JsonToString<JsonInput> jsonToString, StringToJson<JsonOutput> stringToJson) { super(client, new PutMappingRequest(), jsonToString, stringToJson); }
Example #24
Source File: RequestUtils.java From ranger with Apache License 2.0 | 4 votes |
public static <Request extends ActionRequest> List<String> getIndexFromRequest(Request request) { List<String> indexs = new ArrayList<>(); if (request instanceof SingleShardRequest) { indexs.add(((SingleShardRequest<?>) request).index()); return indexs; } if (request instanceof ReplicationRequest) { indexs.add(((ReplicationRequest<?>) request).index()); return indexs; } if (request instanceof InstanceShardOperationRequest) { indexs.add(((InstanceShardOperationRequest<?>) request).index()); return indexs; } if (request instanceof CreateIndexRequest) { indexs.add(((CreateIndexRequest) request).index()); return indexs; } if (request instanceof PutMappingRequest) { indexs.add(((PutMappingRequest) request).getConcreteIndex().getName()); return indexs; } if (request instanceof SearchRequest) { return Arrays.asList(((SearchRequest) request).indices()); } if (request instanceof IndicesStatsRequest) { return Arrays.asList(((IndicesStatsRequest) request).indices()); } if (request instanceof OpenIndexRequest) { return Arrays.asList(((OpenIndexRequest) request).indices()); } if (request instanceof DeleteIndexRequest) { return Arrays.asList(((DeleteIndexRequest) request).indices()); } if (request instanceof BulkRequest) { @SuppressWarnings("rawtypes") List<DocWriteRequest<?>> requests = ((BulkRequest) request).requests(); if (CollectionUtils.isNotEmpty(requests)) { for (DocWriteRequest<?> docWriteRequest : requests) { indexs.add(docWriteRequest.index()); } return indexs; } } if (request instanceof MultiGetRequest) { List<Item> items = ((MultiGetRequest) request).getItems(); if (CollectionUtils.isNotEmpty(items)) { for (Item item : items) { indexs.add(item.index()); } return indexs; } } // No matched request type to find specific index , set default value * indexs.add("*"); return indexs; }
Example #25
Source File: AbstractClient.java From Elasticsearch with Apache License 2.0 | 4 votes |
@Override public void putMapping(final PutMappingRequest request, final ActionListener<PutMappingResponse> listener) { execute(PutMappingAction.INSTANCE, request, listener); }
Example #26
Source File: IndexTransaction.java From es-service-parent with Apache License 2.0 | 4 votes |
/** * 设置mapping * * @return */ private void buildMapping() { try { Fileds fileds = FieldXMLParser.getAndCache().get(indexType.getIndexNo()); contentBuilder = XContentFactory.jsonBuilder(); contentBuilder.startObject().startObject(indexType.getTypeName()); // 如果有主键,用主键作为新建索引id,没有主键让es自动生成主键 if (StringUtils.isNotBlank(fileds.getKey())) { String idname = fileds.getKey().toUpperCase(); contentBuilder.startObject("_id").field("path", idname).endObject(); } // 压缩 contentBuilder.startObject("_source").field("compress", true).endObject(); // 开启_all contentBuilder.startObject("_all").field("enabled", true).endObject(); contentBuilder.startObject("properties"); List<Filed> listfiled = fileds.getListfiled(); for (Filed filed : listfiled) { // 处理类型映射 String type_ = getFiledType(filed); if ("-1".equals(type_)) { continue; } else if ("date".equals(type_)) { String format = filed.getFormat(); if (StringUtils.isBlank(format)) { format = "yyyy-MM-dd HH:mm:ss"; } contentBuilder.startObject(filed.getNameToUpperCase()).field("type", type_) .field("format", format) .field("store", filed.isIsstore() == true ? "yes" : "no") .field("index", "not_analyzed") .field("include_in_all", filed.isIsdefaultsearch()).endObject(); continue; } // mapping基本配置 contentBuilder.startObject(filed.getNameToUpperCase()).field("type", type_) .field("store", filed.isIsstore() == true ? "yes" : "no") .field("include_in_all", filed.isIsdefaultsearch()); // // 是否copyto if (filed.isIscopy()) { contentBuilder.field("copy_to", filed.getCopyto()); } // 是否设置有权重 if (filed.getWeight() > 0d) { contentBuilder.field("boost", filed.getWeight()); } // 多值字段的边界分割 if (filed.getPosition_offset_gap() > 0) { contentBuilder.field("position_offset_gap", filed.getPosition_offset_gap()); } // 设置索引分词方式 if (!filed.isIsindex()) { contentBuilder.field("index", "no"); } else { if (!filed.isAnalyzer()) { contentBuilder.field("index", "not_analyzed"); } else { String indexAnalyzer = filed.getIndexAnalyzer(); String searchAnalyzer = filed.getSearchAnalyzer(); if (StringUtils.isBlank(searchAnalyzer)) { searchAnalyzer = indexAnalyzer; } contentBuilder.field("indexAnalyzer", indexAnalyzer).field( "searchAnalyzer", searchAnalyzer); } } contentBuilder.endObject(); } // 建议器 if (fileds.getSuggest() != null) { contentBuilder.startObject(fileds.getSuggest().key) .field("type", fileds.getSuggest().type) .field("index_analyzer", fileds.getSuggest().getIndexAnalyzer()) .field("search_analyzer", fileds.getSuggest().getSearchAnalyzer()) .field("payloads", fileds.getSuggest().isPayloads()).endObject(); } // 构造mapping请求 PutMappingRequest mappingRequest = Requests.putMappingRequest(currentIndexName) .type(indexType.getTypeName()).source(contentBuilder.endObject().endObject()); ESClient.getClient().admin().indices().putMapping(mappingRequest).actionGet(); ESClient.getClient().admin().indices().flush(new FlushRequest(currentIndexName)) .actionGet(); log.debug("create mappings. index:{},type:{},mapping:{}", currentIndexName, indexType.getTypeName(), contentBuilder.string()); } catch (Exception e) { throw new RuntimeException(e); } }
Example #27
Source File: UpdateMappingFieldDemo.java From javabase with Apache License 2.0 | 4 votes |
private static void updateMapping() throws IOException { PutMappingRequest mapping = Requests.putMappingRequest(INDEX_NAME_v2).type(INDEX_TYPE).source(getUpdateItemInfoMapping().string()); client.admin().indices().putMapping(mapping); }
Example #28
Source File: CreateIndexRequest.java From Elasticsearch with Apache License 2.0 | 4 votes |
/** * A specialized simplified mapping source method, takes the form of simple properties definition: * ("field1", "type=string,store=true"). */ public CreateIndexRequest mapping(String type, Object... source) { mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source)); return this; }
Example #29
Source File: PutIndexTemplateRequest.java From Elasticsearch with Apache License 2.0 | 4 votes |
/** * A specialized simplified mapping source method, takes the form of simple properties definition: * ("field1", "type=string,store=true"). */ public PutIndexTemplateRequest mapping(String type, Object... source) { mapping(type, PutMappingRequest.buildFromSimplifiedDef(type, source)); return this; }
Example #30
Source File: Indexer.java From scava with Eclipse Public License 2.0 | 4 votes |
/** * Adds a document map (mapping) to a specific index * * @param index * - index name * @param type * - doucment type * @param mapping * - a JSON document represented as a string */ private static void addMappingToIndex(String indexName, String documentType, String mapping) { PutMappingRequest putMappingRequest = new PutMappingRequest(indexName.toLowerCase()); putMappingRequest.source(mapping, XContentType.JSON).type(documentType.toLowerCase()); PutMappingResponse putMappingResponse; try { putMappingResponse = highLevelClient.indices().putMapping(putMappingRequest, getWriteHeaders()); if (putMappingResponse.isAcknowledged() == true) { logger.info("Mapping for " + documentType + " in " + indexName + " was added successfully"); } } catch (IOException e) { logger.error(e); } }