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);
}
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;
}*/
}
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);
}
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);
}
}
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);
}
Aggregations