Search in sources :

Example 1 with SuggesterPhraseQuery

use of org.opengrok.suggest.query.SuggesterPhraseQuery 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 2 with SuggesterPhraseQuery

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

the class SuggesterQueryParser method newFieldQuery.

@Override
protected Query newFieldQuery(final Analyzer analyzer, final String field, final String queryText, final boolean quoted) throws ParseException {
    if (quoted && queryText.contains(identifier)) {
        List<String> tokens = getAllTokens(analyzer, field, queryText);
        SuggesterPhraseQuery spq = new SuggesterPhraseQuery(field, identifier, tokens, this.getPhraseSlop());
        this.suggesterQuery = spq.getSuggesterQuery();
        replaceIdentifier(field, tokens.stream().filter(t -> t.contains(identifier)).findAny().get());
        return spq;
    }
    return super.newFieldQuery(analyzer, field, queryText, quoted);
}
Also used : Query(org.apache.lucene.search.Query) ParseException(org.apache.lucene.queryparser.classic.ParseException) Term(org.apache.lucene.index.Term) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) SuggesterRegexpQuery(org.opengrok.suggest.query.SuggesterRegexpQuery) SuggesterRangeQuery(org.opengrok.suggest.query.SuggesterRangeQuery) SuggesterFuzzyQuery(org.opengrok.suggest.query.SuggesterFuzzyQuery) SuggesterPhraseQuery(org.opengrok.suggest.query.SuggesterPhraseQuery) SuggesterPrefixQuery(org.opengrok.suggest.query.SuggesterPrefixQuery) LinkedList(java.util.LinkedList) CharTermAttribute(org.apache.lucene.analysis.tokenattributes.CharTermAttribute) TokenStream(org.apache.lucene.analysis.TokenStream) BytesRef(org.apache.lucene.util.BytesRef) Analyzer(org.apache.lucene.analysis.Analyzer) Set(java.util.Set) IOException(java.io.IOException) FuzzyQuery(org.apache.lucene.search.FuzzyQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Logger(java.util.logging.Logger) SuggesterQuery(org.opengrok.suggest.query.SuggesterQuery) CustomQueryParser(org.opengrok.indexer.search.CustomQueryParser) BooleanClause(org.apache.lucene.search.BooleanClause) List(java.util.List) BooleanQuery(org.apache.lucene.search.BooleanQuery) DEFAULT_BOOST(org.apache.lucene.search.BoostAttribute.DEFAULT_BOOST) SuggesterWildcardQuery(org.opengrok.suggest.query.SuggesterWildcardQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Pattern(java.util.regex.Pattern) SuggesterPhraseQuery(org.opengrok.suggest.query.SuggesterPhraseQuery)

Example 3 with SuggesterPhraseQuery

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

the class SuggesterSearcherTest method suggesterPhraseQueryTest.

@Test
public void suggesterPhraseQueryTest() {
    SuggesterPhraseQuery q = new SuggesterPhraseQuery("test", "abc", Arrays.asList("opengrok", "openabc"), 0);
    List<LookupResultItem> suggestions = searcher.suggest(q.getPhraseQuery(), "test", q.getSuggesterQuery(), k -> 0);
    List<String> tokens = suggestions.stream().map(LookupResultItem::getPhrase).collect(Collectors.toList());
    assertThat(tokens, contains("opengrok2"));
}
Also used : SuggesterPhraseQuery(org.opengrok.suggest.query.SuggesterPhraseQuery) Test(org.junit.jupiter.api.Test)

Aggregations

SuggesterPhraseQuery (org.opengrok.suggest.query.SuggesterPhraseQuery)3 BooleanClause (org.apache.lucene.search.BooleanClause)2 BooleanQuery (org.apache.lucene.search.BooleanQuery)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 SuggesterQuery (org.opengrok.suggest.query.SuggesterQuery)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Set (java.util.Set)1 Level (java.util.logging.Level)1 Logger (java.util.logging.Logger)1 Pattern (java.util.regex.Pattern)1 Analyzer (org.apache.lucene.analysis.Analyzer)1 TokenStream (org.apache.lucene.analysis.TokenStream)1 CharTermAttribute (org.apache.lucene.analysis.tokenattributes.CharTermAttribute)1 Term (org.apache.lucene.index.Term)1 ParseException (org.apache.lucene.queryparser.classic.ParseException)1 DEFAULT_BOOST (org.apache.lucene.search.BoostAttribute.DEFAULT_BOOST)1