Search in sources :

Example 96 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class TestMoreLikeThis method getOriginalValues.

private Map<String, Float> getOriginalValues() throws IOException {
    Map<String, Float> originalValues = new HashMap<>();
    MoreLikeThis mlt = new MoreLikeThis(reader);
    Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false);
    mlt.setAnalyzer(analyzer);
    mlt.setMinDocFreq(1);
    mlt.setMinTermFreq(1);
    mlt.setMinWordLen(1);
    mlt.setFieldNames(new String[] { "text" });
    mlt.setBoost(true);
    BooleanQuery query = (BooleanQuery) mlt.like("text", new StringReader("lucene release"));
    Collection<BooleanClause> clauses = query.clauses();
    for (BooleanClause clause : clauses) {
        BoostQuery bq = (BoostQuery) clause.getQuery();
        TermQuery tq = (TermQuery) bq.getQuery();
        originalValues.put(tq.getTerm().text(), bq.getBoost());
    }
    analyzer.close();
    return originalValues;
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) HashMap(java.util.HashMap) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) BoostQuery(org.apache.lucene.search.BoostQuery) BooleanClause(org.apache.lucene.search.BooleanClause) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) StringReader(java.io.StringReader)

Example 97 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class TestFunctionScoreQuery method testBoostsAreAppliedLast.

// check boosts with non-distributive score source
public void testBoostsAreAppliedLast() throws Exception {
    DoubleValuesSource scores = DoubleValuesSource.function(DoubleValuesSource.SCORES, "ln(v + 4)", v -> Math.log(v + 4));
    Query q1 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "text")), scores);
    TopDocs plain = searcher.search(q1, 5);
    Query boosted = new BoostQuery(q1, 2);
    TopDocs afterboost = searcher.search(boosted, 5);
    assertEquals(plain.totalHits, afterboost.totalHits);
    for (int i = 0; i < 5; i++) {
        assertEquals(plain.scoreDocs[i].doc, afterboost.scoreDocs[i].doc);
        assertEquals(plain.scoreDocs[i].score, afterboost.scoreDocs[i].score / 2, 0.0001);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Term(org.apache.lucene.index.Term) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 98 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class FastVectorHighlighterTest method testBoostedPhraseHighlightTest.

public void testBoostedPhraseHighlightTest() throws IOException {
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig(new MockAnalyzer(random())));
    Document doc = new Document();
    FieldType type = new FieldType(TextField.TYPE_STORED);
    type.setStoreTermVectorOffsets(true);
    type.setStoreTermVectorPositions(true);
    type.setStoreTermVectors(true);
    type.freeze();
    StringBuilder text = new StringBuilder();
    text.append("words words junk junk junk junk junk junk junk junk highlight junk junk junk junk together junk ");
    for (int i = 0; i < 10; i++) {
        text.append("junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk ");
    }
    text.append("highlight words together ");
    for (int i = 0; i < 10; i++) {
        text.append("junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk junk ");
    }
    doc.add(new Field("text", text.toString().trim(), type));
    writer.addDocument(doc);
    FastVectorHighlighter highlighter = new FastVectorHighlighter();
    IndexReader reader = DirectoryReader.open(writer);
    // This mimics what some query parsers do to <highlight words together>
    BooleanQuery.Builder terms = new BooleanQuery.Builder();
    terms.add(clause("text", "highlight"), Occur.MUST);
    terms.add(clause("text", "words"), Occur.MUST);
    terms.add(clause("text", "together"), Occur.MUST);
    // This mimics what some query parsers do to <"highlight words together">
    BooleanQuery.Builder phraseB = new BooleanQuery.Builder();
    phraseB.add(clause("text", "highlight", "words", "together"), Occur.MUST);
    Query phrase = phraseB.build();
    phrase = new BoostQuery(phrase, 100f);
    // Now combine those results in a boolean query which should pull the phrases to the front of the list of fragments 
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(phrase, Occur.MUST);
    query.add(phrase, Occur.SHOULD);
    FieldQuery fieldQuery = new FieldQuery(query.build(), reader, true, false);
    String fragment = highlighter.getBestFragment(fieldQuery, reader, 0, "text", 100);
    assertEquals("junk junk junk junk junk junk junk junk <b>highlight words together</b> junk junk junk junk junk junk junk junk", fragment);
    reader.close();
    writer.close();
    dir.close();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) CommonTermsQuery(org.apache.lucene.queries.CommonTermsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) CustomScoreQuery(org.apache.lucene.queries.CustomScoreQuery) TermQuery(org.apache.lucene.search.TermQuery) SynonymQuery(org.apache.lucene.search.SynonymQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Document(org.apache.lucene.document.Document) BoostQuery(org.apache.lucene.search.BoostQuery) FieldType(org.apache.lucene.document.FieldType) StoredField(org.apache.lucene.document.StoredField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) Directory(org.apache.lucene.store.Directory)

Example 99 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class TestUnifiedHighlighterStrictPhrases method testPreSpanQueryRewrite.

public void testPreSpanQueryRewrite() throws IOException {
    indexWriter.addDocument(newDoc("There is no accord and satisfaction with this - Consideration of the accord is arbitrary."));
    initReaderSearcherHighlighter();
    highlighter = new UnifiedHighlighter(searcher, indexAnalyzer) {

        @Override
        protected Collection<Query> preSpanQueryRewrite(Query query) {
            if (query instanceof MyQuery) {
                return Collections.singletonList(((MyQuery) query).wrapped);
            }
            return null;
        }
    };
    highlighter.setHighlightPhrasesStrictly(true);
    BooleanQuery.Builder bqBuilder = new BooleanQuery.Builder();
    Query phraseQuery = new BoostQuery(new PhraseQuery("body", "accord", "and", "satisfaction"), 2.0f);
    Query oredTerms = new BooleanQuery.Builder().setMinimumNumberShouldMatch(2).add(new TermQuery(new Term("body", "accord")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("body", "satisfaction")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term("body", "consideration")), BooleanClause.Occur.SHOULD).build();
    Query proximityBoostingQuery = new MyQuery(oredTerms);
    Query totalQuery = bqBuilder.add(phraseQuery, BooleanClause.Occur.SHOULD).add(proximityBoostingQuery, BooleanClause.Occur.SHOULD).build();
    TopDocs topDocs = searcher.search(totalQuery, 10, Sort.INDEXORDER);
    assertEquals(1, topDocs.totalHits);
    String[] snippets = highlighter.highlight("body", totalQuery, topDocs);
    assertArrayEquals(new String[] { "There is no <b>accord</b> <b>and</b> <b>satisfaction</b> with this - <b>Consideration</b> of the <b>accord</b> is arbitrary." }, snippets);
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanNearQuery(org.apache.lucene.search.spans.SpanNearQuery) PrefixQuery(org.apache.lucene.search.PrefixQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) PhraseQuery(org.apache.lucene.search.PhraseQuery) MultiPhraseQuery(org.apache.lucene.search.MultiPhraseQuery) QueryBuilder(org.apache.lucene.util.QueryBuilder) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) TopDocs(org.apache.lucene.search.TopDocs) Collection(java.util.Collection)

Example 100 with BoostQuery

use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.

the class FieldQueryTest method testFlattenTermAndPhrase.

public void testFlattenTermAndPhrase() throws Exception {
    initBoost();
    BooleanQuery.Builder booleanQueryB = new BooleanQuery.Builder();
    booleanQueryB.add(tq("A"), Occur.MUST);
    booleanQueryB.add(pqF("B", "C"), Occur.MUST);
    Query booleanQuery = booleanQueryB.build();
    booleanQuery = new BoostQuery(booleanQuery, boost);
    FieldQuery fq = new FieldQuery(booleanQuery, true, true);
    Set<Query> flatQueries = new HashSet<>();
    fq.flatten(booleanQuery, reader, flatQueries, 1f);
    assertCollectionQueries(flatQueries, tq(boost, "A"), pqF(boost, "B", "C"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashSet(java.util.HashSet)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)128 Query (org.apache.lucene.search.Query)107 BooleanQuery (org.apache.lucene.search.BooleanQuery)96 TermQuery (org.apache.lucene.search.TermQuery)84 Term (org.apache.lucene.index.Term)54 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)45 PhraseQuery (org.apache.lucene.search.PhraseQuery)35 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)32 PrefixQuery (org.apache.lucene.search.PrefixQuery)29 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)24 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)23 BooleanClause (org.apache.lucene.search.BooleanClause)20 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 ArrayList (java.util.ArrayList)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)18 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16