Search in sources :

Example 1 with TermsFilterBuilder

use of org.elasticsearch.index.query.TermsFilterBuilder 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 2 with TermsFilterBuilder

use of org.elasticsearch.index.query.TermsFilterBuilder 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)2 TermsFilterBuilder (org.elasticsearch.index.query.TermsFilterBuilder)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 List (java.util.List)1 TimeRange (org.bedework.caldav.util.TimeRange)1 CalFacadeException (org.bedework.calfacade.exc.CalFacadeException)1 BwIcalPropertyInfoEntry (org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)1 BaseFilterBuilder (org.elasticsearch.index.query.BaseFilterBuilder)1 FilterBuilder (org.elasticsearch.index.query.FilterBuilder)1 MatchAllFilterBuilder (org.elasticsearch.index.query.MatchAllFilterBuilder)1 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)1 MultiMatchQueryBuilder (org.elasticsearch.index.query.MultiMatchQueryBuilder)1 NotFilterBuilder (org.elasticsearch.index.query.NotFilterBuilder)1 QueryFilterBuilder (org.elasticsearch.index.query.QueryFilterBuilder)1 RangeFilterBuilder (org.elasticsearch.index.query.RangeFilterBuilder)1