简体   繁体   中英

Multiple filters per search cassandra lucene index

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!

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))

BooleanQuery s 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. IE:

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

translates to:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM