I'm having a trouble understanding the difference between the Bool filter and the And filter in elastic search.
Context: say my documents have fields: X, Y, Z.
Each field can have multiple values.
Goal:
I want to send a query to elastic search in the following sense: (X=valueX1 OR X=valueX2) AND (Y=valueY1 OR Y=valueY2 OR .. ) AND (Z=valueZ1 OR Z=valueZ2 OR ...).
Attempt:
This is how I'm doing that:
BoolFilterBuilder mainClaus = FilterBuilders.boolFilter();
FilterBuilder filterBuilder = mainClaus;
BoolFilterBuilder xClaus = FilterBuilders.boolFilter();
BoolFilterBuilder yClaus = FilterBuilders.boolFilter();
BoolFilterBuilder zClaus = FilterBuilders.boolFilter();
mainClaus.must(xClaus);
mainClaus.must(yClaus);
mainClaus.must(zClaus);
//Return a document if it has at least one of those values.
xClaus.should( FilterBuilders.termFilter("X", "valueX1") );
xClaus.should( FilterBuilders.termFilter("X", "valueX2") );
xClaus.should( FilterBuilders.termFilter("X", "valueX3") );
//Return a document if it has at least one of those values.
yClaus.should( FilterBuilders.termFilter("Y", "valueY1") );
yClaus.should( FilterBuilders.termFilter("Y", "valueY2") );
yClaus.should( FilterBuilders.termFilter("Y", "valueY3") );
//Return a document if it has at least one of those values.
zClaus.should( FilterBuilders.termFilter("Z", "valueZ1") );
zClaus.should( FilterBuilders.termFilter("Z", "valueZ2") );
zClaus.should( FilterBuilders.termFilter("Z", "valueZ3") );
Questions:
- Is my approach correct?
- What's the difference between the Bool filter and the And filter?