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 File: PartitionInfos.java    From Elasticsearch with 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 #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: PartitionInfos.java    From crate with 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 #4
Source File: MetaDataIndexUpgraderTest.java    From crate with 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 #5
Source File: MapperService.java    From crate with 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 #6
Source File: IndexAdmin.java    From search-spring-boot-starter with 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 #7
Source File: IndexShard.java    From crate with 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 #8
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 #9
Source File: ModifyIndexFactory.java    From search-spring-boot-starter with 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 #10
Source File: ESConnectionTest.java    From canal with 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 #11
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 #12
Source File: S3River.java    From es-amazon-s3-river with 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 #13
Source File: ElasticSearchSerializerWithMappingTest.java    From ingestion with 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 #14
Source File: GetMappingsResponse.java    From Elasticsearch with 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 #15
Source File: SQLExecutor.java    From crate with 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 #16
Source File: Elasticsearch5SearchIndex.java    From vertexium with 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 #17
Source File: GetIndexResponse.java    From Elasticsearch with 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 #18
Source File: Elasticsearch7SearchIndex.java    From vertexium with 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 #19
Source File: TransportDeleteAction.java    From Elasticsearch with 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 #20
Source File: ElasticsearchIntegrationTest.java    From openshift-elasticsearch-plugin with 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 #21
Source File: TransportIndexAction.java    From Elasticsearch with 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 #22
Source File: TransportShardBulkAction.java    From Elasticsearch with 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 #23
Source File: ES6xTemplate.java    From canal with Apache License 2.0 5 votes vote down vote up
/**
 * 获取es mapping中的属性类型
 *
 * @param mapping mapping配置
 * @param fieldName 属性名
 * @return 类型
 */
@SuppressWarnings("unchecked")
private String getEsType(ESMapping mapping, String fieldName) {
    String key = mapping.get_index() + "-" + mapping.get_type();
    Map<String, String> fieldType = esFieldTypes.get(key);
    if (fieldType != null) {
        return fieldType.get(fieldName);
    } else {
        MappingMetaData mappingMetaData = esConnection.getMapping(mapping.get_index(), mapping.get_type());

        if (mappingMetaData == null) {
            throw new IllegalArgumentException("Not found the mapping info of index: " + mapping.get_index());
        }

        fieldType = new LinkedHashMap<>();

        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")) {
                fieldType.put(entry.getKey(), "object");
            } else {
                fieldType.put(entry.getKey(), (String) value.get("type"));
            }
        }
        esFieldTypes.put(key, fieldType);

        return fieldType.get(fieldName);
    }
}
 
Example #24
Source File: MetaDataIndexUpgraderTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testDynamicStringTemplateIsPurged() throws IOException {
    MetaDataIndexUpgrader metaDataIndexUpgrader = new MetaDataIndexUpgrader();
    MappingMetaData mappingMetaData = new MappingMetaData(createDynamicStringMappingTemplate());
    MappingMetaData newMappingMetaData = metaDataIndexUpgrader.createUpdatedIndexMetaData(mappingMetaData, "dummy");

    Object dynamicTemplates = newMappingMetaData.getSourceAsMap().get("dynamic_templates");
    assertThat(dynamicTemplates, nullValue());

    // Check that the new metadata still has the root "default" element
    assertThat("{\"default\":{}}", is(newMappingMetaData.source().toString()));
}
 
Example #25
Source File: VertexiumQueryStringQueryBuilder.java    From vertexium with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
protected FieldNameToVisibilityMap getFieldNameToVisibilityMap(QueryShardContext context) {
    try {
        Map<String, String> results = new HashMap<>();
        ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> mappings
            = context.getClient().admin().indices().prepareGetMappings().get().getMappings();
        for (ObjectCursor<String> index : mappings.keys()) {
            ImmutableOpenMap<String, MappingMetaData> types = mappings.get(index.value);
            if (types == null) {
                continue;
            }
            MappingMetaData elementMetadata = types.get(ELEMENT_DOCUMENT_MAPPER_NAME);
            if (elementMetadata == null) {
                continue;
            }
            Map<String, Map<String, String>> meta = (Map<String, Map<String, String>>) elementMetadata.getSourceAsMap().get("_meta");
            if (meta == null) {
                continue;
            }
            Map<String, String> vertexiumMeta = meta.get("vertexium");
            if (vertexiumMeta == null) {
                continue;
            }
            results.putAll(vertexiumMeta);
        }

        return FieldNameToVisibilityMap.createFromVertexiumMetadata(results);
    } catch (Exception ex) {
        throw new RuntimeException("Could not get mappings", ex);
    }
}
 
Example #26
Source File: DocIndexMetaDataTest.java    From crate with Apache License 2.0 5 votes vote down vote up
private IndexMetaData getIndexMetaData(String indexName,
                                       XContentBuilder builder) throws IOException {
    Map<String, Object> mappingSource = XContentHelper.convertToMap(BytesReference.bytes(builder), true, XContentType.JSON).v2();
    mappingSource = sortProperties(mappingSource);

    Settings.Builder settingsBuilder = Settings.builder()
        .put("index.number_of_shards", 1)
        .put("index.number_of_replicas", 0)
        .put("index.version.created", org.elasticsearch.Version.CURRENT);

    IndexMetaData.Builder mdBuilder = IndexMetaData.builder(indexName)
        .settings(settingsBuilder)
        .putMapping(new MappingMetaData(Constants.DEFAULT_MAPPING_TYPE, mappingSource));
    return mdBuilder.build();
}
 
Example #27
Source File: PartitionedTableIntegrationTest.java    From crate with Apache License 2.0 5 votes vote down vote up
@Test
public void testAlterPartitionedTableKeepsMetadata() throws Exception {
    execute("create table dynamic_table (" +
            "  id integer, " +
            "  score double" +
            ") partitioned by (score) with (number_of_replicas=0, column_policy='dynamic')");
    ensureGreen();
    execute("insert into dynamic_table (id, score) values (1, 10)");
    execute("refresh table dynamic_table");
    ensureGreen();
    MappingMetaData partitionMetaData = clusterService().state().metaData().indices()
        .get(new PartitionName(
            new RelationName(sqlExecutor.getCurrentSchema(), "dynamic_table"),
            Collections.singletonList("10.0")).asIndexName())
        .getMappings().get(DEFAULT_MAPPING_TYPE);
    Map<String, Object> metaMap = (Map) partitionMetaData.getSourceAsMap().get("_meta");
    assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
    execute("alter table dynamic_table set (column_policy= 'dynamic')");
    waitNoPendingTasksOnAll();
    partitionMetaData = clusterService().state().metaData().indices()
        .get(new PartitionName(
            new RelationName(sqlExecutor.getCurrentSchema(), "dynamic_table"),
            Collections.singletonList("10.0")).asIndexName())
        .getMappings().get(DEFAULT_MAPPING_TYPE);
    metaMap = (Map) partitionMetaData.getSourceAsMap().get("_meta");
    assertThat(String.valueOf(metaMap.get("partitioned_by")), Matchers.is("[[score, double]]"));
}
 
Example #28
Source File: IndexUtils.java    From search-spring-boot-starter with 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 #29
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 #30
Source File: DcMappingMetaDataImpl.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * .
 * @param meta .
 * @return .
 */
public static DcMappingMetaData getInstance(MappingMetaData meta) {
    if (meta == null) {
        return null;
    }
    return new DcMappingMetaDataImpl(meta);
}