Search in sources :

Example 26 with FilterBuilder

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);
}
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)

Example 27 with FilterBuilder

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

Example 28 with FilterBuilder

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;
}
Also used : SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Group(alien4cloud.security.model.Group) User(alien4cloud.security.model.User) Authentication(org.springframework.security.core.Authentication) FilterBuilder(org.elasticsearch.index.query.FilterBuilder)

Example 29 with 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;
}
Also used : EventInfo(org.bedework.calfacade.svc.EventInfo) SearchHit(org.elasticsearch.search.SearchHit) TermFilterBuilder(org.elasticsearch.index.query.TermFilterBuilder) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) BwEvent(org.bedework.calfacade.BwEvent) Acl(org.bedework.access.Acl) GetEntityResponse(org.bedework.calfacade.responses.GetEntityResponse)

Example 30 with FilterBuilder

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;
}
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