Search in sources :

Example 1 with ReversedWildcardFilterFactory

use of org.apache.solr.analysis.ReversedWildcardFilterFactory in project lucene-solr by apache.

the class SolrQueryParserBase method getWildcardQuery.

// called from parser
protected Query getWildcardQuery(String field, String termStr) throws SyntaxError {
    checkNullField(field);
    // *:* -> MatchAllDocsQuery
    if ("*".equals(termStr)) {
        if ("*".equals(field) || getExplicitField() == null) {
            return newMatchAllDocsQuery();
        }
    }
    FieldType fieldType = schema.getFieldType(field);
    termStr = analyzeIfMultitermTermText(field, termStr, fieldType);
    // can we use reversed wildcards in this field?
    ReversedWildcardFilterFactory factory = getReversedWildcardFilterFactory(fieldType);
    if (factory != null) {
        Term term = new Term(field, termStr);
        // fsa representing the query
        Automaton automaton = WildcardQuery.toAutomaton(term);
        // TODO: we should likely use the automaton to calculate shouldReverse, too.
        if (factory.shouldReverse(termStr)) {
            automaton = Operations.concatenate(automaton, Automata.makeChar(factory.getMarkerChar()));
            automaton = Operations.reverse(automaton);
        } else {
            // reverse wildcardfilter is active: remove false positives
            // fsa representing false positives (markerChar*)
            Automaton falsePositives = Operations.concatenate(Automata.makeChar(factory.getMarkerChar()), Automata.makeAnyString());
            // subtract these away
            automaton = Operations.minus(automaton, falsePositives, Operations.DEFAULT_MAX_DETERMINIZED_STATES);
        }
        return new AutomatonQuery(term, automaton) {

            // override toString so it's completely transparent
            @Override
            public String toString(String field) {
                StringBuilder buffer = new StringBuilder();
                if (!getField().equals(field)) {
                    buffer.append(getField());
                    buffer.append(":");
                }
                buffer.append(term.text());
                return buffer.toString();
            }
        };
    }
    // Solr has always used constant scoring for wildcard queries.  This should return constant scoring by default.
    return newWildcardQuery(new Term(field, termStr));
}
Also used : AutomatonQuery(org.apache.lucene.search.AutomatonQuery) Automaton(org.apache.lucene.util.automaton.Automaton) ReversedWildcardFilterFactory(org.apache.solr.analysis.ReversedWildcardFilterFactory) Term(org.apache.lucene.index.Term) FieldType(org.apache.solr.schema.FieldType)

Example 2 with ReversedWildcardFilterFactory

use of org.apache.solr.analysis.ReversedWildcardFilterFactory in project lucene-solr by apache.

the class SolrQueryParserBase method getReversedWildcardFilterFactory.

protected ReversedWildcardFilterFactory getReversedWildcardFilterFactory(FieldType fieldType) {
    if (leadingWildcards == null)
        leadingWildcards = new HashMap<>();
    ReversedWildcardFilterFactory fac = leadingWildcards.get(fieldType);
    if (fac != null || leadingWildcards.containsKey(fieldType)) {
        return fac;
    }
    Analyzer a = fieldType.getIndexAnalyzer();
    if (a instanceof TokenizerChain) {
        // examine the indexing analysis chain if it supports leading wildcards
        TokenizerChain tc = (TokenizerChain) a;
        TokenFilterFactory[] factories = tc.getTokenFilterFactories();
        for (TokenFilterFactory factory : factories) {
            if (factory instanceof ReversedWildcardFilterFactory) {
                fac = (ReversedWildcardFilterFactory) factory;
                break;
            }
        }
    }
    leadingWildcards.put(fieldType, fac);
    return fac;
}
Also used : TokenizerChain(org.apache.solr.analysis.TokenizerChain) HashMap(java.util.HashMap) ReversedWildcardFilterFactory(org.apache.solr.analysis.ReversedWildcardFilterFactory) Analyzer(org.apache.lucene.analysis.Analyzer) TokenFilterFactory(org.apache.lucene.analysis.util.TokenFilterFactory)

Aggregations

ReversedWildcardFilterFactory (org.apache.solr.analysis.ReversedWildcardFilterFactory)2 HashMap (java.util.HashMap)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 TokenFilterFactory (org.apache.lucene.analysis.util.TokenFilterFactory)1 Term (org.apache.lucene.index.Term)1 AutomatonQuery (org.apache.lucene.search.AutomatonQuery)1 Automaton (org.apache.lucene.util.automaton.Automaton)1 TokenizerChain (org.apache.solr.analysis.TokenizerChain)1 FieldType (org.apache.solr.schema.FieldType)1