Multiple filters per search cassandra lucene index

536 Views Asked by At

Is there a way to use multiple filters (using the builder) for a cassandra lucene index search?

Here's an example of what I'm doing:

    // Age Filter
conditionsToFilter.add(range("age")
    .lower(indexFormatDate(preferences.getAgeMax()))
    .upper(indexFormatDate(preferences.getAgeMin()))
    .includeLower(true)
    .includeUpper(true)
    .docValues(DOC_VALUES));

// Height Filter
conditionsToFilter.add(range("height")
    .lower(preferences.getHeightMin())
    .upper(preferences.getHeightMax())
    .includeLower(true)
    .includeUpper(true)
    .docValues(DOC_VALUES));

// Distance Filter
conditionsToFilter.add(geoDistance("location",
    preferences.getCurrentUserLocation().getLongitude(),
    preferences.getCurrentUserLocation().getLatitude(),
    String.format("%dmi", preferences.getDistanceMax())));


// Apply Filters
Search searchObj = com.stratio.cassandra.lucene.builder.Builder.search();
for (Condition condition : conditionsToFilter) {
  searchObj.filter(condition); <-- this definitely won't work
}

// Create Search String
String query = searchObj
    .refresh(false)
    .build();

what is the prescribed method of doing something like this? Thanks!

1

There are 1 best solutions below

0
Eduardo Alonso On

You should use BooleanQuery.

Replace this:

// Apply Filters
Search searchObj = com.stratio.cassandra.lucene.builder.Builder.search();
for (Condition condition : conditionsToFilter) {
    searchObj.filter(condition); <-- this definitely won't work
}

with:

Search searchObj = com.stratio.cassandra.lucene.builder.Builder.search();
searchObj.filter(bool().must(conditionsToFilter))

BooleanQuerys are able to execute simple boolean expresions including AND with must(), OR with should() or NOT with not().

That feature added to its ability to nest you can build almost every posible boolean expression. I.E:

((A && B && C) || (D && !E))

translates to:

bool().should(bool().must(A,B,C),bool().must(D,bool().not(E)))