Search in sources :

Example 36 with CharacterRunAutomaton

use of org.apache.lucene.util.automaton.CharacterRunAutomaton in project crate by crate.

the class XContentMapValues method filter.

private static Map<String, Object> filter(Map<String, ?> map, CharacterRunAutomaton includeAutomaton, int initialIncludeState, CharacterRunAutomaton excludeAutomaton, int initialExcludeState, CharacterRunAutomaton matchAllAutomaton) {
    Map<String, Object> filtered = new HashMap<>();
    for (Map.Entry<String, ?> entry : map.entrySet()) {
        String key = entry.getKey();
        int includeState = step(includeAutomaton, key, initialIncludeState);
        if (includeState == -1) {
            continue;
        }
        int excludeState = step(excludeAutomaton, key, initialExcludeState);
        if (excludeState != -1 && excludeAutomaton.isAccept(excludeState)) {
            continue;
        }
        Object value = entry.getValue();
        CharacterRunAutomaton subIncludeAutomaton = includeAutomaton;
        int subIncludeState = includeState;
        if (includeAutomaton.isAccept(includeState)) {
            if (excludeState == -1 || excludeAutomaton.step(excludeState, '.') == -1) {
                // the exclude has no chances to match inner properties
                filtered.put(key, value);
                continue;
            } else {
                // the object matched, so consider that the include matches every inner property
                // we only care about excludes now
                subIncludeAutomaton = matchAllAutomaton;
                subIncludeState = 0;
            }
        }
        if (value instanceof Map) {
            subIncludeState = subIncludeAutomaton.step(subIncludeState, '.');
            if (subIncludeState == -1) {
                continue;
            }
            if (excludeState != -1) {
                excludeState = excludeAutomaton.step(excludeState, '.');
            }
            Map<String, Object> valueAsMap = (Map<String, Object>) value;
            Map<String, Object> filteredValue = filter(valueAsMap, subIncludeAutomaton, subIncludeState, excludeAutomaton, excludeState, matchAllAutomaton);
            if (includeAutomaton.isAccept(includeState) || filteredValue.isEmpty() == false) {
                filtered.put(key, filteredValue);
            }
        } else if (value instanceof Iterable) {
            List<Object> filteredValue = filter((Iterable<?>) value, subIncludeAutomaton, subIncludeState, excludeAutomaton, excludeState, matchAllAutomaton);
            if (filteredValue.isEmpty() == false) {
                filtered.put(key, filteredValue);
            }
        } else {
            // leaf property
            if (includeAutomaton.isAccept(includeState) && (excludeState == -1 || excludeAutomaton.isAccept(excludeState) == false)) {
                filtered.put(key, value);
            }
        }
    }
    return filtered;
}
Also used : HashMap(java.util.HashMap) CharacterRunAutomaton(org.apache.lucene.util.automaton.CharacterRunAutomaton) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CharacterRunAutomaton (org.apache.lucene.util.automaton.CharacterRunAutomaton)36 RegExp (org.apache.lucene.util.automaton.RegExp)15 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)10 ArrayList (java.util.ArrayList)7 Term (org.apache.lucene.index.Term)7 PhraseQuery (org.apache.lucene.search.PhraseQuery)6 Query (org.apache.lucene.search.Query)6 TermQuery (org.apache.lucene.search.TermQuery)6 List (java.util.List)5 Analyzer (org.apache.lucene.analysis.Analyzer)5 BooleanQuery (org.apache.lucene.search.BooleanQuery)5 BoostQuery (org.apache.lucene.search.BoostQuery)5 Automaton (org.apache.lucene.util.automaton.Automaton)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Document (org.apache.lucene.document.Document)4 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)4 PrefixQuery (org.apache.lucene.search.PrefixQuery)4 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)4 MockTokenizer (org.apache.lucene.analysis.MockTokenizer)3