Skip to content

Commit

Permalink
Add support for boosting Lucene queries
Browse files Browse the repository at this point in the history
  • Loading branch information
Shredder121 committed Apr 4, 2016
1 parent 5cdb0e3 commit eebf53e
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 6 deletions.
@@ -0,0 +1,35 @@
/*
* Copyright 2016, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.lucene3;

import com.querydsl.core.types.ConstantImpl;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.dsl.BooleanOperation;

/**
* {@code BoostElement} is an element that allows boosting a Query.
*
* @author Shredder121
*/
public class BoostElement extends BooleanOperation {

private static final long serialVersionUID = -5663346955776953048L;

public BoostElement(Expression<Boolean> predicate, float boostFactor) {
super(LuceneOps.BOOST, predicate, ConstantImpl.create(boostFactor));
}

}
Expand Up @@ -17,6 +17,7 @@
import org.apache.lucene.search.FuzzyQuery;

import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;

/**
Expand All @@ -28,6 +29,17 @@
*/
public final class LuceneExpressions {

/**
* Create a boost query.
*
* @param predicate
* @param boost
* @return
*/
public static BooleanExpression boost(Predicate predicate, float boost) {
return new BoostElement(predicate, boost);
}

/**
* Create a fuzzy query
*
Expand Down
Expand Up @@ -22,6 +22,7 @@
*
*/
public enum LuceneOps implements Operator {
BOOST(Boolean.class),
LUCENE_QUERY(Object.class),
PHRASE(String.class),
TERM(String.class);
Expand Down
Expand Up @@ -122,6 +122,13 @@ private Query toQuery(Operation<?> operation, QueryMetadata metadata) {
@SuppressWarnings("unchecked") //This is the expected type
Query rv = ((Constant<Query>) operation.getArg(0)).getConstant();
return rv;
} else if (op == LuceneOps.BOOST) {
@SuppressWarnings("unchecked") //this is the expected type
Constant<Float> boostFactor = (Constant<Float>) operation.getArg(1);

Query query = toQuery(operation.getArg(0), metadata);
query.setBoost(boostFactor.getConstant());
return query;
}
throw new UnsupportedOperationException("Illegal operation " + operation);
}
Expand Down
Expand Up @@ -629,9 +629,8 @@ public void proximity() throws Exception {
}

@Test
@Ignore
public void boost() throws Exception {
fail("Not yet implemented!");
testQuery(LuceneExpressions.boost(titles.any().eq("Jurassic"), 2), "title:jurassic^2.0", 1);
}

@Test
Expand Down
@@ -0,0 +1,35 @@
/*
* Copyright 2016, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.lucene4;

import com.querydsl.core.types.ConstantImpl;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.dsl.BooleanOperation;

/**
* {@code BoostElement} is an element that allows boosting a Query.
*
* @author Shredder121
*/
public class BoostElement extends BooleanOperation {

private static final long serialVersionUID = -5663346955776953048L;

public BoostElement(Expression<Boolean> predicate, float boostFactor) {
super(LuceneOps.BOOST, predicate, ConstantImpl.create(boostFactor));
}

}
Expand Up @@ -18,6 +18,7 @@
import org.apache.lucene.util.automaton.LevenshteinAutomata;

import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;

/**
Expand All @@ -29,6 +30,17 @@
*/
public final class LuceneExpressions {

/**
* Create a boost query.
*
* @param predicate
* @param boost
* @return
*/
public static BooleanExpression boost(Predicate predicate, float boost) {
return new BoostElement(predicate, boost);
}

/**
* Create a fuzzy query
*
Expand Down
Expand Up @@ -22,6 +22,7 @@
*
*/
public enum LuceneOps implements Operator {
BOOST(Boolean.class),
LUCENE_QUERY(Object.class),
PHRASE(String.class),
TERM(String.class);
Expand Down
Expand Up @@ -123,6 +123,13 @@ private Query toQuery(Operation<?> operation, QueryMetadata metadata) {
@SuppressWarnings("unchecked") //this is the expected type
Constant<Query> expectedConstant = (Constant<Query>) operation.getArg(0);
return expectedConstant.getConstant();
} else if (op == LuceneOps.BOOST) {
@SuppressWarnings("unchecked") //this is the expected type
Constant<Float> boostFactor = (Constant<Float>) operation.getArg(1);

Query query = toQuery(operation.getArg(0), metadata);
query.setBoost(boostFactor.getConstant());
return query;
}
throw new UnsupportedOperationException("Illegal operation " + operation);
}
Expand Down
Expand Up @@ -631,9 +631,8 @@ public void proximity() throws Exception {
}

@Test
@Ignore
public void boost() throws Exception {
fail("Not yet implemented!");
testQuery(LuceneExpressions.boost(titles.any().eq("Jurassic"), 2), "title:jurassic^2.0", 1);
}

@Test
Expand Down
@@ -0,0 +1,35 @@
/*
* Copyright 2016, The Querydsl Team (http://www.querydsl.com/team)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.querydsl.lucene5;

import com.querydsl.core.types.ConstantImpl;
import com.querydsl.core.types.Expression;
import com.querydsl.core.types.dsl.BooleanOperation;

/**
* {@code BoostElement} is an element that allows boosting a Query.
*
* @author Shredder121
*/
public class BoostElement extends BooleanOperation {

private static final long serialVersionUID = -5663346955776953048L;

public BoostElement(Expression<Boolean> predicate, float boostFactor) {
super(LuceneOps.BOOST, predicate, ConstantImpl.create(boostFactor));
}

}
Expand Up @@ -18,6 +18,7 @@
import org.apache.lucene.util.automaton.LevenshteinAutomata;

import com.querydsl.core.types.Path;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.BooleanExpression;

/**
Expand All @@ -29,6 +30,17 @@
*/
public final class LuceneExpressions {

/**
* Create a boost query.
*
* @param predicate
* @param boost
* @return
*/
public static BooleanExpression boost(Predicate predicate, float boost) {
return new BoostElement(predicate, boost);
}

/**
* Create a fuzzy query
*
Expand Down
Expand Up @@ -22,6 +22,7 @@
*
*/
public enum LuceneOps implements Operator {
BOOST(Boolean.class),
LUCENE_QUERY(Object.class), PHRASE(String.class), TERM(String.class);

private final Class<?> type;
Expand Down
Expand Up @@ -156,6 +156,13 @@ private Query toQuery(Operation<?> operation, QueryMetadata metadata) {
Constant<Query> expectedConstant = (Constant<Query>) operation
.getArg(0);
return expectedConstant.getConstant();
} else if (op == LuceneOps.BOOST) {
@SuppressWarnings("unchecked") //this is the expected type
Constant<Float> boostFactor = (Constant<Float>) operation.getArg(1);

Query query = toQuery(operation.getArg(0), metadata);
query.setBoost(boostFactor.getConstant());
return query;
}
throw new UnsupportedOperationException("Illegal operation "
+ operation);
Expand Down
Expand Up @@ -706,9 +706,8 @@ public void proximity() throws Exception {
}

@Test
@Ignore
public void boost() throws Exception {
fail("Not yet implemented!");
testQuery(LuceneExpressions.boost(titles.any().eq("Jurassic"), 2), "title:jurassic^2.0", 1);
}

@Test
Expand Down

0 comments on commit eebf53e

Please sign in to comment.