This repository has been archived by the owner on May 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added 'maxClauses' to boolean search (#338)
- Loading branch information
Eduardo Alonso
authored
Jul 4, 2017
1 parent
fd5ca1c
commit e92acf9
Showing
8 changed files
with
164 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,14 +30,14 @@ | |
import static org.apache.lucene.search.BooleanClause.Occur.*; | ||
|
||
/** | ||
* A {@link Condition} that matches documents matching boolean combinations of other queries, e.g. {@link | ||
* MatchCondition}s, {@link RangeCondition}s or other {@link BooleanCondition}s. | ||
* A {@link Condition} that matches documents matching boolean combinations of other queries, e.g. {@link MatchCondition}s, {@link RangeCondition}s or other {@link BooleanCondition}s. | ||
* | ||
* @author Andres de la Pena {@literal <[email protected]>} | ||
*/ | ||
public class BooleanCondition extends Condition { | ||
|
||
protected static final Logger logger = LoggerFactory.getLogger(BooleanCondition.class); | ||
static final Integer DEFAULT_MAX_CLAUSES = 1024; | ||
|
||
/** The mandatory conditions. */ | ||
public final List<Condition> must; | ||
|
@@ -48,28 +48,36 @@ public class BooleanCondition extends Condition { | |
/** The mandatory not conditions. */ | ||
public final List<Condition> not; | ||
|
||
/** The max boolean query clauses. */ | ||
final Integer maxClauses; | ||
|
||
/** | ||
* Returns a new {@link BooleanCondition} compound by the specified {@link Condition}s. | ||
* | ||
* @param boost The boost for this query clause. Documents matching this clause will (in addition to the normal | ||
* weightings) have their score multiplied by {@code boost}. | ||
* @param boost The boost for this query clause. Documents matching this clause will (in addition to the normal weightings) have their score multiplied by {@code boost}. | ||
* @param must the mandatory conditions | ||
* @param should the optional conditions | ||
* @param not the mandatory not conditions | ||
* @param maxClauses teh booleanQuery allowed max clauses | ||
*/ | ||
public BooleanCondition(Float boost, | ||
List<Condition> must, | ||
List<Condition> should, | ||
List<Condition> not) { | ||
List<Condition> not, | ||
Integer maxClauses) { | ||
super(boost); | ||
this.must = must == null ? Collections.EMPTY_LIST : must; | ||
this.should = should == null ? Collections.EMPTY_LIST : should; | ||
this.not = not == null ? Collections.EMPTY_LIST : not; | ||
this.maxClauses = maxClauses == null ? DEFAULT_MAX_CLAUSES : maxClauses; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
@Override | ||
public BooleanQuery doQuery(Schema schema) { | ||
public synchronized BooleanQuery doQuery(Schema schema) { | ||
int oldMaxClauses= BooleanQuery.getMaxClauseCount(); | ||
BooleanQuery.setMaxClauseCount(maxClauses); | ||
|
||
BooleanQuery.Builder builder = new BooleanQuery.Builder(); | ||
must.forEach(condition -> builder.add(condition.query(schema), MUST)); | ||
should.forEach(condition -> builder.add(condition.query(schema), SHOULD)); | ||
|
@@ -78,7 +86,9 @@ public BooleanQuery doQuery(Schema schema) { | |
logger.warn("Performing resource-intensive pure negation query {}", this); | ||
builder.add(new MatchAllDocsQuery(), FILTER); | ||
} | ||
return builder.build(); | ||
BooleanQuery out=builder.build(); | ||
BooleanQuery.setMaxClauseCount(oldMaxClauses); | ||
return out; | ||
} | ||
|
||
/** {@inheritDoc} */ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
...T/src/test/java/com/stratio/cassandra/lucene/testsAT/varia/TestBooleanExtremeClauses.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package com.stratio.cassandra.lucene.testsAT.varia; | ||
|
||
import com.stratio.cassandra.lucene.builder.search.condition.BooleanCondition; | ||
import com.stratio.cassandra.lucene.testsAT.BaseIT; | ||
import com.stratio.cassandra.lucene.testsAT.util.CassandraUtils; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import static com.stratio.cassandra.lucene.builder.Builder.all; | ||
import static com.stratio.cassandra.lucene.builder.Builder.bool; | ||
import static com.stratio.cassandra.lucene.builder.Builder.range; | ||
|
||
/** | ||
* @author Eduardo Alonso {@literal <[email protected]>} | ||
*/ | ||
@RunWith(JUnit4.class) | ||
public class TestBooleanExtremeClauses extends BaseIT { | ||
|
||
private static final int NUM_PARTITIONS = 100; | ||
private static final int NUM_MAX_CLAUSES = 1000; | ||
private static CassandraUtils utils; | ||
|
||
@BeforeClass | ||
public static void before() { | ||
utils = CassandraUtils.builder("stateless_search_skinny") | ||
.withPartitionKey("pk") | ||
.withColumn("pk", "int") | ||
.withColumn("rc", "int") | ||
.build() | ||
.createKeyspace() | ||
.createTable() | ||
.createIndex(); | ||
for (Integer i = 0; i < NUM_PARTITIONS; i++) { | ||
Map<String, String> data = new LinkedHashMap<>(); | ||
data.put("pk", i.toString()); | ||
data.put("rc", i.toString()); | ||
utils.insert(data); | ||
} | ||
utils.refresh(); | ||
} | ||
|
||
@AfterClass | ||
public static void after() { | ||
CassandraUtils.dropKeyspaceIfNotNull(utils); | ||
} | ||
|
||
@Test | ||
public void testQuery() throws Exception { | ||
BooleanCondition bool= bool().maxClauses(NUM_MAX_CLAUSES + 1); | ||
for (int i=0;i< NUM_MAX_CLAUSES ; i++ ) { | ||
bool.must(range("rc").lower(0).upper(i).includeLower(true).includeUpper(true)); | ||
} | ||
utils.query(bool).check(1); | ||
} | ||
} |