Skip to content

Commit 22ca8d8

Browse files
committed
feat(search-engine): Support for boosting queries
1 parent 731d7bf commit 22ca8d8

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package se.l4.silo.index.search.internal;
2+
3+
import java.util.Objects;
4+
5+
import se.l4.silo.index.search.QueryClause;
6+
import se.l4.silo.index.search.query.BoostQuery;
7+
8+
/**
9+
* Implementation of {@link BoostQuery}.
10+
*/
11+
public class BoostQueryImpl
12+
implements BoostQuery
13+
{
14+
private final QueryClause clause;
15+
private final float boost;
16+
17+
public BoostQueryImpl(
18+
QueryClause clause,
19+
float boost
20+
)
21+
{
22+
this.clause = clause;
23+
this.boost = boost;
24+
}
25+
26+
@Override
27+
public QueryClause getClause()
28+
{
29+
return clause;
30+
}
31+
32+
@Override
33+
public float getBoost()
34+
{
35+
return boost;
36+
}
37+
38+
@Override
39+
public int hashCode()
40+
{
41+
return Objects.hash(boost, clause);
42+
}
43+
44+
@Override
45+
public boolean equals(Object obj)
46+
{
47+
if(this == obj) return true;
48+
if(obj == null) return false;
49+
if(getClass() != obj.getClass()) return false;
50+
BoostQueryImpl other = (BoostQueryImpl) obj;
51+
return Float.floatToIntBits(boost) == Float.floatToIntBits(other.boost)
52+
&& Objects.equals(clause, other.clause);
53+
}
54+
55+
@Override
56+
public String toString()
57+
{
58+
return "BoostQuery{boost=" + boost + ", clause=" + clause + "}";
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package se.l4.silo.index.search.query;
2+
3+
import se.l4.silo.index.search.QueryClause;
4+
import se.l4.silo.index.search.internal.BoostQueryImpl;
5+
6+
/**
7+
* {@link QueryClause} to increase or lower the important of another clause.
8+
*/
9+
public interface BoostQuery
10+
extends QueryClause
11+
{
12+
/**
13+
* The clause to apply a boost to.
14+
*
15+
* @return
16+
*/
17+
QueryClause getClause();
18+
19+
/**
20+
* Boost to apply. Less than one will reduce the importance of the clause,
21+
* more than one will increase the important of the clause.
22+
*
23+
* @return
24+
*/
25+
float getBoost();
26+
27+
/**
28+
* Create an instance that will boost the given clause.
29+
*
30+
* @param clause
31+
* clause that will be boosted
32+
* @param boost
33+
* boost to apply. Less than one will reduce the importance of the clause,
34+
* more than one will increase the important of the clause.
35+
* @return
36+
*/
37+
static BoostQuery create(QueryClause clause, float boost)
38+
{
39+
return new BoostQueryImpl(clause, boost);
40+
}
41+
}
42+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package se.l4.silo.engine.index.search.internal.query;
2+
3+
import java.io.IOException;
4+
5+
import org.apache.lucene.search.Query;
6+
7+
import se.l4.silo.engine.index.search.query.QueryBuilder;
8+
import se.l4.silo.engine.index.search.query.QueryEncounter;
9+
import se.l4.silo.index.search.query.BoostQuery;
10+
11+
/**
12+
* Builder that handles {@link BoostQuery}.
13+
*/
14+
public class BoostQueryBuilder
15+
implements QueryBuilder<BoostQuery>
16+
{
17+
@Override
18+
public Query parse(QueryEncounter<BoostQuery> encounter)
19+
throws IOException
20+
{
21+
BoostQuery instance = encounter.clause();
22+
return new org.apache.lucene.search.BoostQuery(
23+
encounter.parse(instance.getClause()),
24+
instance.getBoost()
25+
);
26+
}
27+
}

silo-search-engine/src/main/java/se/l4/silo/engine/index/search/internal/query/QueryBuildersImpl.java

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class QueryBuildersImpl
2222
.add(new OrQueryBuilder())
2323
.add(new FieldQueryBuilder())
2424
.add(new NegateQueryBuilder())
25+
.add(new BoostQueryBuilder())
2526
.add(new UserQueryBuilder());
2627

2728
public static final QueryBuilders DEFAULT = DEFAULT_BUILDER.build();

0 commit comments

Comments
 (0)