Java Code Examples for org.elasticsearch.common.regex.Regex#simpleMatch()

The following examples show how to use org.elasticsearch.common.regex.Regex#simpleMatch() . 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: TransportGetIndexTemplatesAction.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) {
    List<IndexTemplateMetaData> results;

    // If we did not ask for a specific name, then we return all templates
    if (request.names().length == 0) {
        results = Arrays.asList(state.metaData().templates().values().toArray(IndexTemplateMetaData.class));
    } else {
        results = new ArrayList<>();
    }

    for (String name : request.names()) {
        if (Regex.isSimpleMatchPattern(name)) {
            for (ObjectObjectCursor<String, IndexTemplateMetaData> entry : state.metaData().templates()) {
                if (Regex.simpleMatch(name, entry.key)) {
                    results.add(entry.value);
                }
            }
        } else if (state.metaData().templates().containsKey(name)) {
            results.add(state.metaData().templates().get(name));
        }
    }

    listener.onResponse(new GetIndexTemplatesResponse(results));
}
 
Example 2
Source File: TransportGetIndexTemplatesAction.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
protected void masterOperation(GetIndexTemplatesRequest request, ClusterState state, ActionListener<GetIndexTemplatesResponse> listener) {
    List<IndexTemplateMetaData> results;

    // If we did not ask for a specific name, then we return all templates
    if (request.names().length == 0) {
        results = Arrays.asList(state.metaData().templates().values().toArray(IndexTemplateMetaData.class));
    } else {
        results = new ArrayList<>();
    }

    for (String name : request.names()) {
        if (Regex.isSimpleMatchPattern(name)) {
            for (ObjectObjectCursor<String, IndexTemplateMetaData> entry : state.metaData().templates()) {
                if (Regex.simpleMatch(name, entry.key)) {
                    results.add(entry.value);
                }
            }
        } else if (state.metaData().templates().containsKey(name)) {
            results.add(state.metaData().templates().get(name));
        }
    }

    listener.onResponse(new GetIndexTemplatesResponse(results));
}
 
Example 3
Source File: BaseTasksRequest.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public boolean match(Task task) {
    if (getActions() != null && getActions().length > 0 && Regex.simpleMatch(getActions(), task.getAction()) == false) {
        return false;
    }
    if (getTaskId().isSet()) {
        if(getTaskId().getId() != task.getId()) {
            return false;
        }
    }
    if (parentTaskId.isSet()) {
        if (parentTaskId.equals(task.getParentTaskId()) == false) {
            return false;
        }
    }
    return true;
}
 
Example 4
Source File: TribeService.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private void addNewIndex(ClusterState tribeState, ClusterBlocks.Builder blocks, MetaData.Builder metaData, RoutingTable.Builder routingTable, IndexMetaData tribeIndex) {
    Settings tribeSettings = Settings.builder().put(tribeIndex.getSettings()).put(TRIBE_NAME, tribeName).build();
    metaData.put(IndexMetaData.builder(tribeIndex).settings(tribeSettings));
    routingTable.add(tribeState.routingTable().index(tribeIndex.getIndex()));
    if (Regex.simpleMatch(blockIndicesMetadata, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_METADATA_BLOCK);
    }
    if (Regex.simpleMatch(blockIndicesRead, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_READ_BLOCK);
    }
    if (Regex.simpleMatch(blockIndicesWrite, tribeIndex.getIndex())) {
        blocks.addIndexBlock(tribeIndex.getIndex(), IndexMetaData.INDEX_WRITE_BLOCK);
    }
}
 
Example 5
Source File: TransportGetSettingsAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void masterOperation(GetSettingsRequest request, ClusterState state, ActionListener<GetSettingsResponse> listener) {
    String[] concreteIndices = indexNameExpressionResolver.concreteIndices(state, request);
    ImmutableOpenMap.Builder<String, Settings> indexToSettingsBuilder = ImmutableOpenMap.builder();
    for (String concreteIndex : concreteIndices) {
        IndexMetaData indexMetaData = state.getMetaData().index(concreteIndex);
        if (indexMetaData == null) {
            continue;
        }

        Settings settings = SettingsFilter.filterSettings(settingsFilter.getPatterns(), indexMetaData.getSettings());
        if (request.humanReadable()) {
            settings = IndexMetaData.addHumanReadableSettings(settings);
        }
        if (!CollectionUtils.isEmpty(request.names())) {
            Settings.Builder settingsBuilder = Settings.builder();
            for (Map.Entry<String, String> entry : settings.getAsMap().entrySet()) {
                if (Regex.simpleMatch(request.names(), entry.getKey())) {
                    settingsBuilder.put(entry.getKey(), entry.getValue());
                }
            }
            settings = settingsBuilder.build();
        }
        indexToSettingsBuilder.put(concreteIndex, settings);
    }
    listener.onResponse(new GetSettingsResponse(indexToSettingsBuilder.build()));
}
 
Example 6
Source File: FieldTypeLookup.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a list of the full names of a simple match regex like pattern against full name and index name.
 */
public Collection<String> simpleMatchToFullName(String pattern) {
    Set<String> fields = new HashSet<>();
    for (MappedFieldType fieldType : this) {
        if (Regex.simpleMatch(pattern, fieldType.name())) {
            fields.add(fieldType.name());
        }
    }
    for (String aliasName : aliasToConcreteName.keySet()) {
        if (Regex.simpleMatch(pattern, aliasName)) {
            fields.add(aliasName);
        }
    }
    return fields;
}
 
Example 7
Source File: DiscoveryNodeFilters.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private boolean matchByIP(String[] values, @Nullable String hostIp, @Nullable String publishIp) {
    for (String value : values) {
        boolean matchIp = Regex.simpleMatch(value, hostIp) || Regex.simpleMatch(value, publishIp);
        if (matchIp) {
            return matchIp;
        }
    }
    return false;
}
 
Example 8
Source File: DynamicSettings.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public String validateDynamicSetting(String dynamicSetting, String value, ClusterState clusterState) {
    for (Map.Entry<String, Validator> setting : dynamicSettings.entrySet()) {
        if (Regex.simpleMatch(setting.getKey(), dynamicSetting)) {
            return setting.getValue().validate(dynamicSetting, value, clusterState);
        }
    }
    return null;
}
 
Example 9
Source File: DefaultIndexTemplateFilter.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public boolean apply(CreateIndexClusterStateUpdateRequest request, IndexTemplateMetaData template) {
    long tenantId = TenantProperty.ROOT_TENANT_ID;
    LoginUserContext userInfo = (LoginUserContext)request.originalMessage().getHeader(LoginUserContext.USER_INFO_KEY);
    if (userInfo != null) {
        tenantId = userInfo.tenantId();
    }
    if (tenantId != template.templateOwnerTenantId()) {
        return false;
    }
    return Regex.simpleMatch(template.template(), request.index());
}
 
Example 10
Source File: RestTable.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * Extracts all the required fields from the RestRequest 'h' parameter. In order to support wildcards like
 * 'bulk.*' this needs potentially parse all the configured headers and its aliases and needs to ensure
 * that everything is only added once to the returned headers, even if 'h=bulk.*.bulk.*' is specified
 * or some headers are contained twice due to matching aliases
 */
private static Set<String> expandHeadersFromRequest(Table table, RestRequest request) {
    Set<String> headers = new LinkedHashSet<>(table.getHeaders().size());

    // check headers and aliases
    for (String header : Strings.splitStringByCommaToArray(request.param("h"))) {
        if (Regex.isSimpleMatchPattern(header)) {
            for (Table.Cell tableHeaderCell : table.getHeaders()) {
                String configuredHeader = tableHeaderCell.value.toString();
                if (Regex.simpleMatch(header, configuredHeader)) {
                    headers.add(configuredHeader);
                } else if (tableHeaderCell.attr.containsKey("alias")) {
                    String[] aliases = Strings.splitStringByCommaToArray(tableHeaderCell.attr.get("alias"));
                    for (String alias : aliases) {
                        if (Regex.simpleMatch(header, alias)) {
                            headers.add(configuredHeader);
                            break;
                        }
                    }
                }
            }
        } else {
            headers.add(header);
        }
    }

    return headers;
}
 
Example 11
Source File: CustomFieldsVisitor.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Status needsField(FieldInfo fieldInfo) throws IOException {
    if (super.needsField(fieldInfo) == Status.YES) {
        return Status.YES;
    }
    if (fields.contains(fieldInfo.name)) {
        return Status.YES;
    }
    for (String pattern : patterns) {
        if (Regex.simpleMatch(pattern, fieldInfo.name)) {
            return Status.YES;
        }
    }
    return Status.NO;
}
 
Example 12
Source File: TransportService.java    From crate with Apache License 2.0 5 votes vote down vote up
private boolean shouldTraceAction(String action) {
    if (tracerLogInclude.length > 0) {
        if (Regex.simpleMatch(tracerLogInclude, action) == false) {
            return false;
        }
    }
    if (tracerLogExclude.length > 0) {
        return !Regex.simpleMatch(tracerLogExclude, action);
    }
    return true;
}
 
Example 13
Source File: MockLogAppender.java    From crate with Apache License 2.0 5 votes vote down vote up
@Override
public void match(LogEvent event) {
    if (event.getLevel().equals(level) && event.getLoggerName().equals(logger) && innerMatch(event)) {
        if (Regex.isSimpleMatchPattern(message)) {
            if (Regex.simpleMatch(message, event.getMessage().getFormattedMessage())) {
                saw = true;
            }
        } else {
            if (event.getMessage().getFormattedMessage().contains(message)) {
                saw = true;
            }
        }
    }
}
 
Example 14
Source File: ShardFieldData.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public FieldDataStats stats(String... fields) {
    ObjectLongHashMap<String> fieldTotals = null;
    if (fields != null && fields.length > 0) {
        fieldTotals = new ObjectLongHashMap<>();
        for (Map.Entry<String, CounterMetric> entry : perFieldTotals.entrySet()) {
            if (Regex.simpleMatch(fields, entry.getKey())) {
                fieldTotals.put(entry.getKey(), entry.getValue().count());
            }
        }
    }
    return new FieldDataStats(totalMetric.count(), evictionsMetric.count(), fieldTotals);
}
 
Example 15
Source File: IndicesQueryParser.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
protected boolean matchesIndices(String currentIndex, String... indices) {
    final String[] concreteIndices = indexNameExpressionResolver.concreteIndices(clusterService.state(), IndicesOptions.lenientExpandOpen(), indices);
    for (String index : concreteIndices) {
        if (Regex.simpleMatch(index, currentIndex)) {
            return true;
        }
    }
    return false;
}
 
Example 16
Source File: DynamicTemplate.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public boolean matches(String pattern, String value) {
    return Regex.simpleMatch(pattern, value);
}
 
Example 17
Source File: SnapshotUtils.java    From crate with Apache License 2.0 4 votes vote down vote up
/**
 * Filters out list of available indices based on the list of selected indices.
 *
 * @param availableIndices list of available indices
 * @param selectedIndices  list of selected indices
 * @param indicesOptions    ignore indices flag
 * @return filtered out indices
 */
public static List<String> filterIndices(List<String> availableIndices, String[] selectedIndices, IndicesOptions indicesOptions) {
    if (IndexNameExpressionResolver.isAllIndices(Arrays.asList(selectedIndices))) {
        return availableIndices;
    }
    Set<String> result = null;
    for (int i = 0; i < selectedIndices.length; i++) {
        String indexOrPattern = selectedIndices[i];
        boolean add = true;
        if (!indexOrPattern.isEmpty()) {
            if (availableIndices.contains(indexOrPattern)) {
                if (result == null) {
                    result = new HashSet<>();
                }
                result.add(indexOrPattern);
                continue;
            }
            if (indexOrPattern.charAt(0) == '+') {
                add = true;
                indexOrPattern = indexOrPattern.substring(1);
                // if its the first, add empty set
                if (i == 0) {
                    result = new HashSet<>();
                }
            } else if (indexOrPattern.charAt(0) == '-') {
                // if its the first, fill it with all the indices...
                if (i == 0) {
                    result = new HashSet<>(availableIndices);
                }
                add = false;
                indexOrPattern = indexOrPattern.substring(1);
            }
        }
        if (indexOrPattern.isEmpty() || !Regex.isSimpleMatchPattern(indexOrPattern)) {
            if (!availableIndices.contains(indexOrPattern)) {
                if (!indicesOptions.ignoreUnavailable()) {
                    throw new IndexNotFoundException(indexOrPattern);
                } else {
                    if (result == null) {
                        // add all the previous ones...
                        result = new HashSet<>(availableIndices.subList(0, i));
                    }
                }
            } else {
                if (result != null) {
                    if (add) {
                        result.add(indexOrPattern);
                    } else {
                        result.remove(indexOrPattern);
                    }
                }
            }
            continue;
        }
        if (result == null) {
            // add all the previous ones...
            result = new HashSet<>(availableIndices.subList(0, i));
        }
        boolean found = false;
        for (String index : availableIndices) {
            if (Regex.simpleMatch(indexOrPattern, index)) {
                found = true;
                if (add) {
                    result.add(index);
                } else {
                    result.remove(index);
                }
            }
        }
        if (!found && !indicesOptions.allowNoIndices()) {
            throw new IndexNotFoundException(indexOrPattern);
        }
    }
    if (result == null) {
        return Collections.unmodifiableList(new ArrayList<>(Arrays.asList(selectedIndices)));
    }
    return Collections.unmodifiableList(new ArrayList<>(result));
}
 
Example 18
Source File: DynamicTemplate.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private boolean patternMatch(String pattern, String str) {
    if (matchType == MatchType.SIMPLE) {
        return Regex.simpleMatch(pattern, str);
    }
    return str.matches(pattern);
}
 
Example 19
Source File: FilterPath.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public FilterPath matchProperty(String name) {
    if ((next != null) && (simpleWildcard || doubleWildcard || Regex.simpleMatch(segment, name))) {
        return next;
    }
    return null;
}
 
Example 20
Source File: Setting.java    From crate with Apache License 2.0 4 votes vote down vote up
@Override
public boolean match(String toTest) {
    return Regex.simpleMatch(key + "*", toTest);
}