use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestScoreJoinQPScore method testBoost.
@Ignore("SOLR-7814, also don't forget cover boost at testCacheHit()")
public void testBoost() throws Exception {
indexDataForScorring();
ScoreMode score = ScoreMode.values()[random().nextInt(ScoreMode.values().length)];
final SolrQueryRequest req = req("q", "{!join from=movieId_s to=id score=" + score + " b=200}title:movie", "fl", "id,score", "omitHeader", "true");
SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, new SolrQueryResponse()));
final Query luceneQ = QParser.getParser(req.getParams().get("q"), req).getQuery().rewrite(req.getSearcher().getSlowAtomicReader());
assertTrue(luceneQ instanceof BoostQuery);
float boost = ((BoostQuery) luceneQ).getBoost();
assertEquals("" + luceneQ, Float.floatToIntBits(200), Float.floatToIntBits(boost));
SolrRequestInfo.clearRequestInfo();
req.close();
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestBooleanSimilarity method testTermScoreIsEqualToBoost.
public void testTermScoreIsEqualToBoost() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig());
Document doc = new Document();
doc.add(new StringField("foo", "bar", Store.NO));
doc.add(new StringField("foo", "baz", Store.NO));
w.addDocument(doc);
doc = new Document();
doc.add(new StringField("foo", "bar", Store.NO));
doc.add(new StringField("foo", "bar", Store.NO));
w.addDocument(doc);
DirectoryReader reader = w.getReader();
w.close();
IndexSearcher searcher = newSearcher(reader);
searcher.setSimilarity(new BooleanSimilarity());
TopDocs topDocs = searcher.search(new TermQuery(new Term("foo", "bar")), 2);
assertEquals(2, topDocs.totalHits);
assertEquals(1f, topDocs.scoreDocs[0].score, 0f);
assertEquals(1f, topDocs.scoreDocs[1].score, 0f);
topDocs = searcher.search(new TermQuery(new Term("foo", "baz")), 1);
assertEquals(1, topDocs.totalHits);
assertEquals(1f, topDocs.scoreDocs[0].score, 0f);
topDocs = searcher.search(new BoostQuery(new TermQuery(new Term("foo", "baz")), 3f), 1);
assertEquals(1, topDocs.totalHits);
assertEquals(3f, topDocs.scoreDocs[0].score, 0f);
reader.close();
dir.close();
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestTaxonomyFacetSumValueSource method testSumScoreAggregator.
public void testSumScoreAggregator() throws Exception {
Directory indexDir = newDirectory();
Directory taxoDir = newDirectory();
DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir);
IndexWriter iw = new IndexWriter(indexDir, newIndexWriterConfig(new MockAnalyzer(random())));
FacetsConfig config = new FacetsConfig();
for (int i = atLeast(30); i > 0; --i) {
Document doc = new Document();
if (random().nextBoolean()) {
// don't match all documents
doc.add(new StringField("f", "v", Field.Store.NO));
}
doc.add(new FacetField("dim", "a"));
iw.addDocument(config.build(taxoWriter, doc));
}
DirectoryReader r = DirectoryReader.open(iw);
DirectoryTaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
FacetsCollector fc = new FacetsCollector(true);
BoostQuery csq = new BoostQuery(new ConstantScoreQuery(new MatchAllDocsQuery()), 2f);
TopDocs td = FacetsCollector.search(newSearcher(r), csq, 10, fc);
Facets facets = new TaxonomyFacetSumValueSource(taxoReader, config, fc, DoubleValuesSource.SCORES);
int expected = (int) (td.getMaxScore() * td.totalHits);
assertEquals(expected, facets.getSpecificValue("dim", "a").intValue());
iw.close();
IOUtils.close(taxoWriter, taxoReader, taxoDir, r, indexDir);
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class BoostQueryNodeBuilder method build.
@Override
public Query build(QueryNode queryNode) throws QueryNodeException {
BoostQueryNode boostNode = (BoostQueryNode) queryNode;
QueryNode child = boostNode.getChild();
if (child == null) {
return null;
}
Query query = (Query) child.getTag(QueryTreeBuilder.QUERY_TREE_BUILDER_TAGID);
return new BoostQuery(query, boostNode.getValue());
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class MultiFieldQueryParser method getFieldQuery.
@Override
protected Query getFieldQuery(String field, String queryText, boolean quoted) throws ParseException {
if (field == null) {
List<Query> clauses = new ArrayList<>();
Query[] fieldQueries = new Query[fields.length];
int maxTerms = 0;
for (int i = 0; i < fields.length; i++) {
Query q = super.getFieldQuery(fields[i], queryText, quoted);
if (q != null) {
if (q instanceof BooleanQuery) {
maxTerms = Math.max(maxTerms, ((BooleanQuery) q).clauses().size());
} else {
maxTerms = Math.max(1, maxTerms);
}
fieldQueries[i] = q;
}
}
for (int termNum = 0; termNum < maxTerms; termNum++) {
List<Query> termClauses = new ArrayList<>();
for (int i = 0; i < fields.length; i++) {
if (fieldQueries[i] != null) {
Query q = null;
if (fieldQueries[i] instanceof BooleanQuery) {
List<BooleanClause> nestedClauses = ((BooleanQuery) fieldQueries[i]).clauses();
if (termNum < nestedClauses.size()) {
q = nestedClauses.get(termNum).getQuery();
}
} else if (termNum == 0) {
// e.g. TermQuery-s
q = fieldQueries[i];
}
if (q != null) {
if (boosts != null) {
//Get the boost from the map and apply them
Float boost = boosts.get(fields[i]);
if (boost != null) {
q = new BoostQuery(q, boost);
}
}
termClauses.add(q);
}
}
}
if (maxTerms > 1) {
if (termClauses.size() > 0) {
BooleanQuery.Builder builder = newBooleanQuery();
for (Query termClause : termClauses) {
builder.add(termClause, BooleanClause.Occur.SHOULD);
}
clauses.add(builder.build());
}
} else {
clauses.addAll(termClauses);
}
}
if (// happens for stopwords
clauses.size() == 0)
return null;
return getMultiFieldQuery(clauses);
}
Query q = super.getFieldQuery(field, queryText, quoted);
return q;
}
Aggregations