use of org.elasticsearch.index.query.TermFilterBuilder 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.TermFilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method overridesOnly.
final FilterBuilder overridesOnly(final String uid) {
FilterBuilder flt = new TermFilterBuilder("_type", docTypeEvent);
flt = and(flt, addTerm(PropertyInfoIndex.UID, uid), null);
return and(flt, addTerm(PropertyInfoIndex.OVERRIDE, "true"), null);
}
use of org.elasticsearch.index.query.TermFilterBuilder 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.TermFilterBuilder 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);
}
use of org.elasticsearch.index.query.TermFilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method makeFilter.
private FilterBuilder makeFilter(final PropertyInfoIndex pii, final String path, final Object val, final OperationType opType) throws CalFacadeException {
final BwIcalPropertyInfoEntry bwPie = BwIcalPropertyInfo.getPinfo(pii);
switch(opType) {
case compare:
if (bwPie.getAnalyzed()) {
if (val instanceof Collection) {
final String[] vals;
try {
final Collection valsC = (Collection) val;
vals = (String[]) (valsC).toArray(new String[valsC.size()]);
} catch (final Throwable t) {
throw new CalFacadeException(CalFacadeException.filterBadOperator, "Invalid query. Multi match only allowed on strings");
}
final MultiMatchQueryBuilder mmqb = new MultiMatchQueryBuilder(path, vals);
return new QueryFilterBuilder(mmqb);
}
return new QueryFilterBuilder(new MatchQueryBuilder(path, val));
}
if (val instanceof Collection) {
final TermsFilterBuilder tfb = FilterBuilders.termsFilter(path, (Collection) val);
tfb.execution("or");
return tfb;
}
return new TermFilterBuilder(path, val);
case timeRange:
final RangeFilterBuilder rfb = FilterBuilders.rangeFilter(path);
final TimeRange tr = (TimeRange) val;
if (tr.getEnd() == null) {
rfb.gte(tr.getStart().toString());
return rfb;
}
if (tr.getStart() == null) {
rfb.lt(tr.getEnd().toString());
return rfb;
}
rfb.from(tr.getStart().toString());
rfb.to(tr.getEnd().toString());
rfb.includeLower(true);
rfb.includeUpper(false);
return rfb;
case absence:
return FilterBuilders.missingFilter(path);
case presence:
return FilterBuilders.existsFilter(path);
case prefix:
return FilterBuilders.prefixFilter(path, (String) val);
default:
throw new CalFacadeException(CalFacadeException.filterBadOperator, opType.toString());
}
}
Aggregations