use of org.elasticsearch.index.query.NestedFilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method makeFilter.
/* TODO we need to provide a chain of filters when we have deep paths,
e.g. entity[key1].entity[key2].value = "something"
*/
public FilterBuilder makeFilter(final List<PropertyInfoIndex> pis, final Object val, final Integer intKey, final String strKey, final OperationType opType, final boolean negate) throws CalFacadeException {
/* Work backwards through the property list building a path.
When the head of the path is a nested type:
If it's the first we found:
generate a match or term query based on the leaf
otherwise:
we already have a nested query to push inside a new one
If the top entry has a keyindex we expect a String or Numeric
key value we generate a bool query with 2 must match terms.
*/
FilterBuilder fb = null;
// current nested level
FilterBuilder nfb = null;
PropertyInfoIndex leafPii = null;
/* See if we need to build a nested query */
final BwIcalPropertyInfoEntry rootPie = BwIcalPropertyInfo.getPinfo(pis.get(0));
final boolean isNested = rootPie.getNested();
for (int plistIndex = pis.size() - 1; plistIndex >= 0; plistIndex--) {
final PropertyInfoIndex pii = pis.get(plistIndex);
if (leafPii == null) {
leafPii = pii;
}
final BwIcalPropertyInfoEntry bwPie = BwIcalPropertyInfo.getPinfo(pii);
if (isNested) {
final FilterBuilder nested;
String path = makePropertyRef(pis, plistIndex);
if (nfb != null) {
if (plistIndex == 0) {
// TODO Temp fix this
path = "event." + path;
}
nested = new NestedFilterBuilder(path, nfb);
} else {
fb = makeFilter(leafPii, makePropertyRef(pis), val, opType);
/* Is the parent indexed? */
final BwIcalPropertyInfoEntry parentPie;
if (plistIndex == 0) {
// No parent
parentPie = null;
} else {
parentPie = BwIcalPropertyInfo.getPinfo(pis.get(plistIndex - 1));
}
if ((parentPie != null) && (parentPie.getKeyindex() != PropertyInfoIndex.UNKNOWN_PROPERTY)) {
final BoolFilterBuilder bfb = new BoolFilterBuilder();
if (fb == null) {
error("No nested query for " + pii);
return null;
}
bfb.must(fb);
final List<PropertyInfoIndex> indexPis = new ArrayList<>();
indexPis.add(pis.get(plistIndex - 1));
indexPis.add(parentPie.getKeyindex());
final String indexPath = makePropertyRef(indexPis);
if (intKey != null) {
bfb.must(new TermFilterBuilder(indexPath, intKey));
} else if (strKey != null) {
bfb.must(new TermFilterBuilder(indexPath, strKey));
} else {
error("Missing key for index for " + pii);
return null;
}
fb = bfb;
}
nested = fb;
}
nfb = nested;
} else if (plistIndex == 0) {
// No nested types found
fb = makeFilter(leafPii, makePropertyRef(pis), val, opType);
}
}
if (nfb != null) {
fb = nfb;
}
if (negate) {
return FilterBuilders.notFilter(fb);
}
return fb;
}
Aggregations