Search in sources :

Example 1 with SuggesterQuery

use of org.opengrok.suggest.query.SuggesterQuery in project OpenGrok by OpenGrok.

the class SuggesterQueryDataParser method parse.

/**
 * Parses the {@link SuggesterQueryData}.
 * @param data data to parse
 * @return parsed data for the suggester use
 * @throws ParseException could not parse the search data into a valid {@link Query}
 */
public static SuggesterData parse(final SuggesterQueryData data) throws ParseException {
    List<String> projectList = data.getProjects().stream().filter(p -> RuntimeEnvironment.getInstance().getProjectNames().contains(p)).collect(Collectors.toList());
    Map<String, String> fieldQueries = getFieldQueries(data);
    ProcessedQueryData queryData = processQuery(fieldQueries.get(data.getField()), data.getCaretPosition());
    fieldQueries.put(data.getField(), queryData.query);
    SuggesterQueryBuilder builder = new SuggesterQueryBuilder(data.getField(), queryData.identifier);
    builder.setFreetext(fieldQueries.get(QueryBuilder.FULL)).setDefs(fieldQueries.get(QueryBuilder.DEFS)).setRefs(fieldQueries.get(QueryBuilder.REFS)).setPath(fieldQueries.get(QueryBuilder.PATH)).setHist(fieldQueries.get(QueryBuilder.HIST)).setType(fieldQueries.get(QueryBuilder.TYPE));
    Query query;
    try {
        query = builder.build();
    } catch (ParseException e) {
        // the position might be still wrong if the parse error was at the end of the identifier
        throw new ParseException(e.getMessage().replaceAll(queryData.identifier, ""));
    }
    SuggesterQuery suggesterQuery = builder.getSuggesterQuery();
    // builder can return the suggester query if it was simple query, we ignore it in that case
    if (query.equals(suggesterQuery)) {
        query = null;
    }
    return new SuggesterData(suggesterQuery, projectList, query, builder.getQueryTextWithPlaceholder(), builder.getIdentifier());
}
Also used : Query(org.apache.lucene.search.Query) ParseException(org.apache.lucene.queryparser.classic.ParseException) SuggesterQueryData(org.opengrok.web.api.v1.suggester.model.SuggesterQueryData) QueryBuilder(org.opengrok.indexer.search.QueryBuilder) HashMap(java.util.HashMap) Logger(java.util.logging.Logger) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) Collectors(java.util.stream.Collectors) Level(java.util.logging.Level) List(java.util.List) Map(java.util.Map) RuntimeEnvironment(org.opengrok.indexer.configuration.RuntimeEnvironment) SuggesterData(org.opengrok.web.api.v1.suggester.model.SuggesterData) SuggesterQueryBuilder(org.opengrok.web.api.v1.suggester.query.SuggesterQueryBuilder) LoggerFactory(org.opengrok.indexer.logger.LoggerFactory) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) SuggesterQueryBuilder(org.opengrok.web.api.v1.suggester.query.SuggesterQueryBuilder) Query(org.apache.lucene.search.Query) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) ParseException(org.apache.lucene.queryparser.classic.ParseException) SuggesterData(org.opengrok.web.api.v1.suggester.model.SuggesterData)

Example 2 with SuggesterQuery

use of org.opengrok.suggest.query.SuggesterQuery in project OpenGrok by OpenGrok.

the class SuggesterQueryParser method newBooleanClause.

@Override
protected BooleanClause newBooleanClause(final Query q, final BooleanClause.Occur occur) {
    BooleanClause bc;
    if (q instanceof SuggesterPhraseQuery) {
        bc = super.newBooleanClause(((SuggesterPhraseQuery) q).getPhraseQuery(), BooleanClause.Occur.MUST);
        suggesterClauses.add(bc);
    } else if (q instanceof SuggesterQuery) {
        bc = super.newBooleanClause(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
        suggesterClauses.add(bc);
    } else if (q instanceof BooleanQuery) {
        bc = super.newBooleanClause(q, occur);
        for (BooleanClause clause : ((BooleanQuery) q).clauses()) {
            if (suggesterClauses.contains(clause)) {
                suggesterClauses.add(bc);
            }
        }
    } else {
        bc = super.newBooleanClause(q, occur);
    }
    return bc;
}
Also used : BooleanClause(org.apache.lucene.search.BooleanClause) BooleanQuery(org.apache.lucene.search.BooleanQuery) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) SuggesterPhraseQuery(org.opengrok.suggest.query.SuggesterPhraseQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery)

Example 3 with SuggesterQuery

use of org.opengrok.suggest.query.SuggesterQuery in project OpenGrok by OpenGrok.

the class SuggesterSearcher method suggest.

/**
 * Returns the suggestions for generic {@link SuggesterQuery} (almost all except lone
 * {@link org.opengrok.suggest.query.SuggesterPrefixQuery} for which see {@link SuggesterProjectData}).
 * @param query query on which the suggestions depend
 * @param project name of the project
 * @param suggesterQuery query for the suggestions
 * @param popularityCounter data structure which contains the number of times the terms were searched for. It is
 * used to provide the most popular completion functionality.
 * @return suggestions
 */
public List<LookupResultItem> suggest(final Query query, final String project, final SuggesterQuery suggesterQuery, final PopularityCounter popularityCounter) {
    List<LookupResultItem> results = new ArrayList<>(resultSize * leafContexts.size());
    Query rewrittenQuery = null;
    try {
        if (query != null) {
            rewrittenQuery = query.rewrite(getIndexReader());
        }
    } catch (IOException e) {
        logger.log(Level.WARNING, "Could not rewrite query", e);
        return results;
    }
    for (LeafReaderContext context : this.leafContexts) {
        if (interrupted) {
            break;
        }
        try {
            results.addAll(suggest(rewrittenQuery, context, project, suggesterQuery, popularityCounter));
        } catch (IOException e) {
            logger.log(Level.WARNING, "Cannot perform suggester search", e);
        }
    }
    if (results.size() > resultSize) {
        return SuggesterUtils.combineResults(results, resultSize);
    }
    return results;
}
Also used : Query(org.apache.lucene.search.Query) CustomPhraseQuery(org.opengrok.suggest.query.customized.CustomPhraseQuery) SuggesterRangeQuery(org.opengrok.suggest.query.SuggesterRangeQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ArrayList(java.util.ArrayList) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) IOException(java.io.IOException)

Example 4 with SuggesterQuery

use of org.opengrok.suggest.query.SuggesterQuery in project OpenGrok by OpenGrok.

the class SuggesterSearcher method suggest.

private List<LookupResultItem> suggest(final Query query, final LeafReaderContext leafReaderContext, final String project, final SuggesterQuery suggesterQuery, final PopularityCounter searchCounts) throws IOException {
    if (Thread.currentThread().isInterrupted()) {
        interrupted = true;
        return Collections.emptyList();
    }
    boolean shouldLeaveOutSameTerms = shouldLeaveOutSameTerms(query, suggesterQuery);
    Set<BytesRef> tokensAlreadyIncluded = null;
    if (shouldLeaveOutSameTerms) {
        tokensAlreadyIncluded = SuggesterUtils.intoTermsExceptPhraseQuery(query).stream().filter(t -> t.field().equals(suggesterQuery.getField())).map(Term::bytes).collect(Collectors.toSet());
    }
    boolean needsDocumentIds = query != null && !(query instanceof MatchAllDocsQuery);
    ComplexQueryData complexQueryData = null;
    if (needsDocumentIds) {
        complexQueryData = getComplexQueryData(query, leafReaderContext);
        if (interrupted) {
            return Collections.emptyList();
        }
    }
    Terms terms = leafReaderContext.reader().terms(suggesterQuery.getField());
    TermsEnum termsEnum = suggesterQuery.getTermsEnumForSuggestions(terms);
    LookupPriorityQueue queue = new LookupPriorityQueue(resultSize);
    boolean needPositionsAndFrequencies = needPositionsAndFrequencies(query);
    PostingsEnum postingsEnum = null;
    BytesRef term = termsEnum.next();
    while (term != null) {
        if (Thread.currentThread().isInterrupted()) {
            interrupted = true;
            break;
        }
        if (needPositionsAndFrequencies) {
            postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.POSITIONS | PostingsEnum.FREQS);
        } else {
            postingsEnum = termsEnum.postings(postingsEnum, PostingsEnum.NONE);
        }
        int score = 0;
        if (!needsDocumentIds) {
            score = normalizeDocumentFrequency(termsEnum.docFreq(), numDocs);
        } else if (needPositionsAndFrequencies) {
            score = getPhraseScore(complexQueryData, leafReaderContext.docBase, postingsEnum);
        } else if (complexQueryData != null) {
            score = getDocumentFrequency(complexQueryData.documentIds, leafReaderContext.docBase, postingsEnum);
        }
        if (score > 0) {
            if (!shouldLeaveOutSameTerms || !tokensAlreadyIncluded.contains(term)) {
                score += searchCounts.get(term) * TERM_ALREADY_SEARCHED_MULTIPLIER;
                if (queue.canInsert(score)) {
                    queue.insertWithOverflow(new LookupResultItem(term.utf8ToString(), project, score));
                }
            }
        }
        term = termsEnum.next();
    }
    return queue.getResult();
}
Also used : Query(org.apache.lucene.search.Query) LeafCollector(org.apache.lucene.search.LeafCollector) Term(org.apache.lucene.index.Term) IntsHolder(org.opengrok.suggest.query.data.IntsHolder) Scorable(org.apache.lucene.search.Scorable) CustomPhraseQuery(org.opengrok.suggest.query.customized.CustomPhraseQuery) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) PopularityCounter(org.opengrok.suggest.popular.PopularityCounter) TermsEnum(org.apache.lucene.index.TermsEnum) SuggesterRangeQuery(org.opengrok.suggest.query.SuggesterRangeQuery) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) BitIntsHolder(org.opengrok.suggest.query.data.BitIntsHolder) PostingsEnum(org.apache.lucene.index.PostingsEnum) Terms(org.apache.lucene.index.Terms) Scorer(org.apache.lucene.search.Scorer) BytesRef(org.apache.lucene.util.BytesRef) Set(java.util.Set) IOException(java.io.IOException) Collector(org.apache.lucene.search.Collector) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Logger(java.util.logging.Logger) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) Collectors(java.util.stream.Collectors) BooleanClause(org.apache.lucene.search.BooleanClause) ScoreMode(org.apache.lucene.search.ScoreMode) List(java.util.List) BooleanQuery(org.apache.lucene.search.BooleanQuery) Collections(java.util.Collections) IndexReader(org.apache.lucene.index.IndexReader) PhraseScorer(org.opengrok.suggest.query.PhraseScorer) IndexSearcher(org.apache.lucene.search.IndexSearcher) Terms(org.apache.lucene.index.Terms) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermsEnum(org.apache.lucene.index.TermsEnum) PostingsEnum(org.apache.lucene.index.PostingsEnum) BytesRef(org.apache.lucene.util.BytesRef)

Example 5 with SuggesterQuery

use of org.opengrok.suggest.query.SuggesterQuery in project OpenGrok by OpenGrok.

the class SuggesterSearcher method getComplexQueryData.

private ComplexQueryData getComplexQueryData(final Query query, final LeafReaderContext leafReaderContext) {
    ComplexQueryData data = new ComplexQueryData();
    if (query == null || query instanceof SuggesterQuery) {
        data.documentIds = new BitIntsHolder(0);
        return data;
    }
    BitIntsHolder documentIds = new BitIntsHolder();
    try {
        search(query, new Collector() {

            @Override
            public LeafCollector getLeafCollector(final LeafReaderContext context) {
                return new LeafCollector() {

                    final int docBase = context.docBase;

                    @Override
                    public void setScorer(final Scorable scorer) {
                        if (leafReaderContext == context) {
                            if (scorer instanceof PhraseScorer) {
                                data.scorer = (PhraseScorer) scorer;
                            } else {
                                try {
                                    // in #setScorer but no better way was found
                                    for (Scorer.ChildScorable childScorer : scorer.getChildren()) {
                                        if (childScorer.child instanceof PhraseScorer) {
                                            data.scorer = (PhraseScorer) childScorer.child;
                                        }
                                    }
                                } catch (Exception e) {
                                // ignore
                                }
                            }
                        }
                    }

                    @Override
                    public void collect(int doc) {
                        if (leafReaderContext == context) {
                            documentIds.set(docBase + doc);
                        }
                    }
                };
            }

            @Override
            public ScoreMode scoreMode() {
                return ScoreMode.COMPLETE_NO_SCORES;
            }
        });
    } catch (IOException e) {
        if (Thread.currentThread().isInterrupted()) {
            interrupted = true;
            return null;
        } else {
            logger.log(Level.WARNING, "Could not get document ids for " + query, e);
        }
    } catch (Exception e) {
        logger.log(Level.WARNING, "Could not get document ids for " + query, e);
    }
    data.documentIds = documentIds;
    return data;
}
Also used : ScoreMode(org.apache.lucene.search.ScoreMode) BitIntsHolder(org.opengrok.suggest.query.data.BitIntsHolder) Scorable(org.apache.lucene.search.Scorable) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) IOException(java.io.IOException) PhraseScorer(org.opengrok.suggest.query.PhraseScorer) IOException(java.io.IOException) LeafCollector(org.apache.lucene.search.LeafCollector) LeafCollector(org.apache.lucene.search.LeafCollector) Collector(org.apache.lucene.search.Collector) LeafReaderContext(org.apache.lucene.index.LeafReaderContext)

Aggregations

SuggesterQuery (org.opengrok.suggest.query.SuggesterQuery)5 IOException (java.io.IOException)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Query (org.apache.lucene.search.Query)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Level (java.util.logging.Level)2 Logger (java.util.logging.Logger)2 Collectors (java.util.stream.Collectors)2 BooleanClause (org.apache.lucene.search.BooleanClause)2 Collector (org.apache.lucene.search.Collector)2 LeafCollector (org.apache.lucene.search.LeafCollector)2 Scorable (org.apache.lucene.search.Scorable)2 ScoreMode (org.apache.lucene.search.ScoreMode)2 PhraseScorer (org.opengrok.suggest.query.PhraseScorer)2 SuggesterRangeQuery (org.opengrok.suggest.query.SuggesterRangeQuery)2 CustomPhraseQuery (org.opengrok.suggest.query.customized.CustomPhraseQuery)2 BitIntsHolder (org.opengrok.suggest.query.data.BitIntsHolder)2