org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.mapping.get.GetMappingsRequest. 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: ElasticsearchSchema.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();

    try {
        GetMappingsResponse response = client.admin().indices().getMappings(
                new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c: mapping) {
            builder.put(c.key, new ElasticsearchTable(client, index, c.key));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return builder.build();
}
 
Example #2
Source File: ElasticsearchSchema.java    From dk-fitting with Apache License 2.0 6 votes vote down vote up
@Override
protected Map<String, Table> getTableMap() {
    final ImmutableMap.Builder<String, Table> builder = ImmutableMap.builder();

    try {
        GetMappingsResponse response = client.admin().indices().getMappings(
                new GetMappingsRequest().indices(index)).get();
        ImmutableOpenMap<String, MappingMetaData> mapping = response.getMappings().get(index);
        for (ObjectObjectCursor<String, MappingMetaData> c: mapping) {
            builder.put(c.key, new ElasticsearchTable(client, index, c.key));
        }
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return builder.build();
}
 
Example #3
Source File: QueryDescParser.java    From elasticsearch-sql with MIT License 6 votes vote down vote up
@Override
public void parse(ElasticDslContext dslContext) {
    if(dslContext.getSqlContext().descOperation()!=null){
        dslContext.getParseResult().setSqlOperation(SqlOperation.DESC);
        GetFieldMappingsRequest getFieldMappingsRequest = new GetFieldMappingsRequest();
        GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
        ElasticsearchParser.TableRefContext tableRefContext=dslContext.getSqlContext().descOperation().tableRef();
        String index = tableRefContext.indexName.getText();
        if(dslContext.getSqlContext().descOperation().identity()!=null){
            String field = dslContext.getSqlContext().descOperation().identity().getText();
            getFieldMappingsRequest.indices(index);
            getFieldMappingsRequest.fields(field);
            dslContext.getParseResult().setFieldMappingsRequest(getFieldMappingsRequest);
        }else{
            getMappingsRequest.indices(index);
            dslContext.getParseResult().setMappingsRequest(getMappingsRequest);
        }

    }
}
 
Example #4
Source File: ElasticsearchUtil.java    From adaptive-alerting with Apache License 2.0 6 votes vote down vote up
@Generated
public Set<String> removeFieldsHavingExistingMapping(Set<String> fields, String indexName, String docType) {
    GetMappingsRequest request = new GetMappingsRequest();
    request.indices(indexName);

    try {
        GetMappingsResponse mappingsResponse = legacyElasticSearchClient.indices().getMapping(request, RequestOptions.DEFAULT);
        Map<String, String> mapProperties = ((Map<String, String>) mappingsResponse.getMappings()
                .get(indexName)
                .get(docType)
                .sourceAsMap().get("properties"));

        Set<String> mappedFields = new HashSet<>(mapProperties.keySet());
        fields.removeAll(mappedFields);
        return fields;
    } catch (IOException e) {
        log.error("Error finding mappings", e);
        throw new RuntimeException(e);
    }
}
 
Example #5
Source File: TestElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
private boolean doesMappingExist(final String index, final String mappingName) {
	GetMappingsRequest request = new GetMappingsRequest()
			.indices(index);
	try {
		GetMappingsResponse response = elasticSearchClient.admin()
				.indices()
				.getMappings(request)
				.get();

		return response.getMappings()
				.get(index)
				.containsKey(mappingName);
	} catch (InterruptedException | ExecutionException e) {
		throw new RuntimeException(e);
	}
}
 
Example #6
Source File: TestElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
private boolean doesMappingExist(final String index, final String mappingName) {
    GetMappingsRequest request = new GetMappingsRequest()
            .indices(index);
    try {
        GetMappingsResponse response = elasticSearchClient.admin()
                .indices()
                .getMappings(request)
                .get();

        return response.getMappings()
                .get(index)
                .containsKey(mappingName);
    } catch (InterruptedException | ExecutionException e) {
        throw new RuntimeException(e);
    }
}
 
Example #7
Source File: IngestIndexCreationTest.java    From elasticsearch-helper with Apache License 2.0 6 votes vote down vote up
@Test
public void testIngestCreation() throws Exception {
    Settings settingsForIndex = Settings.settingsBuilder()
            .put("index.number_of_shards", 1)
            .build();
    Map<String,String> mappings = new HashMap<>();
    mappings.put("typename","{\"properties\":{\"message\":{\"type\":\"string\"}}}");
    final IngestTransportClient ingest = ClientBuilder.builder()
            .put(getSettings())
            .setMetric(new LongAdderIngestMetric())
            .toIngestTransportClient();
    try {
        ingest.newIndex("test", settingsForIndex, mappings);
        GetMappingsRequest getMappingsRequest = new GetMappingsRequest().indices("test");
        GetMappingsResponse getMappingsResponse =
                ingest.client().execute(GetMappingsAction.INSTANCE, getMappingsRequest).actionGet();
        MappingMetaData md = getMappingsResponse.getMappings().get("test").get("typename");
        assertEquals("{properties={message={type=string}}}", md.getSourceAsMap().toString());
    } finally {
        ingest.shutdown();
    }
}
 
Example #8
Source File: ElasticSearch.java    From javabase with Apache License 2.0 5 votes vote down vote up
@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 #9
Source File: ElasticSearchEngine.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> getDynamicFieldNames() throws SearchEngineException {
	List<String> fieldNames = new LinkedList<>();

	try {
		GetMappingsRequest req =
				new GetMappingsRequestBuilder(client, GetMappingsAction.INSTANCE, configuration.getIndexName())
						.setTypes(configuration.getDocType())
						.request();
		GetMappingsResponse response = client.admin().indices().getMappings(req).actionGet();
		MappingMetaData metaData = response.getMappings()
				.get(configuration.getIndexName())
				.get(configuration.getDocType());
		Map<String, Object> sourceMap = metaData.getSourceAsMap();
		Object annotationField = ((Map)sourceMap.get("properties")).get(configuration.getAnnotationField());
		Map<String, Object> annotationProperties = (Map<String, Object>)((Map)annotationField).get("properties");

		if (annotationProperties != null) {
			for (String field : annotationProperties.keySet()) {
				if (field.matches(DYNAMIC_LABEL_FIELD_REGEX)) {
					fieldNames.add(field);
				}
			}
		}
	} catch (IOException e) {
		LOGGER.error("Caught IOException retrieving field source: {}", e.getMessage());
		throw new SearchEngineException(e);
	}

	return fieldNames;
}
 
Example #10
Source File: RestHighLevelClientExt.java    From canal with Apache License 2.0 5 votes vote down vote up
public static GetMappingsResponse getMapping(RestHighLevelClient restHighLevelClient,
                                             GetMappingsRequest getMappingsRequest,
                                             RequestOptions options) throws IOException {
    return restHighLevelClient.performRequestAndParseEntity(getMappingsRequest,
        RequestConvertersExt::getMappings,
        options,
        GetMappingsResponse::fromXContent,
        emptySet());

}
 
Example #11
Source File: DkesService.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public JSONObject getIndexAndTypeName(String es_url, String esCluster_name) {
    JSONObject result = new JSONObject();
    ArrayList<JSONObject> indices = new ArrayList<>();
    if (es_url.contains(":") && StringUtils.split(es_url, ":").length == 2) {
        String[] esU = StringUtils.split(es_url, ":");
        String esIp = esU[0];
        int esPort = Integer.valueOf(esU[1]);
        Settings settings = Settings.builder().put("cluster.name", esCluster_name)
                .put("client.transport.sniff", true)  // 开启嗅探,自动搜寻新加入集群IP
                .build();
        TransportClient transportClient = null;
        try {
            transportClient = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(esIp), esPort));
            ActionFuture<GetMappingsResponse> mappings = transportClient.admin().indices().getMappings(new GetMappingsRequest());
            Iterator<ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>>> iterator = mappings.actionGet().getMappings().iterator();
            while (iterator.hasNext()) {
                ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> next = iterator.next();
                String key = next.key;
                ImmutableOpenMap<String, MappingMetaData> value = next.value;
                Iterator<String> stringIterator = value.keysIt();
                while (stringIterator.hasNext()) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("index_name", key);
                    jsonObject.put("index_type", stringIterator.next());
                    indices.add(jsonObject);
                }
            }
            result.put("succeed", true);
            result.put("indices", indices);
        } catch (UnknownHostException e) {
            e.printStackTrace();
        } finally {
            if (transportClient != null) {
                transportClient.close();
            }
        }
    }else {
        result.put("succeed", false);
        result.put("msg", "连接es的地址输入格式有误!");
        return result;
    }

    /*if (es_url.contains(":") && StringUtils.split(es_url, ":").length == 2) {
        String[] esU = StringUtils.split(es_url, ":");
        String esIp = esU[0];
        ArrayList<JSONObject> indices = new ArrayList<>();
        JSONObject body = restTemplate.getForEntity("http://" + esIp + ":" + esHttpPort + "/_mappings", JSONObject.class).getBody();
        if (!body.isEmpty()) {
            for (String indexName : body.keySet()) {
                JSONObject o = body.getJSONObject(indexName);
                JSONObject mappings = o.getJSONObject("mappings");

                for (String esType : mappings.keySet()) {
                    JSONObject jsonObject = new JSONObject();
                    jsonObject.put("index_name", indexName);
                    jsonObject.put("index_type", esType);
                    indices.add(jsonObject);
                }
            }
            result.put("succeed", true);
            result.put("indices", indices);
        } else {
            result.put("succeed", false);
            result.put("errmsg", "未发现集群中的index!");
        }
    } else {
        result.put("succeed", false);
        result.put("msg", "连接es的地址输入格式有误!");
        return result;
    }*/
    return result;
}
 
Example #12
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 4 votes vote down vote up
public GetMappingsRequest getMappingsRequest() {
    return mappingsRequest;
}
 
Example #13
Source File: ElasticSqlParseResult.java    From elasticsearch-sql with MIT License 4 votes vote down vote up
public void setMappingsRequest(GetMappingsRequest mappingsRequest) {
    this.mappingsRequest = mappingsRequest;
}
 
Example #14
Source File: RestGetMappingAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    final String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    final String[] types = request.paramAsStringArrayOrEmptyIfAll("type");
    GetMappingsRequest getMappingsRequest = new GetMappingsRequest();
    getMappingsRequest.indices(indices).types(types);
    getMappingsRequest.indicesOptions(IndicesOptions.fromRequest(request, getMappingsRequest.indicesOptions()));
    getMappingsRequest.local(request.paramAsBoolean("local", getMappingsRequest.local()));
    client.admin().indices().getMappings(getMappingsRequest, new RestBuilderListener<GetMappingsResponse>(channel) {
        @Override
        public RestResponse buildResponse(GetMappingsResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappingsByIndex = response.getMappings();
            if (mappingsByIndex.isEmpty()) {
                if (indices.length != 0 && types.length != 0) {
                    return new BytesRestResponse(OK, builder.endObject());
                } else if (indices.length != 0) {
                    return new BytesRestResponse(channel, new IndexNotFoundException(indices[0]));
                } else if (types.length != 0) {
                    return new BytesRestResponse(channel, new TypeMissingException(new Index("_all"), types[0]));
                } else {
                    return new BytesRestResponse(OK, builder.endObject());
                }
            }

            for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexEntry : mappingsByIndex) {
                if (indexEntry.value.isEmpty()) {
                    continue;
                }
                builder.startObject(indexEntry.key, XContentBuilder.FieldCaseConversion.NONE);
                builder.startObject(Fields.MAPPINGS);
                for (ObjectObjectCursor<String, MappingMetaData> typeEntry : indexEntry.value) {
                    builder.field(typeEntry.key);
                    Map<String, Object> mapping = typeEntry.value.sourceAsMap();
                    if (mapping.containsKey("_meta")) {
                        ((Map<String, Object>)mapping.get("_meta")).put(MappingMetaData.MAPPING_VERSION, typeEntry.value.mappingVersion());
                    } else {
                        HashMap<String, Object> mappingMeta = new HashMap<>();
                        mappingMeta.put(MappingMetaData.MAPPING_VERSION, typeEntry.value.mappingVersion());
                        mapping.put("_meta", mappingMeta);
                    }
                    builder.map(mapping);
                }
                builder.endObject();
                builder.endObject();
            }

            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}
 
Example #15
Source File: RequestConvertersExt.java    From canal with Apache License 2.0 3 votes vote down vote up
/**
 * 修改 getMappings 去掉request参数
 *
 * @param getMappingsRequest
 * @return
 * @throws IOException
 */
static Request getMappings(GetMappingsRequest getMappingsRequest) throws IOException {
    String[] indices = getMappingsRequest.indices() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.indices();
    String[] types = getMappingsRequest.types() == null ? Strings.EMPTY_ARRAY : getMappingsRequest.types();

    return new Request(HttpGet.METHOD_NAME, RequestConverters.endpoint(indices, "_mapping", types));
}