Search in sources :

Example 1 with GraphTokenStreamFiniteStrings

use of org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings in project lucene-solr by apache.

the class QueryBuilder method analyzeGraphPhrase.

/**
   * Creates a span near (phrase) query from a graph token stream. The articulation points of the graph are visited in
   * order and the queries created at each point are merged in the returned near query.
   */
protected SpanQuery analyzeGraphPhrase(TokenStream source, String field, int phraseSlop) throws IOException {
    source.reset();
    GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(source);
    List<SpanQuery> clauses = new ArrayList<>();
    int[] articulationPoints = graph.articulationPoints();
    int lastState = 0;
    for (int i = 0; i <= articulationPoints.length; i++) {
        int start = lastState;
        int end = -1;
        if (i < articulationPoints.length) {
            end = articulationPoints[i];
        }
        lastState = end;
        final SpanQuery queryPos;
        if (graph.hasSidePath(start)) {
            List<SpanQuery> queries = new ArrayList<>();
            Iterator<TokenStream> it = graph.getFiniteStrings(start, end);
            while (it.hasNext()) {
                TokenStream ts = it.next();
                SpanQuery q = createSpanQuery(ts, field);
                if (q != null) {
                    queries.add(q);
                }
            }
            if (queries.size() > 0) {
                queryPos = new SpanOrQuery(queries.toArray(new SpanQuery[0]));
            } else {
                queryPos = null;
            }
        } else {
            Term[] terms = graph.getTerms(field, start);
            assert terms.length > 0;
            if (terms.length == 1) {
                queryPos = new SpanTermQuery(terms[0]);
            } else {
                SpanTermQuery[] orClauses = new SpanTermQuery[terms.length];
                for (int idx = 0; idx < terms.length; idx++) {
                    orClauses[idx] = new SpanTermQuery(terms[idx]);
                }
                queryPos = new SpanOrQuery(orClauses);
            }
        }
        if (queryPos != null) {
            clauses.add(queryPos);
        }
    }
    if (clauses.isEmpty()) {
        return null;
    } else if (clauses.size() == 1) {
        return clauses.get(0);
    } else {
        return new SpanNearQuery(clauses.toArray(new SpanQuery[0]), phraseSlop, true);
    }
}
Also used : TokenStream(org.apache.lucene.analysis.TokenStream) GraphTokenStreamFiniteStrings(org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings) ArrayList(java.util.ArrayList) Term(org.apache.lucene.index.Term) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery)

Example 2 with GraphTokenStreamFiniteStrings

use of org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings in project lucene-solr by apache.

the class QueryBuilder method analyzeGraphBoolean.

/**
   * Creates a boolean query from a graph token stream. The articulation points of the graph are visited in order and the queries
   * created at each point are merged in the returned boolean query.
   */
protected Query analyzeGraphBoolean(String field, TokenStream source, BooleanClause.Occur operator) throws IOException {
    source.reset();
    GraphTokenStreamFiniteStrings graph = new GraphTokenStreamFiniteStrings(source);
    BooleanQuery.Builder builder = new BooleanQuery.Builder();
    int[] articulationPoints = graph.articulationPoints();
    int lastState = 0;
    for (int i = 0; i <= articulationPoints.length; i++) {
        int start = lastState;
        int end = -1;
        if (i < articulationPoints.length) {
            end = articulationPoints[i];
        }
        lastState = end;
        final Query queryPos;
        if (graph.hasSidePath(start)) {
            final Iterator<TokenStream> it = graph.getFiniteStrings(start, end);
            Iterator<Query> queries = new Iterator<Query>() {

                @Override
                public boolean hasNext() {
                    return it.hasNext();
                }

                @Override
                public Query next() {
                    TokenStream ts = it.next();
                    return createFieldQuery(ts, BooleanClause.Occur.MUST, field, getAutoGenerateMultiTermSynonymsPhraseQuery(), 0);
                }
            };
            queryPos = newGraphSynonymQuery(queries);
        } else {
            Term[] terms = graph.getTerms(field, start);
            assert terms.length > 0;
            if (terms.length == 1) {
                queryPos = newTermQuery(terms[0]);
            } else {
                queryPos = newSynonymQuery(terms);
            }
        }
        if (queryPos != null) {
            builder.add(queryPos, operator);
        }
    }
    BooleanQuery bq = builder.build();
    if (bq.clauses().size() == 1) {
        return bq.clauses().get(0).getQuery();
    }
    return bq;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TokenStream(org.apache.lucene.analysis.TokenStream) Query(org.apache.lucene.search.Query) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanOrQuery(org.apache.lucene.search.spans.SpanOrQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) GraphTokenStreamFiniteStrings(org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings) Term(org.apache.lucene.index.Term) Iterator(java.util.Iterator)

Aggregations

TokenStream (org.apache.lucene.analysis.TokenStream)2 Term (org.apache.lucene.index.Term)2 SpanNearQuery (org.apache.lucene.search.spans.SpanNearQuery)2 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)2 SpanQuery (org.apache.lucene.search.spans.SpanQuery)2 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)2 GraphTokenStreamFiniteStrings (org.apache.lucene.util.graph.GraphTokenStreamFiniteStrings)2 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 BooleanQuery (org.apache.lucene.search.BooleanQuery)1 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)1 PhraseQuery (org.apache.lucene.search.PhraseQuery)1 Query (org.apache.lucene.search.Query)1 SynonymQuery (org.apache.lucene.search.SynonymQuery)1 TermQuery (org.apache.lucene.search.TermQuery)1