Search in sources :

Example 6 with FilterBuilder

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

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

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

use of org.elasticsearch.index.query.FilterBuilder in project opencast by opencast.

the class AbstractElasticsearchQueryBuilder method createQuery.

/**
 * Create the actual query. We start with a query that matches everything, then move to the boolean conditions,
 * finally add filter queries.
 */
private void createQuery() {
    queryBuilder = new MatchAllQueryBuilder();
    // The boolean query builder
    BoolQueryBuilder booleanQuery = new BoolQueryBuilder();
    // Terms
    if (searchTerms != null) {
        for (Map.Entry<String, Set<Object>> entry : searchTerms.entrySet()) {
            Set<Object> values = entry.getValue();
            if (values.size() == 1)
                booleanQuery.must(new TermsQueryBuilder(entry.getKey(), values.iterator().next()));
            else
                booleanQuery.must(new TermsQueryBuilder(entry.getKey(), values.toArray(new String[values.size()])));
        }
        this.queryBuilder = booleanQuery;
    }
    // Negative terms
    if (negativeSearchTerms != null) {
        for (Map.Entry<String, Set<Object>> entry : negativeSearchTerms.entrySet()) {
            Set<Object> values = entry.getValue();
            if (values.size() == 1)
                booleanQuery.mustNot(new TermsQueryBuilder(entry.getKey(), values.iterator().next()));
            else
                booleanQuery.mustNot(new TermsQueryBuilder(entry.getKey(), values.toArray(new String[values.size()])));
        }
        this.queryBuilder = booleanQuery;
    }
    // Date ranges
    if (dateRanges != null) {
        for (DateRange dr : dateRanges) {
            booleanQuery.must(dr.getQueryBuilder());
        }
        this.queryBuilder = booleanQuery;
    }
    // Text
    if (text != null) {
        QueryStringQueryBuilder queryBuilder = QueryBuilders.queryString(text).field(TEXT);
        booleanQuery.must(queryBuilder);
        this.queryBuilder = booleanQuery;
    }
    // Fuzzy text
    if (fuzzyText != null) {
        FuzzyLikeThisQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyLikeThisQuery(TEXT_FUZZY).likeText(fuzzyText);
        booleanQuery.must(fuzzyQueryBuilder);
        this.queryBuilder = booleanQuery;
    }
    QueryBuilder unfilteredQuery = queryBuilder;
    List<FilterBuilder> filters = new ArrayList<FilterBuilder>();
    // Add filtering for AND terms
    if (groups != null) {
        for (ValueGroup group : groups) {
            filters.addAll(group.getFilterBuilders());
        }
    }
    // Non-Empty fields
    if (nonEmptyFields != null) {
        for (String field : nonEmptyFields) {
            filters.add(FilterBuilders.existsFilter(field));
        }
    }
    // Empty fields
    if (emptyFields != null) {
        for (String field : emptyFields) {
            filters.add(FilterBuilders.missingFilter(field));
        }
    }
    // Filter expressions
    if (filter != null) {
        filters.add(FilterBuilders.termFilter(IndexSchema.TEXT, filter));
    }
    // Apply the filters
    if (filters.size() == 1) {
        this.queryBuilder = QueryBuilders.filteredQuery(unfilteredQuery, filters.get(0));
    } else if (filters.size() > 1) {
        FilterBuilder andFilter = FilterBuilders.andFilter(filters.toArray(new FilterBuilder[filters.size()]));
        this.queryBuilder = QueryBuilders.filteredQuery(unfilteredQuery, andFilter);
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ArrayList(java.util.ArrayList) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) FuzzyLikeThisQueryBuilder(org.elasticsearch.index.query.FuzzyLikeThisQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) HashMap(java.util.HashMap) Map(java.util.Map) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) FuzzyLikeThisQueryBuilder(org.elasticsearch.index.query.FuzzyLikeThisQueryBuilder)

Example 10 with FilterBuilder

use of org.elasticsearch.index.query.FilterBuilder in project alien4cloud by alien4cloud.

the class EsDaoPaginatedSearchTest method facetedSearchPaginatedTest.

// @Ignore
@Test
public void facetedSearchPaginatedTest() throws IndexingServiceException, IOException, InterruptedException {
    String searchText = "jndi";
    int maxElement = getCount(QueryBuilders.matchPhrasePrefixQuery("_all", searchText).maxExpansions(10));
    int size = 7;
    // simple faceted pagination
    assertTrue(maxElement > 0);
    testFacetedSearchWellPaginated(maxElement, size, searchText, null, null);
    // faceted search with filters
    FilterBuilder filter = FilterBuilders.termFilter("capabilities.type", "jndi");
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
    queryBuilder = QueryBuilders.filteredQuery(queryBuilder, filter);
    maxElement = getCount(queryBuilder);
    Map<String, String[]> filters = new HashMap<String, String[]>();
    filters.put("capabilities.type", new String[] { "jndi" });
    assertTrue(maxElement > 0);
    testFacetedSearchWellPaginated(maxElement, size, searchText, filters, FetchContext.SUMMARY);
    // test nothing found
    // test when nothing found
    searchText = "pacpac";
    maxElement = getCount(QueryBuilders.matchPhrasePrefixQuery("_all", searchText).maxExpansions(10));
    assertEquals(0, maxElement);
    GetMultipleDataResult<NodeType> searchResp = dao.facetedSearch(NodeType.class, searchText, null, null, 0, size);
    assertNotNull(searchResp);
    assertNotNull(searchResp.getData());
    assertNotNull(searchResp.getTypes());
    assertEquals(0, searchResp.getData().length);
    assertEquals(0, searchResp.getTypes().length);
}
Also used : HashMap(java.util.HashMap) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) NodeType(org.alien4cloud.tosca.model.types.NodeType) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Test(org.junit.Test)

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