Search in sources :

Example 1 with NestedFilterBuilder

use of org.elasticsearch.index.query.NestedFilterBuilder 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)

Aggregations

ArrayList (java.util.ArrayList)1 BwIcalPropertyInfoEntry (org.bedework.calfacade.ical.BwIcalPropertyInfo.BwIcalPropertyInfoEntry)1 PropertyInfoIndex (org.bedework.util.calendar.PropertyIndex.PropertyInfoIndex)1 AndFilterBuilder (org.elasticsearch.index.query.AndFilterBuilder)1 BoolFilterBuilder (org.elasticsearch.index.query.BoolFilterBuilder)1 FilterBuilder (org.elasticsearch.index.query.FilterBuilder)1 MatchAllFilterBuilder (org.elasticsearch.index.query.MatchAllFilterBuilder)1 NestedFilterBuilder (org.elasticsearch.index.query.NestedFilterBuilder)1 NotFilterBuilder (org.elasticsearch.index.query.NotFilterBuilder)1 OrFilterBuilder (org.elasticsearch.index.query.OrFilterBuilder)1 QueryFilterBuilder (org.elasticsearch.index.query.QueryFilterBuilder)1 RangeFilterBuilder (org.elasticsearch.index.query.RangeFilterBuilder)1 TermFilterBuilder (org.elasticsearch.index.query.TermFilterBuilder)1 TermsFilterBuilder (org.elasticsearch.index.query.TermsFilterBuilder)1