Search in sources :

Example 1 with TermFilterBuilder

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;
}
Also used : PropertyInfoIndex(org.bedework.util.calendar.PropertyIndex.PropertyInfoIndex) BwIcalPropertyInfoEntry(org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry) NestedFilterBuilder(org.elasticsearch.index.query.NestedFilterBuilder) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) AndFilterBuilder(org.elasticsearch.index.query.AndFilterBuilder) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) OrFilterBuilder(org.elasticsearch.index.query.OrFilterBuilder) RangeFilterBuilder(org.elasticsearch.index.query.RangeFilterBuilder) BoolFilterBuilder(org.elasticsearch.index.query.BoolFilterBuilder) QueryFilterBuilder(org.elasticsearch.index.query.QueryFilterBuilder) NotFilterBuilder(org.elasticsearch.index.query.NotFilterBuilder) NestedFilterBuilder(org.elasticsearch.index.query.NestedFilterBuilder) MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) TermsFilterBuilder(org.elasticsearch.index.query.TermsFilterBuilder) ArrayList(java.util.ArrayList) BoolFilterBuilder(org.elasticsearch.index.query.BoolFilterBuilder)

Example 2 with TermFilterBuilder

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);
}
Also used : TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) AndFilterBuilder(org.elasticsearch.index.query.AndFilterBuilder) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) OrFilterBuilder(org.elasticsearch.index.query.OrFilterBuilder) RangeFilterBuilder(org.elasticsearch.index.query.RangeFilterBuilder) BoolFilterBuilder(org.elasticsearch.index.query.BoolFilterBuilder) QueryFilterBuilder(org.elasticsearch.index.query.QueryFilterBuilder) NotFilterBuilder(org.elasticsearch.index.query.NotFilterBuilder) NestedFilterBuilder(org.elasticsearch.index.query.NestedFilterBuilder) MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) TermsFilterBuilder(org.elasticsearch.index.query.TermsFilterBuilder)

Example 3 with TermFilterBuilder

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;
    }*/
}
Also used : BooleanFilter(org.bedework.caldav.util.filter.BooleanFilter) PresenceFilter(org.bedework.caldav.util.filter.PresenceFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) TimeRangeFilter(org.bedework.caldav.util.filter.TimeRangeFilter) BwIcalPropertyInfo(org.bedework.calfacade.ical.BwIcalPropertyInfo) EntityTypeFilter(org.bedework.caldav.util.filter.EntityTypeFilter) BwCollectionFilter(org.bedework.calfacade.filter.BwCollectionFilter) AndFilterBuilder(org.elasticsearch.index.query.AndFilterBuilder) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) OrFilterBuilder(org.elasticsearch.index.query.OrFilterBuilder) RangeFilterBuilder(org.elasticsearch.index.query.RangeFilterBuilder) BoolFilterBuilder(org.elasticsearch.index.query.BoolFilterBuilder) QueryFilterBuilder(org.elasticsearch.index.query.QueryFilterBuilder) NotFilterBuilder(org.elasticsearch.index.query.NotFilterBuilder) NestedFilterBuilder(org.elasticsearch.index.query.NestedFilterBuilder) MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) TermsFilterBuilder(org.elasticsearch.index.query.TermsFilterBuilder) AndFilterBuilder(org.elasticsearch.index.query.AndFilterBuilder) BwHrefFilter(org.bedework.calfacade.filter.BwHrefFilter) OrFilter(org.bedework.caldav.util.filter.OrFilter) BwCalendar(org.bedework.calfacade.BwCalendar) ObjectFilter(org.bedework.caldav.util.filter.ObjectFilter) EntityTimeRangeFilter(org.bedework.caldav.util.filter.EntityTimeRangeFilter) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) OrFilterBuilder(org.elasticsearch.index.query.OrFilterBuilder) AndFilter(org.bedework.caldav.util.filter.AndFilter) BwViewFilter(org.bedework.calfacade.filter.BwViewFilter) TimeRange(org.bedework.caldav.util.TimeRange) MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) PropertyInfoIndex(org.bedework.util.calendar.PropertyIndex.PropertyInfoIndex) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) PropertyFilter(org.bedework.caldav.util.filter.PropertyFilter) BwIcalPropertyInfoEntry(org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)

Example 4 with TermFilterBuilder

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);
}
Also used : MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) NotFilterBuilder(org.elasticsearch.index.query.NotFilterBuilder) BaseFilterBuilder(org.elasticsearch.index.query.BaseFilterBuilder) MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) TermsFilterBuilder(org.elasticsearch.index.query.TermsFilterBuilder) TermsFilterBuilder(org.elasticsearch.index.query.TermsFilterBuilder) NotFilterBuilder(org.elasticsearch.index.query.NotFilterBuilder) List(java.util.List) ArrayList(java.util.ArrayList)

Example 5 with TermFilterBuilder

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());
    }
}
Also used : RangeFilterBuilder(org.elasticsearch.index.query.RangeFilterBuilder) TermsFilterBuilder(org.elasticsearch.index.query.TermsFilterBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) QueryFilterBuilder(org.elasticsearch.index.query.QueryFilterBuilder) CalFacadeException(org.bedework.calfacade.exc.CalFacadeException) TimeRange(org.bedework.caldav.util.TimeRange) BwIcalPropertyInfoEntry(org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) Collection(java.util.Collection) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder)

Aggregations

TermFilterBuilder (org.elasticsearch.index.query.TermFilterBuilder)5 TermsFilterBuilder (org.elasticsearch.index.query.TermsFilterBuilder)5 FilterBuilder (org.elasticsearch.index.query.FilterBuilder)4 MatchAllFilterBuilder (org.elasticsearch.index.query.MatchAllFilterBuilder)4 NotFilterBuilder (org.elasticsearch.index.query.NotFilterBuilder)4 QueryFilterBuilder (org.elasticsearch.index.query.QueryFilterBuilder)4 RangeFilterBuilder (org.elasticsearch.index.query.RangeFilterBuilder)4 BwIcalPropertyInfoEntry (org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)3 AndFilterBuilder (org.elasticsearch.index.query.AndFilterBuilder)3 BoolFilterBuilder (org.elasticsearch.index.query.BoolFilterBuilder)3 NestedFilterBuilder (org.elasticsearch.index.query.NestedFilterBuilder)3 OrFilterBuilder (org.elasticsearch.index.query.OrFilterBuilder)3 ArrayList (java.util.ArrayList)2 TimeRange (org.bedework.caldav.util.TimeRange)2 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)2 PropertyInfoIndex (org.bedework.util.calendar.PropertyIndex.PropertyInfoIndex)2 Collection (java.util.Collection)1 List (java.util.List)1 AndFilter (org.bedework.caldav.util.filter.AndFilter)1 BooleanFilter (org.bedework.caldav.util.filter.BooleanFilter)1