Java Code Examples for org.elasticsearch.common.collect.ImmutableOpenMap#get()

The following examples show how to use org.elasticsearch.common.collect.ImmutableOpenMap#get() . 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: 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 2
Source File: JoinClusterAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public ClusterState execute(ClusterState currentState) {
    DiscoveryNodes.Builder nodesBuilder;
    nodesBuilder = DiscoveryNodes.builder(currentState.nodes());
    if (currentState.nodes().nodeExists(node.id())) {
        logger.debug("received a join request for an existing node [{}]", node);
        return currentState;
    } 
    // If this node is not in dead node list, then ignore this request
    ImmutableOpenMap<String, DiscoveryNode> deadNodes = clusterService.state().nodes().deadNodes();
    if (deadNodes.get(node.getIpPortAddress()) == null) {
        logger.warn("failed to find node [{}] in node list, ignore the join request", node);
        throw new IllegalStateException("could not find this node " + node + " from active node list and dead node list");
    }
    nodesBuilder.put(node);
    nodesBuilder.removeDeadNodeByIpPort(node);
    final ClusterState.Builder newStateBuilder = ClusterState.builder(currentState);
    newStateBuilder.nodes(nodesBuilder);
    ClusterState newState = newStateBuilder.build();
    return newState;
}
 
Example 3
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 4
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 5
Source File: DiskThresholdDecider.java    From crate with Apache License 2.0 6 votes vote down vote up
private DiskUsageWithRelocations getDiskUsage(RoutingNode node, RoutingAllocation allocation,
                                              ImmutableOpenMap<String, DiskUsage> usages, boolean subtractLeavingShards) {
    DiskUsage usage = usages.get(node.nodeId());
    if (usage == null) {
        // If there is no usage, and we have other nodes in the cluster,
        // use the average usage for all nodes as the usage for this node
        usage = averageUsage(node, usages);
        LOGGER.debug("unable to determine disk usage for {}, defaulting to average across nodes [{} total] [{} free] [{}% free]",
                node.nodeId(), usage.getTotalBytes(), usage.getFreeBytes(), usage.getFreeDiskAsPercentage());
    }

    final DiskUsageWithRelocations diskUsageWithRelocations = new DiskUsageWithRelocations(usage,
        sizeOfRelocatingShards(node, subtractLeavingShards, usage.getPath(),
            allocation.clusterInfo(), allocation.metaData(), allocation.routingTable()));
    LOGGER.trace("getDiskUsage(subtractLeavingShards={}) returning {}", subtractLeavingShards, diskUsageWithRelocations);
    return diskUsageWithRelocations;
}
 
Example 6
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 7
Source File: IndexServiceImpl.java    From microservices-platform with 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 8
Source File: JoinClusterAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
void handleJoinRequest(final DiscoveryNode node, final ClusterState state, final JoinCallback callback) {
    if (!transportService.addressSupported(node.address().getClass())) {
        logger.warn("received a wrong address type from [{}], ignoring...", node);
    } else {
        transportService.connectToNode(node);
        try {
            // if the node already in active node list, then ignore this request
            if (clusterService.state().nodes().nodeExists(node.getId())) {
                callback.onSuccess();
                return;
            }
            // If this node is not in dead node list, then ignore this request
            ImmutableOpenMap<String, DiscoveryNode> deadNodes = clusterService.state().nodes().deadNodes();
            if (deadNodes.get(node.getIpPortAddress()) == null) {
                logger.warn("failed to find node [{}] in node list, ignore the join request", node);
                callback.onFailure(new IllegalStateException("could not find this node " + node + " from node list"));
                return;
            }
        } catch (Throwable e) {
            logger.warn("failed to validate incoming join request from node [{}]", node);
            callback.onFailure(new IllegalStateException("failure when sending a validation request to node", e));
            return;
        }
        // join task has to be immediate because if node is left then all shards is failed, this will generate a lot of tasks
        clusterService.submitStateUpdateTask("dl-disco-join(join from node[" + node + "])", new ProcessJoinsTask(Priority.IMMEDIATE, node, callback));
    }
}
 
Example 9
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public Map<String, Object> getMappings() throws IOException {
	ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetaData>> indexMappings = client.admin()
			.indices()
			.prepareGetMappings(indexName)
			.setTypes(documentType)
			.execute()
			.actionGet()
			.getMappings();
	ImmutableOpenMap<String, MappingMetaData> typeMappings = indexMappings.get(indexName);
	MappingMetaData mappings = typeMappings.get(documentType);
	return mappings.sourceAsMap();
}
 
Example 10
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 11
Source File: ShowTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void showIndexType_onlyOneTypeReturn() throws SqlParseException, SQLFeatureNotSupportedException, IOException {
    String query = String.format("show %s/account", TEST_INDEX_ACCOUNT);
    GetIndexResponse getIndexResponse = runShowQuery(query);
    ImmutableOpenMap<String, ImmutableOpenMap<String, MappingMetadata>> mappings = getIndexResponse.getMappings();
    ImmutableOpenMap<String, MappingMetadata> typeToData = mappings.get(TEST_INDEX_ACCOUNT);
    Assert.assertEquals(1,typeToData.size());
}
 
Example 12
Source File: SearchUpdaterImpl.java    From stash-codesearch-plugin with Apache License 2.0 5 votes vote down vote up
private String getIndexFromAlias(String alias) {
    ImmutableOpenMap<String, List<AliasMetaData>> aliasMap =
        es.getClient().admin().indices().prepareGetAliases(alias).get().getAliases();
    for (String index : aliasMap.keys().toArray(String.class)) {
        for (AliasMetaData aliasEntry : aliasMap.get(index)) {
            if (aliasEntry.getAlias().equals(alias)) {
                return index;
            }
        }
    }
    return null;
}
 
Example 13
Source File: SnapshotShardsService.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if any shards were processed that the new master doesn't know about
 */
private void syncShardStatsOnNewMaster(ClusterChangedEvent event) {
    SnapshotsInProgress snapshotsInProgress = event.state().custom(SnapshotsInProgress.TYPE);
    if (snapshotsInProgress == null) {
        return;
    }

    for (SnapshotsInProgress.Entry snapshot : snapshotsInProgress.entries()) {
        if (snapshot.state() == State.STARTED || snapshot.state() == State.ABORTED) {
            Map<ShardId, IndexShardSnapshotStatus> localShards = currentSnapshotShards(snapshot.snapshot());
            if (localShards != null) {
                ImmutableOpenMap<ShardId, ShardSnapshotStatus> masterShards = snapshot.shards();
                for (Map.Entry<ShardId, IndexShardSnapshotStatus> localShard : localShards.entrySet()) {
                    ShardId shardId = localShard.getKey();
                    ShardSnapshotStatus masterShard = masterShards.get(shardId);
                    if (masterShard != null && masterShard.state().completed() == false) {
                        final IndexShardSnapshotStatus.Copy indexShardSnapshotStatus = localShard.getValue().asCopy();
                        final Stage stage = indexShardSnapshotStatus.getStage();
                        // Master knows about the shard and thinks it has not completed
                        if (stage == Stage.DONE) {
                            // but we think the shard is done - we need to make new master know that the shard is done
                            LOGGER.debug("[{}] new master thinks the shard [{}] is not completed but the shard is done locally, " +
                                "updating status on the master", snapshot.snapshot(), shardId);
                            notifySuccessfulSnapshotShard(snapshot.snapshot(), shardId, localShard.getValue().generation());

                        } else if (stage == Stage.FAILURE) {
                            // but we think the shard failed - we need to make new master know that the shard failed
                            LOGGER.debug("[{}] new master thinks the shard [{}] is not completed but the shard failed locally, " +
                                "updating status on master", snapshot.snapshot(), shardId);
                            notifyFailedSnapshotShard(snapshot.snapshot(), shardId, indexShardSnapshotStatus.getFailure());
                        }
                    }
                }
            }
        }
    }
}
 
Example 14
Source File: ESTemplate.java    From canal-1.1.3 with Apache License 2.0 4 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) {
        ImmutableOpenMap<String, MappingMetaData> mappings;
        try {
            mappings = transportClient.admin()
                .cluster()
                .prepareState()
                .execute()
                .actionGet()
                .getState()
                .getMetaData()
                .getIndices()
                .get(mapping.get_index())
                .getMappings();
        } catch (NullPointerException e) {
            throw new IllegalArgumentException("Not found the mapping info of index: " + mapping.get_index());
        }
        MappingMetaData mappingMetaData = mappings.get(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);
}