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;
}
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);
}
}
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();
}
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);
}
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"));
}
Aggregations