Java Code Examples for org.elasticsearch.action.support.IndicesOptions#allowNoIndices()

The following examples show how to use org.elasticsearch.action.support.IndicesOptions#allowNoIndices() . 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: IndexNameExpressionResolver.java    From crate with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> resolve(Context context, List<String> expressions) {
    IndicesOptions options = context.getOptions();
    MetaData metaData = context.getState().metaData();
    if (options.expandWildcardsClosed() == false && options.expandWildcardsOpen() == false) {
        return expressions;
    }

    if (isEmptyOrTrivialWildcard(expressions)) {
        return resolveEmptyOrTrivialWildcard(options, metaData);
    }

    Set<String> result = innerResolve(context, expressions, options, metaData);

    if (result == null) {
        return expressions;
    }
    if (result.isEmpty() && !options.allowNoIndices()) {
        IndexNotFoundException infe = new IndexNotFoundException((String)null);
        infe.setResources("index_or_alias", expressions.toArray(new String[0]));
        throw infe;
    }
    return new ArrayList<>(result);
}
 
Example 2
Source File: SnapshotUtils.java    From Elasticsearch 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 (selectedIndices == null || selectedIndices.length == 0) {
        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<>();
                        result.addAll(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<>();
            result.addAll(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 3
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));
}