Search in sources :

Example 1 with FilterBuilder

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);
}
Also used : 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) FilteredQueryBuilder(org.elasticsearch.index.query.FilteredQueryBuilder)

Example 2 with FilterBuilder

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;
}
Also used : 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 FilterBuilder

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;
}
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 4 with FilterBuilder

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);
}
Also used : BwViewFilter(org.bedework.calfacade.filter.BwViewFilter) MatchAllFilterBuilder(org.elasticsearch.index.query.MatchAllFilterBuilder) 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) FilterBase(org.bedework.caldav.util.filter.FilterBase) ESQueryFilterBase(org.bedework.util.elasticsearch.ESQueryFilterBase)

Example 5 with FilterBuilder

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;
}
Also used : 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)

Aggregations

FilterBuilder (org.elasticsearch.index.query.FilterBuilder)35 TermFilterBuilder (org.elasticsearch.index.query.TermFilterBuilder)14 MatchAllFilterBuilder (org.elasticsearch.index.query.MatchAllFilterBuilder)12 NotFilterBuilder (org.elasticsearch.index.query.NotFilterBuilder)12 TermsFilterBuilder (org.elasticsearch.index.query.TermsFilterBuilder)12 AndFilterBuilder (org.elasticsearch.index.query.AndFilterBuilder)11 BoolFilterBuilder (org.elasticsearch.index.query.BoolFilterBuilder)11 NestedFilterBuilder (org.elasticsearch.index.query.NestedFilterBuilder)11 OrFilterBuilder (org.elasticsearch.index.query.OrFilterBuilder)11 QueryFilterBuilder (org.elasticsearch.index.query.QueryFilterBuilder)11 RangeFilterBuilder (org.elasticsearch.index.query.RangeFilterBuilder)11 GetMultipleDataResult (alien4cloud.dao.model.GetMultipleDataResult)7 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 Set (java.util.Set)6 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)6 IGenericSearchDAO (alien4cloud.dao.IGenericSearchDAO)5 LocationResourceTemplate (alien4cloud.model.orchestrators.locations.LocationResourceTemplate)5 AbstractSecurityEnabledResource (alien4cloud.security.AbstractSecurityEnabledResource)5 Subject (alien4cloud.security.Subject)5