org.elasticsearch.cluster.metadata.MappingMetaData Java Examples

The following examples show how to use org.elasticsearch.cluster.metadata.MappingMetaData. 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 Project: search-spring-boot-starter   Author: niuzhiweimr   File: ModifyIndexFactory.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * <b>重建索引处理</b>
 * <ul>
 * <li>将变更后的数据结构创建一个临时索引</li>
 * <li>将当前索引中的数据迁移到临时索引</li>
 * <li>重建当前索引(删除+重建索引)</li>
 * <li>将临时索引中的数据迁移到当前索引</li>
 * <li>删除临时索引</li>
 * </ul>
 *
 * @param index            索引名称
 * @param type             类型
 * @param updateProperties 更新属性处理
 * @param sortName         排序字段
 * @param order            排序方式
 * @return
 * @throws IOException
 * @throws ExecutionException
 * @throws InterruptedException
 */
public boolean reindex(String index, String type, UpdateProperties updateProperties, String sortName, SortOrder order)
        throws IOException, InterruptedException, ExecutionException {
    String tmp_index = index + "_tmp";
    MappingMetaData metaData = IndexUtils.loadIndexMeta(client, index, type);
    Map<String, Object> data = updateProperties.execute(metaData.getSourceAsMap());
    if (!IndexUtils.createIndex(client, tmp_index, type, data)) {
        throw new IllegalArgumentException("创建临时索引失败");
    }
    //将数据拷贝到临时索引
    copy_data(index, tmp_index, type, sortName, order);
    //删除主索引
    IndexUtils.deleteIndex(client, index);
    //重建主索引
    if (!IndexUtils.createIndex(client, index, type, data)) {
        throw new IllegalArgumentException("重建主索引失败");
    }
    // 从临时索引中拷贝到主索引中
    copy_data(tmp_index, index, type, sortName, order);
    // 删除临时索引
    IndexUtils.deleteIndex(client, tmp_index);
    return true;
}
 
Example #2
Source Project: search-spring-boot-starter   Author: niuzhiweimr   File: IndexAdmin.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 装载索引数据结构
 *
 * @param index
 * @param type
 * @return
 */
private String loadIndexStruct(String index, String type) {
    ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
    ImmutableOpenMap<String, IndexMetaData> immutableOpenMap = response.getState().getMetaData().getIndices();
    if (immutableOpenMap != null) {
        IndexMetaData metaData = immutableOpenMap.get(index);
        if (metaData != null) {
            ImmutableOpenMap<String, MappingMetaData> mappings = metaData.getMappings();
            if (mappings != null) {
                MappingMetaData mappingMetaData = mappings.get(type);
                if (mappingMetaData != null) {
                    CompressedXContent content = mappingMetaData.source();
                    if (content != null) {
                        return content.toString();
                    }
                }
            }
        }
    }
    LOGGER.error("获取ES数据结构失败 index:" + index + "|type:" + type);
    return null;
}
 
Example #3
Source Project: dk-fitting   Author: dkhadoop   File: ElasticsearchSchema.java    License: 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 #4
Source Project: dk-fitting   Author: dkhadoop   File: ElasticsearchSchema.java    License: 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 #5
Source Project: Elasticsearch   Author: baidu   File: PartitionInfos.java    License: Apache License 2.0 6 votes vote down vote up
@Nullable
private static Map<String, Object> buildValuesMap(PartitionName partitionName, MappingMetaData mappingMetaData) throws Exception{
    int i = 0;
    Map<String, Object> valuesMap = new HashMap<>();
    Iterable<Tuple<ColumnIdent, DataType>> partitionColumnInfoIterable = PartitionedByMappingExtractor.extractPartitionedByColumns(mappingMetaData.sourceAsMap());
    for (Tuple<ColumnIdent, DataType> columnInfo : partitionColumnInfoIterable) {
        String columnName = columnInfo.v1().sqlFqn();
        // produce string type values as string, not bytesref
        Object value = BytesRefs.toString(partitionName.values().get(i));
        if (!columnInfo.v2().equals(DataTypes.STRING)) {
            value = columnInfo.v2().value(value);
        }
        valuesMap.put(columnName, value);
        i++;
    }
    return valuesMap;
}
 
Example #6
Source Project: Elasticsearch   Author: baidu   File: GetMappingsResponse.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void readFrom(StreamInput in) throws IOException {
    super.readFrom(in);
    int size = in.readVInt();
    ImmutableOpenMap.Builder<String, ImmutableOpenMap<String, MappingMetaData>> indexMapBuilder = ImmutableOpenMap.builder();
    for (int i = 0; i < size; i++) {
        String key = in.readString();
        int valueSize = in.readVInt();
        ImmutableOpenMap.Builder<String, MappingMetaData> typeMapBuilder = ImmutableOpenMap.builder();
        for (int j = 0; j < valueSize; j++) {
            typeMapBuilder.put(in.readString(), MappingMetaData.PROTO.readFrom(in));
        }
        indexMapBuilder.put(key, typeMapBuilder.build());
    }
    mappings = indexMapBuilder.build();
}
 
Example #7
Source Project: Elasticsearch   Author: baidu   File: GetIndexResponse.java    License: Apache License 2.0 6 votes vote down vote up
GetIndexResponse(String[] indices, ImmutableOpenMap<String, List<IndexWarmersMetaData.Entry>> warmers,
        ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings,
        ImmutableOpenMap<String, List<AliasMetaData>> aliases, ImmutableOpenMap<String, Settings> settings) {
    this.indices = indices;
    if (warmers != null) {
        this.warmers = warmers;
    }
    if (mappings != null) {
        this.mappings = mappings;
    }
    if (aliases != null) {
        this.aliases = aliases;
    }
    if (settings != null) {
        this.settings = settings;
    }
}
 
Example #8
Source Project: Elasticsearch   Author: baidu   File: TransportDeleteAction.java    License: Apache License 2.0 6 votes vote down vote up
public static void resolveAndValidateRouting(final MetaData metaData, String concreteIndex, DeleteRequest request) {
    request.routing(metaData.resolveIndexRouting(request.routing(), request.index()));
    if (metaData.hasIndex(concreteIndex)) {
        // check if routing is required, if so, throw error if routing wasn't specified
        MappingMetaData mappingMd = metaData.index(concreteIndex).mappingOrDefault(request.type());
        if (mappingMd != null && mappingMd.routing().required()) {
            if (request.routing() == null) {
                if (request.versionType() != VersionType.INTERNAL) {
                    // TODO: implement this feature
                    throw new IllegalArgumentException("routing value is required for deleting documents of type [" + request.type()
                        + "] while using version_type [" + request.versionType() + "]");
                }
                throw new RoutingMissingException(concreteIndex, request.type(), request.id());
            }
        }
    }
}
 
Example #9
Source Project: Elasticsearch   Author: baidu   File: TransportIndexAction.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected Tuple<IndexResponse, IndexRequest> shardOperationOnPrimary(MetaData metaData, IndexRequest request) throws Throwable {

    // validate, if routing is required, that we got routing
    IndexMetaData indexMetaData = metaData.index(request.shardId().getIndex());
    MappingMetaData mappingMd = indexMetaData.mappingOrDefault(request.type());
    if (mappingMd != null && mappingMd.routing().required()) {
        if (request.routing() == null) {
            throw new RoutingMissingException(request.shardId().getIndex(), request.type(), request.id());
        }
    }

    IndexService indexService = indicesService.indexServiceSafe(request.shardId().getIndex());
    IndexShard indexShard = indexService.shardSafe(request.shardId().id());
    indexShard.checkDiskSpace(fsService);
    final WriteResult<IndexResponse> result = executeIndexRequestOnPrimary(null, request, indexShard, mappingUpdatedAction);
    final IndexResponse response = result.response;
    final Translog.Location location = result.location;
    processAfterWrite(request.refresh(), indexShard, location);
    return new Tuple<>(response, request);
}
 
Example #10
Source Project: Elasticsearch   Author: baidu   File: TransportShardBulkAction.java    License: Apache License 2.0 6 votes vote down vote up
private WriteResult<IndexResponse> shardIndexOperation(BulkShardRequest request, IndexRequest indexRequest, MetaData metaData,
                                        IndexShard indexShard, boolean processed) throws Throwable {
    indexShard.checkDiskSpace(fsService);
    // validate, if routing is required, that we got routing
    MappingMetaData mappingMd = metaData.index(request.index()).mappingOrDefault(indexRequest.type());
    if (mappingMd != null && mappingMd.routing().required()) {
        if (indexRequest.routing() == null) {
            throw new RoutingMissingException(request.index(), indexRequest.type(), indexRequest.id());
        }
    }

    if (!processed) {
        indexRequest.process(metaData, mappingMd, allowIdGeneration, request.index());
    }

    return TransportIndexAction.executeIndexRequestOnPrimary(request, indexRequest, indexShard, mappingUpdatedAction);
}
 
Example #11
Source Project: openshift-elasticsearch-plugin   Author: fabric8io   File: ElasticsearchIntegrationTest.java    License: Apache License 2.0 6 votes vote down vote up
protected void dumpIndices() throws Exception {
    ThreadContext threadContext = esNode1.client().threadPool().getThreadContext();
    try (StoredContext cxt = threadContext.stashContext()) {
        threadContext.putHeader(ConfigConstants.SG_CONF_REQUEST_HEADER, "true");
        ClusterStateResponse response = esNode1.client().admin().cluster().prepareState().get();
        Iterator<ObjectObjectCursor<String, IndexMetaData>> iterator = response.getState().getMetaData().indices().iterator();
        while (iterator.hasNext()) {
            ObjectObjectCursor<String, IndexMetaData> c = (ObjectObjectCursor<String, IndexMetaData>) iterator.next();
            IndexMetaData meta = c.value;
            ImmutableOpenMap<String, MappingMetaData> mappings = meta.getMappings();
            Iterator<String> it = mappings.keysIt();
            while (it.hasNext()) {
                String key = it.next();
                System.out.println(String.format("%s %s %s", c.key, key,  mappings.get(key).type()));
            }
        }
    }
}
 
Example #12
Source Project: vertexium   Author: visallo   File: Elasticsearch7SearchIndex.java    License: Apache License 2.0 6 votes vote down vote up
private void loadExistingMappingIntoIndexInfo(Graph graph, IndexInfo indexInfo, String indexName) {
    indexRefreshTracker.refresh(client, indexName);
    GetMappingsResponse mapping = client.admin().indices().prepareGetMappings(indexName).get();
    for (ObjectCursor<String> mappingIndexName : mapping.getMappings().keys()) {
        ImmutableOpenMap<String, MappingMetaData> typeMappings = mapping.getMappings().get(mappingIndexName.value);
        for (ObjectCursor<String> typeName : typeMappings.keys()) {
            MappingMetaData typeMapping = typeMappings.get(typeName.value);
            Map<String, Map<String, String>> properties = getPropertiesFromTypeMapping(typeMapping);
            if (properties == null) {
                continue;
            }

            for (Map.Entry<String, Map<String, String>> propertyEntry : properties.entrySet()) {
                String rawPropertyName = propertyEntry.getKey().replace(FIELDNAME_DOT_REPLACEMENT, ".");
                loadExistingPropertyMappingIntoIndexInfo(graph, indexInfo, rawPropertyName);
            }
        }
    }
}
 
Example #13
Source Project: vertexium   Author: visallo   File: Elasticsearch5SearchIndex.java    License: Apache License 2.0 6 votes vote down vote up
private void loadExistingMappingIntoIndexInfo(Graph graph, IndexInfo indexInfo, String indexName) {
    indexRefreshTracker.refresh(client, indexName);
    GetMappingsResponse mapping = client.admin().indices().prepareGetMappings(indexName).get();
    for (ObjectCursor<String> mappingIndexName : mapping.getMappings().keys()) {
        ImmutableOpenMap<String, MappingMetaData> typeMappings = mapping.getMappings().get(mappingIndexName.value);
        for (ObjectCursor<String> typeName : typeMappings.keys()) {
            MappingMetaData typeMapping = typeMappings.get(typeName.value);
            Map<String, Map<String, String>> properties = getPropertiesFromTypeMapping(typeMapping);
            if (properties == null) {
                continue;
            }

            for (Map.Entry<String, Map<String, String>> propertyEntry : properties.entrySet()) {
                String rawPropertyName = propertyEntry.getKey().replace(FIELDNAME_DOT_REPLACEMENT, ".");
                loadExistingPropertyMappingIntoIndexInfo(graph, indexInfo, rawPropertyName);
            }
        }
    }
}
 
Example #14
Source Project: crate   Author: crate   File: SQLExecutor.java    License: Apache License 2.0 6 votes vote down vote up
private static IndexMetaData.Builder getIndexMetaData(String indexName,
                                                      Settings settings,
                                                      @Nullable Map<String, Object> mapping,
                                                      Version smallestNodeVersion) throws IOException {
    Settings.Builder builder = Settings.builder()
        .put(settings)
        .put(SETTING_VERSION_CREATED, smallestNodeVersion)
        .put(SETTING_CREATION_DATE, Instant.now().toEpochMilli())
        .put(SETTING_INDEX_UUID, UUIDs.randomBase64UUID());

    Settings indexSettings = builder.build();
    IndexMetaData.Builder metaBuilder = IndexMetaData.builder(indexName)
        .settings(indexSettings);
    if (mapping != null) {
        metaBuilder.putMapping(new MappingMetaData(
            Constants.DEFAULT_MAPPING_TYPE,
            mapping));
    }

    return metaBuilder;
}
 
Example #15
Source Project: ingestion   Author: Stratio   File: ElasticSearchSerializerWithMappingTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test(expected = NullPointerException.class)
public void differentTimestampEventsShouldCreateDifferentIndecesWithTheExpectedMapping() throws IOException {
	long timestamp = System.currentTimeMillis();
	Event event1 = createExampleEvent(timestamp);
	String indexName1 = getIndexName(INDEX_PREFIX,  new TimeStampedEvent(event1).getTimestamp());
	Event event2 = createExampleEvent(timestamp + DAY_IN_MILLIS);
	String indexName2 = getIndexName(INDEX_PREFIX,  new TimeStampedEvent(event2).getTimestamp());

	serializer.createIndexRequest(elasticSearchClient, INDEX_PREFIX, INDEX_TYPE, event1);
	serializer.createIndexRequest(elasticSearchClient, INDEX_PREFIX, INDEX_TYPE, event2);
	ImmutableOpenMap<String, IndexMetaData> indices = getIndices();
	ImmutableOpenMap<String, MappingMetaData> mappingsIndex1 = indices.get(indexName1).getMappings();
	ImmutableOpenMap<String, MappingMetaData> mappingsIndex2 = indices.get(indexName2).getMappings();
	String mappingIndex1 = mappingsIndex1.get(INDEX_TYPE).source().string();
	String mappingIndex2 = mappingsIndex2.get(INDEX_TYPE).source().string();

	Assert.assertTrue("The first index must exists and its mapping must be the same as the expected", mappingIndex1.equals(expectedESMapping));
	Assert.assertTrue("The second index must exists and its mapping must be the same as the expected", mappingIndex2.equals(expectedESMapping));
}
 
Example #16
Source Project: es-amazon-s3-river   Author: lbroudoux   File: S3River.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Check if a mapping already exists in an index
 * @param index Index name
 * @param type Mapping name
 * @return true if mapping exists
 */
private boolean isMappingExist(String index, String type) {
   ClusterState cs = client.admin().cluster().prepareState()
         .setIndices(index).execute().actionGet()
         .getState();
   // Check index metadata existence.
   IndexMetaData imd = cs.getMetaData().index(index);
   if (imd == null){
      return false;
   }
   // Check mapping metadata existence.
   MappingMetaData mdd = imd.mapping(type);
   if (mdd != null){
      return true;
   }
   return false;
}
 
Example #17
Source Project: elasticsearch-helper   Author: jprante   File: IngestIndexCreationTest.java    License: 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 #18
Source Project: canal   Author: alibaba   File: ESConnectionTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test01() {
    MappingMetaData mappingMetaData = esConnection.getMapping("mytest_user");

    Map<String, Object> sourceMap = mappingMetaData.getSourceAsMap();
    Map<String, Object> esMapping = (Map<String, Object>) sourceMap.get("properties");
    for (Map.Entry<String, Object> entry : esMapping.entrySet()) {
        Map<String, Object> value = (Map<String, Object>) entry.getValue();
        if (value.containsKey("properties")) {
            System.out.println(entry.getKey() + " object");
        } else {
            System.out.println(entry.getKey() + " " + value.get("type"));
            Assert.notNull(entry.getKey(), "null column name");
            Assert.notNull(value.get("type"), "null column type");
        }
    }
}
 
Example #19
Source Project: crate   Author: crate   File: IndexShard.java    License: Apache License 2.0 6 votes vote down vote up
public boolean recoverFromLocalShards(BiConsumer<String, MappingMetaData> mappingUpdateConsumer, List<IndexShard> localShards) throws IOException {
    assert shardRouting.primary() : "recover from local shards only makes sense if the shard is a primary shard";
    assert recoveryState.getRecoverySource().getType() == RecoverySource.Type.LOCAL_SHARDS : "invalid recovery type: " + recoveryState.getRecoverySource();
    final List<LocalShardSnapshot> snapshots = new ArrayList<>();
    try {
        for (IndexShard shard : localShards) {
            snapshots.add(new LocalShardSnapshot(shard));
        }

        // we are the first primary, recover from the gateway
        // if its post api allocation, the index should exists
        assert shardRouting.primary() : "recover from local shards only makes sense if the shard is a primary shard";
        StoreRecovery storeRecovery = new StoreRecovery(shardId, logger);
        return storeRecovery.recoverFromLocalShards(mappingUpdateConsumer, this, snapshots);
    } finally {
        IOUtils.close(snapshots);
    }
}
 
Example #20
Source Project: crate   Author: crate   File: MapperService.java    License: Apache License 2.0 6 votes vote down vote up
private synchronized Map<String, DocumentMapper> internalMerge(IndexMetaData indexMetaData, MergeReason reason, boolean updateAllTypes,
                                                               boolean onlyUpdateIfNeeded) {
    Map<String, CompressedXContent> map = new LinkedHashMap<>();
    for (ObjectCursor<MappingMetaData> cursor : indexMetaData.getMappings().values()) {
        MappingMetaData mappingMetaData = cursor.value;
        if (onlyUpdateIfNeeded) {
            DocumentMapper existingMapper = documentMapper(mappingMetaData.type());
            if (existingMapper == null || mappingMetaData.source().equals(existingMapper.mappingSource()) == false) {
                map.put(mappingMetaData.type(), mappingMetaData.source());
            }
        } else {
            map.put(mappingMetaData.type(), mappingMetaData.source());
        }
    }
    return internalMerge(map, reason, updateAllTypes);
}
 
Example #21
Source Project: crate   Author: crate   File: PartitionInfos.java    License: Apache License 2.0 6 votes vote down vote up
private static PartitionInfo createPartitionInfo(ObjectObjectCursor<String, IndexMetaData> indexMetaDataEntry) {
    PartitionName partitionName = PartitionName.fromIndexOrTemplate(indexMetaDataEntry.key);
    try {
        IndexMetaData indexMetaData = indexMetaDataEntry.value;
        MappingMetaData mappingMetaData = indexMetaData.mapping(Constants.DEFAULT_MAPPING_TYPE);
        Map<String, Object> mappingMap = mappingMetaData.sourceAsMap();
        Map<String, Object> valuesMap = buildValuesMap(partitionName, mappingMetaData);
        Settings settings = indexMetaData.getSettings();
        String numberOfReplicas = NumberOfReplicas.fromSettings(settings);
        return new PartitionInfo(
            partitionName,
            indexMetaData.getNumberOfShards(),
            numberOfReplicas,
            IndexMetaData.SETTING_INDEX_VERSION_CREATED.get(settings),
            settings.getAsVersion(IndexMetaData.SETTING_VERSION_UPGRADED, null),
            DocIndexMetaData.isClosed(indexMetaData, mappingMap, false),
            valuesMap,
            indexMetaData.getSettings());
    } catch (Exception e) {
        LOGGER.trace("error extracting partition infos from index {}", e, indexMetaDataEntry.key);
        return null; // must filter on null
    }
}
 
Example #22
Source Project: crate   Author: crate   File: MetaDataIndexUpgraderTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void test__all_is_removed_from_mapping() throws Throwable {
    IndexMetaData indexMetaData = IndexMetaData.builder(new RelationName("doc", "users").indexNameOrAlias())
        .settings(Settings.builder().put("index.version.created", Version.ES_V_6_5_1))
        .numberOfShards(1)
        .numberOfReplicas(0)
        .putMapping(
            Constants.DEFAULT_MAPPING_TYPE,
            "{" +
            "   \"_all\": {\"enabled\": false}," +
            "   \"properties\": {" +
            "       \"name\": {" +
            "           \"type\": \"keyword\"" +
            "       }" +
            "   }" +
            "}")
        .build();

    MetaDataIndexUpgrader metaDataIndexUpgrader = new MetaDataIndexUpgrader();
    IndexMetaData updatedMetaData = metaDataIndexUpgrader.apply(indexMetaData);

    MappingMetaData mapping = updatedMetaData.mapping(Constants.DEFAULT_MAPPING_TYPE);
    assertThat(mapping.source().string(), Matchers.is("{\"default\":{\"properties\":{\"name\":{\"type\":\"keyword\"}}}}"));
}
 
Example #23
Source Project: search-spring-boot-starter   Author: niuzhiweimr   File: IndexUtils.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * 获取索引元数据信息
 *
 * @param index
 * @param type
 * @return
 */
public static MappingMetaData loadIndexMeta(Client client, String index, String type) {
    ClusterStateResponse response = client.admin().cluster().prepareState().execute().actionGet();
    ImmutableOpenMap<String, IndexMetaData> immutableOpenMap = response.getState().getMetaData().getIndices();
    if (immutableOpenMap != null) {
        IndexMetaData metaData = immutableOpenMap.get(index);
        if (metaData != null) {
            ImmutableOpenMap<String, MappingMetaData> mappings = metaData.getMappings();
            if (mappings != null) {
                return mappings.get(type);
            }
        }
    }
    log.error("获取ES数据结构失败 index:" + index + "|type:" + type);
    return null;
}
 
Example #24
Source Project: microservices-platform   Author: zlt2000   File: IndexServiceImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> show(String indexName) throws IOException {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    GetIndexResponse getIndexResponse = elasticsearchRestTemplate.getClient()
            .indices().get(request, RequestOptions.DEFAULT);
    ImmutableOpenMap<String, MappingMetaData> mappOpenMap = getIndexResponse.getMappings().get(indexName);
    List<AliasMetaData> indexAliases = getIndexResponse.getAliases().get(indexName);

    String settingsStr = getIndexResponse.getSettings().get(indexName).toString();
    Object settingsObj = null;
    if (StrUtil.isNotEmpty(settingsStr)) {
        settingsObj = JSONObject.parse(settingsStr);
    }
    Map<String, Object> result = new HashMap<>(1);
    Map<String, Object> indexMap = new HashMap<>(3);
    Map<String, Object> mappMap = new HashMap<>(mappOpenMap.size());
    List<String> aliasesList = new ArrayList<>(indexAliases.size());
    indexMap.put("aliases", aliasesList);
    indexMap.put("settings", settingsObj);
    indexMap.put("mappings", mappMap);
    result.put(indexName, indexMap);
    //获取mappings数据
    for (ObjectCursor<String> key : mappOpenMap.keys()) {
        MappingMetaData data = mappOpenMap.get(key.value);
        Map<String, Object> dataMap = data.getSourceAsMap();
        mappMap.put(key.value, dataMap);
    }
    //获取aliases数据
    for (AliasMetaData aliases : indexAliases) {
        aliasesList.add(aliases.getAlias());
    }
    return result;
}
 
Example #25
Source Project: summerframework   Author: spring-avengers   File: ElasticsearchTemplate.java    License: Apache License 2.0 5 votes vote down vote up
public List<Map<String, Object>> getAllMapping(String index) throws IOException {
    List<Map<String, Object>> result = new ArrayList<>();
    ImmutableOpenMap<String, MappingMetaData> mappings = esClient.admin().cluster().prepareState().execute()
        .actionGet().getState().getMetaData().getIndices().get(index).getMappings();

    for (ObjectObjectCursor<String, MappingMetaData> cursor : mappings) {
        log.info("type is:{}", cursor.key);
        result.add(cursor.value.getSourceAsMap());
    }
    return result;
}
 
Example #26
Source Project: summerframework   Author: spring-avengers   File: ElasticsearchTemplate.java    License: Apache License 2.0 5 votes vote down vote up
public List<Map<String, Object>> getAllMapping(String index) throws IOException {
    List<Map<String, Object>> result = new ArrayList<>();
    ImmutableOpenMap<String, MappingMetaData> mappings = esClient.admin().cluster().prepareState().execute()
        .actionGet().getState().getMetaData().getIndices().get(index).getMappings();

    for (ObjectObjectCursor<String, MappingMetaData> cursor : mappings) {
        log.info("type is:{}", cursor.key);
        result.add(cursor.value.getSourceAsMap());
    }
    return result;
}
 
Example #27
Source Project: presto-connectors   Author: harbby   File: Elasticsearch2Client.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    //----es scher error --
    Thread.currentThread().setName("getTable_001");
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example #28
Source Project: presto-connectors   Author: harbby   File: Elasticsearch6Client.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    Thread.currentThread().setName("getTable_001"); //----es scher error --
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example #29
Source Project: presto-connectors   Author: harbby   File: Elasticsearch5Client.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public ElasticsearchTable getTable(SchemaTableName tableName)
{
    String indexWildcard = tableName.getTableName();
    GetIndexRequest getIndexRequest = createGetIndexRequest(indexWildcard);
    Thread.currentThread().setName("getTable_001"); //----es scher error --
    GetIndexResponse response = client.admin().indices()
            .getIndex(getIndexRequest).actionGet();
    if (response.getIndices() == null || response.getIndices().length == 0) {
        return null;
    }
    //TODO: es中运行index名访问时可以使用*进行匹配,所以可能会返回多个index的mapping, 因此下面需要进行mapping merge  test table = test1"*"
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings = response.getMappings();

    List<IndexResolution> resolutions;
    if (mappings.size() > 0) {
        resolutions = new ArrayList<>(mappings.size());
        for (ObjectObjectCursor<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings : mappings) {
            resolutions.add(buildGetIndexResult(indexMappings.key, indexMappings.value));
        }
    }
    else {
        resolutions = emptyList();
    }

    IndexResolution indexWithMerged = merge(resolutions, indexWildcard);
    return new ElasticsearchTable(typeManager, tableName.getSchemaName(), tableName.getTableName(), indexWithMerged.get());
}
 
Example #30
Source Project: Elasticsearch   Author: baidu   File: TransportTypesExistsAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected void masterOperation(final TypesExistsRequest request, final ClusterState state, final ActionListener<TypesExistsResponse> listener) {
    String[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request.indicesOptions(), request.indices());
    if (concreteIndices.length == 0) {
        listener.onResponse(new TypesExistsResponse(false));
        return;
    }

    for (String concreteIndex : concreteIndices) {
        if (!state.metaData().hasConcreteIndex(concreteIndex)) {
            listener.onResponse(new TypesExistsResponse(false));
            return;
        }

        ImmutableOpenMap<String, MappingMetaData> mappings = state.metaData().getIndices().get(concreteIndex).getMappings();
        if (mappings.isEmpty()) {
            listener.onResponse(new TypesExistsResponse(false));
            return;
        }

        for (String type : request.types()) {
            if (!mappings.containsKey(type)) {
                listener.onResponse(new TypesExistsResponse(false));
                return;
            }
        }
    }

    listener.onResponse(new TypesExistsResponse(true));
}