use of org.elasticsearch.index.query.FilterBuilder in project alien4cloud by alien4cloud.
the class EsDaoPaginatedSearchTest method textBasedSearchPaginatedTest.
@Test
public void textBasedSearchPaginatedTest() throws IndexingServiceException, IOException, InterruptedException {
// text search based
String searchText = "jndi";
int maxElement = getCount(QueryBuilders.matchPhrasePrefixQuery("_all", searchText).maxExpansions(10));
int size = 7;
assertTrue(maxElement > 0);
testTextBasedSearchWellPaginated(maxElement, size, searchText, null);
// text search based 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);
testTextBasedSearchWellPaginated(maxElement, size, searchText, filters);
// test when nothing found
searchText = "pacpac";
maxElement = getCount(QueryBuilders.matchPhrasePrefixQuery("_all", searchText).maxExpansions(10));
assertEquals(0, maxElement);
GetMultipleDataResult<NodeType> searchResp = dao.search(NodeType.class, searchText, null, 0, size);
assertNotNull(searchResp);
assertNotNull(searchResp.getData());
assertNotNull(searchResp.getTypes());
assertEquals(0, searchResp.getData().length);
assertEquals(0, searchResp.getTypes().length);
}
use of org.elasticsearch.index.query.FilterBuilder in project alien4cloud by alien4cloud.
the class ApplicationEnvironmentController method searchAuthorizedEnvironments.
private GetMultipleDataResult<ApplicationEnvironment> searchAuthorizedEnvironments(String applicationId, FilteredSearchRequest searchRequest) {
FilterBuilder authorizationFilter = getEnvironmentAuthorizationFilters(applicationId);
Map<String, String[]> applicationEnvironmentFilters = getApplicationEnvironmentFilters(applicationId);
return alienDAO.search(ApplicationEnvironment.class, searchRequest.getQuery(), applicationEnvironmentFilters, authorizationFilter, null, searchRequest.getFrom(), searchRequest.getSize());
}
use of org.elasticsearch.index.query.FilterBuilder in project alien4cloud by alien4cloud.
the class AuthorizationUtil method getResourceAuthorizationFilters.
/**
* Add a filter that check for authorizations on resources
* Takes also in account the ALL_USER group
*/
public static FilterBuilder getResourceAuthorizationFilters() {
final Authentication auth = SecurityContextHolder.getContext().getAuthentication();
if (auth.getAuthorities().contains(new SimpleGrantedAuthority(Role.ADMIN.toString()))) {
return null;
}
FilterBuilder filterBuilder;
User user = (User) auth.getPrincipal();
if (user.getGroups() != null && !user.getGroups().isEmpty()) {
filterBuilder = FilterBuilders.boolFilter().should(FilterBuilders.nestedFilter("userRoles", FilterBuilders.termFilter("userRoles.key", auth.getName()))).should(FilterBuilders.nestedFilter("groupRoles", FilterBuilders.inFilter("groupRoles.key", user.getGroups().toArray())));
} else {
filterBuilder = FilterBuilders.nestedFilter("userRoles", FilterBuilders.termFilter("userRoles.key", auth.getName()));
}
Group group = getAllUsersGroup();
if (group != null) {
String groupId = group.getId();
// add ALL_USERS group as OR filter
filterBuilder = FilterBuilders.orFilter(filterBuilder, FilterBuilders.nestedFilter("groupRoles", FilterBuilders.inFilter("groupRoles.key", groupId)));
}
return filterBuilder;
}
use of org.elasticsearch.index.query.FilterBuilder in project bw-calendar-engine by Bedework.
the class BwIndexEsImpl method fetchEvent.
@Override
public GetEntityResponse<EventInfo> fetchEvent(final String href) throws CalFacadeException {
final GetEntityResponse<EventInfo> resp = new GetEntityResponse<>();
final String recurrenceId;
final String hrefNorid;
// Check validity
final int pos = href.lastIndexOf("/");
if (pos < 0) {
throw new RuntimeException("Bad href: " + href);
}
final int fragPos = href.lastIndexOf("#");
if (fragPos < pos) {
hrefNorid = href;
recurrenceId = null;
} else {
hrefNorid = href.substring(0, fragPos);
recurrenceId = href.substring(fragPos + 1);
}
final FilterBuilder fltr = getFilters(null).singleEventFilter(href, recurrenceId);
final SearchHit hit = fetchEntity(docTypeEvent, fltr);
if (hit == null) {
return notFound(resp);
}
final EntityBuilder eb = getEntityBuilder(hit.sourceAsMap());
final EventInfo ei = eb.makeEvent(hit.getId(), false);
if (ei == null) {
return notFound(resp);
}
final BwEvent ev = ei.getEvent();
final Acl.CurrentAccess ca = accessCheck.checkAccess(ev, privRead, true);
if ((ca == null) || !ca.getAccessAllowed()) {
return notFound(resp);
}
ei.setCurrentAccess(ca);
if (ev.getRecurrenceId() != null) {
// Single instance
resp.setEntity(ei);
return resp;
}
addOverrides(resp, idxpars.getUserIndexName(), ei);
return resp;
}
use of org.elasticsearch.index.query.FilterBuilder in project bw-calendar-engine by Bedework.
the class ESQueryFilter method recurTerms.
final FilterBuilder recurTerms() throws CalFacadeException {
if (recurRetrieval.mode == Rmode.expanded) {
// Limit events to instances only //
FilterBuilder limit = not(addTerm("_type", docTypeEvent));
limit = or(limit, addTerm(PropertyInfoIndex.INSTANCE, "true"));
limit = or(limit, addTerm(PropertyInfoIndex.OVERRIDE, "true"));
limit = or(limit, and(addTerm(PropertyInfoIndex.MASTER, "true"), addTerm(PropertyInfoIndex.RECURRING, "false"), null));
return limit;
}
if (recurRetrieval.mode == Rmode.entityOnly) {
FilterBuilder limit = not(addTerm("_type", docTypeEvent));
limit = or(limit, addTerm(PropertyInfoIndex.MASTER, "true"));
return limit;
}
if (queryFiltered) {
return null;
}
FilterBuilder limit = not(addTerm("_type", docTypeEvent));
limit = or(limit, addTerm(PropertyInfoIndex.MASTER, "true"));
limit = or(limit, addTerm(PropertyInfoIndex.OVERRIDE, "true"));
return limit;
}
Aggregations