Permalink
Browse files

Start adding support for ES boost queries

  • Loading branch information...
1 parent 4c42ed8 commit 260d386155a16300353a7f7921b012cb142b66c5 Holden Karau committed Jun 17, 2012
Showing with 49 additions and 3 deletions.
  1. +49 −3 src/main/scala/com/foursquare/slashem/Schema.scala
@@ -29,9 +29,12 @@ import org.elasticsearch.common.settings.ImmutableSettings
import org.elasticsearch.common.transport.InetSocketTransportAddress
import org.elasticsearch.common.unit.TimeValue
import org.elasticsearch.index.query.QueryBuilders.filteredQuery
-import org.elasticsearch.index.query.{AndFilterBuilder, CustomScoreQueryBuilder,
+import org.elasticsearch.index.query.{AndFilterBuilder,
+ BoostingQueryBuilder,
+ CustomScoreQueryBuilder,
FilterBuilder => ElasticFilterBuilder,
- QueryBuilder => ElasticQueryBuilder}
+ QueryBuilder => ElasticQueryBuilder,
+ QueryBuilders => EQueryBuilders}
import org.elasticsearch.node.Node
import org.elasticsearch.node.NodeBuilder._
import org.elasticsearch.search.facet.AbstractFacetBuilder
@@ -581,6 +584,7 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
.onFailure(e => meta.logger.failure("e"+meta.indexName, queryText, e))
}
+
def constructSearchResults[Y](creator: Option[Response.RawDoc => Y],
start: Int,
fallOff: Option[Double],
@@ -640,7 +644,12 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
}
case _ => scoreFields(fq, qb.boostFields)
}
- scoredQuery
+ //Apply query boosting
+ val boostedQuery = qb.boostQueries match {
+ case (x::xs) => boostQueries(scoredQuery, qb)
+ case _ => scoredQuery
+ }
+ boostedQuery
}
def termFacetQuery(facetFields: List[Ast.Field], facetLimit: Option[Int]): List[AbstractFacetBuilder] = {
@@ -658,6 +667,42 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
}
/**
+ * Applies positive and negative query boosts
+ */
+ def boostQueries(query: ElasticQueryBuilder, qb: QueryBuilder[_, _, _, _, _, _, _, _, _, _]): ElasticQueryBuilder = {
+ //Only bother making boost queries if we have negative boost queries otherwise we just append it
+ val boostQueries = qb.boostQueries
+ val negativeQueries = boostQueries.filter(q => q match {
+ case Clause(_,_,false) => true
+ case _ => false
+ })
+ if (negativeQueries.length > 0) {
+ val boostedQuery = new BoostingQueryBuilder()
+ boostedQuery.positive(query)
+ boostedQuery.negative(query)
+ boostQueries.map(q => q match {
+ case Clause(_,_,false) => boostedQuery.negative(q.elasticExtend(qb.queryFields,
+ qb.phraseBoostFields,
+ qb.minimumMatch))
+ case _ => boostedQuery.positive(q.elasticExtend(qb.queryFields,
+ qb.phraseBoostFields,
+ qb.minimumMatch))
+ })
+ boostedQuery.negativeBoost(0.1.toFloat)
+ boostedQuery
+ } else {
+ val orQuery = EQueryBuilders.boolQuery
+ orQuery.should(query)
+ boostQueries.map(q => orQuery.should(q.elasticExtend(qb.queryFields,
+ qb.phraseBoostFields,
+ qb.minimumMatch)
+ )
+ )
+ orQuery
+ }
+ }
+
+ /**
* Custom score the fields which have scoreboosts
*/
def scoreFields(query: ElasticQueryBuilder, fieldsToScore: List[ScoreBoost]): ElasticQueryBuilder = {
@@ -671,6 +716,7 @@ trait ElasticSchema[M <: Record[M]] extends SlashemSchema[M] {
scoreWithScript(query, scoreScript, namesAndParams, false)
}
+
/**
* Add the provided script and its params to the query and build a
* CustomScoreQuery with it.

0 comments on commit 260d386

Please sign in to comment.