use of org.elasticsearch.index.query.MatchAllFilterBuilder 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.MatchAllFilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method makeFilter.
private FilterBuilder makeFilter(final FilterBase f) throws CalFacadeException {
if (f == null) {
return null;
}
if (f instanceof BooleanFilter) {
final BooleanFilter bf = (BooleanFilter) f;
if (!bf.getValue()) {
return new MatchNone();
} else {
return new MatchAllFilterBuilder();
}
}
if (f instanceof AndFilter) {
final List<FilterBuilder> fbs = makeFilters(f.getChildren(), true);
if (fbs.size() == 1) {
return fbs.get(0);
}
final AndFilterBuilder afb = new AndFilterBuilder();
afb.filterName(f.getName());
for (final FilterBuilder fb : fbs) {
if (fb instanceof TermOrTerms) {
afb.add(makeFilter((TermOrTerms) fb));
} else {
afb.add(fb);
}
}
return afb;
}
if (f instanceof OrFilter) {
final List<FilterBuilder> fbs = makeFilters(f.getChildren(), false);
if (fbs.size() == 1) {
return fbs.get(0);
}
final OrFilterBuilder ofb = new OrFilterBuilder();
ofb.filterName(f.getName());
for (final FilterBuilder fb : fbs) {
if (fb instanceof TermOrTerms) {
ofb.add(((TermOrTerms) fb).makeFb());
} else {
ofb.add(fb);
}
}
return ofb;
}
if (f instanceof BwViewFilter) {
return doView((BwViewFilter) f);
}
if (f instanceof BwHrefFilter) {
queryLimited = true;
final TermFilterBuilder tfb = FilterBuilders.termFilter(hrefJname, ((BwHrefFilter) f).getHref());
tfb.filterName(f.getName());
return tfb;
}
if (!(f instanceof PropertyFilter)) {
return null;
}
final PropertyFilter pf = (PropertyFilter) f;
/*
if (pf.getPropertyIndex() == PropertyInfoIndex.CATEGORY_PATH) {
// Special case this one.
return new TermOrTerms(PropertyInfoIndex.CATEGORY_PATH.getJname(),
((ObjectFilter)pf).getEntity());
}*/
final BwIcalPropertyInfo.BwIcalPropertyInfoEntry pi = BwIcalPropertyInfo.getPinfo(pf.getPropertyIndex());
if (pi == null) {
throw new CalFacadeException("org.bedework.filters.unknownproperty", String.valueOf(pf.getPropertyIndex()));
}
final String fieldName;
final PropertyInfoIndex pii = pf.getPropertyIndex();
if (pf.getParentPropertyIndex() != null) {
fieldName = makePropertyRef(pf.getParentPropertyIndex(), pii);
} else {
fieldName = getJname(pii);
}
if (f instanceof PresenceFilter) {
final PresenceFilter prf = (PresenceFilter) f;
if (prf.getTestPresent()) {
return makeFilter(pf.getPropertyIndexes(), null, pf.getIntKey(), pf.getStrKey(), OperationType.presence, pf.getNot());
}
return makeFilter(pf.getPropertyIndexes(), null, pf.getIntKey(), pf.getStrKey(), OperationType.absence, pf.getNot());
}
if (pf instanceof TimeRangeFilter) {
return makeFilter(pf.getPropertyIndexes(), ((TimeRangeFilter) pf).getEntity(), pf.getIntKey(), pf.getStrKey(), OperationType.timeRange, pf.getNot());
}
if (pf instanceof BwCollectionFilter) {
if (!pf.getNot()) {
queryLimited = true;
}
final BwCalendar col = ((BwCollectionFilter) pf).getEntity();
return new TermOrTerms(colpathJname, col.getPath(), pf.getNot(), f.getName());
}
if (pf instanceof EntityTypeFilter) {
final EntityTypeFilter etf = (EntityTypeFilter) pf;
return new TermOrTerms(entityTypeJname, IcalDefs.entityTypeNames[etf.getEntity()], pf.getNot(), f.getName());
}
if (pf instanceof EntityTimeRangeFilter) {
final EntityTimeRangeFilter etrf = (EntityTimeRangeFilter) pf;
final TimeRange tr = etrf.getEntity();
String start = null;
String end = null;
if (tr.getStart() != null) {
start = tr.getStart().toString();
}
if (tr.getEnd() != null) {
end = tr.getEnd().toString();
}
return addDateRangeFilter(null, etrf.getEntityType(), start, end);
}
if (pii == PropertyInfoIndex.COLLECTION) {
if (!pf.getNot()) {
queryLimited = true;
}
}
if (pf instanceof ObjectFilter) {
final OperationType op;
if (((ObjectFilter) pf).getPrefixMatch()) {
op = OperationType.prefix;
} else {
op = OperationType.compare;
}
return makeFilter(pf.getPropertyIndexes(), getValue((ObjectFilter) pf), pf.getIntKey(), pf.getStrKey(), op, pf.getNot());
}
return null;
/*
if (f instanceof TimeRangeFilter) {
addThisJoin(pi);
return;
}
if (f instanceof BwObjectFilter) {
addThisJoin(pi);
return;
}*/
}
use of org.elasticsearch.index.query.MatchAllFilterBuilder in project bw-calendar-engine by Bedework.
the class TermOrTerms method makeFb.
FilterBuilder makeFb() {
final FilterBuilder fb;
if (!isTerms) {
final TermFilterBuilder tfb = FilterBuilders.termFilter(fldName, value);
if (filterName != null) {
tfb.filterName(filterName);
}
fb = tfb;
} else {
final List vals = (List) value;
FilterBuilder newFb = null;
if (anding) {
for (final Object o : vals) {
if (o instanceof MatchNone) {
// and false is always false
newFb = (FilterBuilder) o;
break;
}
}
} else {
for (final Object o : vals) {
if (o instanceof MatchAllFilterBuilder) {
// or true is always true
newFb = (FilterBuilder) o;
break;
}
}
}
if (newFb != null) {
fb = newFb;
} else {
final TermsFilterBuilder tfb = FilterBuilders.termsFilter(fldName, (Iterable<?>) value).execution(exec);
if (filterName != null) {
tfb.filterName(filterName);
}
fb = tfb;
}
}
if (!not) {
return fb;
}
if (fb instanceof MatchAllFilterBuilder) {
return new MatchNone();
}
if (fb instanceof MatchNone) {
return new MatchAllFilterBuilder();
}
return new NotFilterBuilder(fb);
}
Aggregations