use of org.elasticsearch.index.query.FilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method getAllForReindexStats.
public QueryBuilder getAllForReindexStats() {
FilterBuilder limit = not(addTerm("_type", docTypeEvent));
limit = or(limit, addTerm(PropertyInfoIndex.MASTER, "true"));
limit = or(limit, addTerm(PropertyInfoIndex.OVERRIDE, "true"));
return new FilteredQueryBuilder(null, limit);
}
use of org.elasticsearch.index.query.FilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method singleEventFilter.
/**
* Build a filter for a single event identified by the href
* and recurrenceid. If recurrenceId is null we target a master only
* otherwise we target an instance only.
*
* @param href of event
* @param recurrenceId of instance
* @return a filter builder
*/
public FilterBuilder singleEventFilter(final String href, final String recurrenceId) {
FilterBuilder anded = and(addTerm("_type", docTypeEvent), addTerm(makePropertyRef(PropertyInfoIndex.HREF), href), null);
if (recurrenceId == null) {
anded = and(anded, addTerm(PropertyInfoIndex.MASTER, "true"), null);
} else {
anded = and(anded, addTerm(PropertyInfoIndex.INSTANCE, "true"), null);
anded = and(anded, addTerm(PropertyInfoIndex.RECURRENCE_ID, recurrenceId), null);
}
return anded;
}
use of org.elasticsearch.index.query.FilterBuilder 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;
}
use of org.elasticsearch.index.query.FilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method addLimits.
/**
* This method adds extra limits to the search if they are necessary.
* If the search is already limited to one or more collections or
* specific href(s) then we don't need to add anything.
*
* <p>Otherwise we apply a default search context (if any). If the
* result is still not limited we limit to entities owned by the
* current principal</p>
*
* @param f current filter
* @param defaultFilterContext set if we have one
* @return augmented filter
* @throws CalFacadeException on error
*/
public FilterBuilder addLimits(final FilterBuilder f, final FilterBase defaultFilterContext, final DeletedState delState) throws CalFacadeException {
if (f instanceof MatchNone) {
return f;
}
final List<NamedFilterBuilder> nfbs = new ArrayList<>();
if (!queryLimited) {
if (defaultFilterContext != null) {
if (defaultFilterContext instanceof BwViewFilter) {
/* Treat this specially. Create a named query for each
child filter. The name will be the filter name which
itself is derived from the collection href.
*/
final FilterBase fb = ((BwViewFilter) defaultFilterContext).getFilter();
if ((fb != null) && (fb.getChildren() != null)) {
for (final FilterBase vfb : fb.getChildren()) {
nfbs.add(new NamedFilterBuilder(vfb.getName(), and(buildFilter(vfb), f, vfb.getName())));
}
}
} else {
final FilterBuilder limFb = buildFilter(defaultFilterContext);
nfbs.add(new NamedFilterBuilder(null, and(f, limFb, null)));
}
}
if (!queryLimited) {
nfbs.add(new NamedFilterBuilder(null, principalFilter(f)));
}
} else {
nfbs.add(new NamedFilterBuilder(null, f));
}
FilterBuilder recurFb = recurTerms();
FilterBuilder fb;
if (nfbs.size() == 1) {
fb = nfbs.get(0).fb;
if (recurFb != null) {
fb = and(fb, recurFb, null);
}
} else {
fb = null;
if (recurFb == null) {
recurFb = new MatchAllFilterBuilder();
}
for (final NamedFilterBuilder nfb : nfbs) {
fb = or(fb, and(nfb.fb, recurFb, nfb.name));
}
}
if (delState == includeDeleted) {
return fb;
}
return and(fb, addTerm(PropertyInfoIndex.DELETED, String.valueOf(delState == onlyDeleted)), null);
}
use of org.elasticsearch.index.query.FilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method multiHrefFilter.
public FilterBuilder multiHrefFilter(final Set<String> hrefs, final RecurringRetrievalMode rmode) throws CalFacadeException {
FilterBuilder fb = null;
for (final String href : hrefs) {
fb = or(fb, addTerm(hrefJname, href));
}
if (rmode.mode == Rmode.entityOnly) {
FilterBuilder limit = not(addTerm("_type", docTypeEvent));
limit = or(limit, addTerm(PropertyInfoIndex.MASTER, "true"));
return and(fb, limit, null);
}
if (rmode.mode == Rmode.overrides) {
final FilterBuilder limit = or(addTerm(PropertyInfoIndex.MASTER, "true"), addTerm(PropertyInfoIndex.OVERRIDE, "true"));
return and(fb, limit, null);
}
return fb;
}
Aggregations