3

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!

Programmer9000
  • 1,959
  • 3
  • 17
  • 27

1 Answers1

5

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)))
Eduardo Alonso
  • 233
  • 1
  • 5