Search in sources :

Example 11 with FilterBuilder

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

the class EsDaoPaginatedSearchTest method simpleFindPaginatedTest.

@Test
public void simpleFindPaginatedTest() throws IOException {
    int maxElement;
    int size;
    // test simple find all search
    maxElement = getCount(QueryBuilders.matchAllQuery());
    size = 11;
    assertTrue(maxElement > 0);
    testSimpleSearchWellPaginated(maxElement, size, null);
    // test simple find with filters
    FilterBuilder filter = FilterBuilders.termFilter("capabilities.type", "jndi");
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
    queryBuilder = QueryBuilders.filteredQuery(queryBuilder, filter);
    maxElement = getCount(queryBuilder);
    size = 4;
    assertTrue(maxElement > 0);
    Map<String, String[]> filters = new HashMap<String, String[]>();
    filters.put("capabilities.type", new String[] { "jndi" });
    testSimpleSearchWellPaginated(maxElement, size, filters);
}
Also used : HashMap(java.util.HashMap) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Test(org.junit.Test)

Example 12 with FilterBuilder

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

the class ApplicationController method search.

/**
 * Search for an application.
 *
 * @param searchRequest The element that contains criterias for search operation.
 * @return A rest response that contains a {@link FacetedSearchResult} containing applications.
 */
@ApiOperation(value = "Search for applications", notes = "Returns a search result with that contains applications matching the request. A application is returned only if the connected user has at least one application role in [ APPLICATION_MANAGER | APPLICATION_USER | APPLICATION_DEVOPS | DEPLOYMENT_MANAGER ]")
@RequestMapping(value = "/search", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public RestResponse<FacetedSearchResult> search(@RequestBody FilteredSearchRequest searchRequest) {
    FilterBuilder authorizationFilter = AuthorizationUtil.getResourceAuthorizationFilters();
    // We want to sort applications by deployed/undeployed and then application name.
    // Query all application ids and name.
    QueryBuilder queryBuilder = alienDAO.buildSearchQuery(Application.class, searchRequest.getQuery()).setFilters(searchRequest.getFilters(), authorizationFilter).queryBuilder();
    SearchResponse response = alienDAO.getClient().prepareSearch(alienDAO.getIndexForType(Application.class)).setQuery(queryBuilder).setFetchSource(new String[] { "name" }, null).setSize(Integer.MAX_VALUE).get();
    // Get their status (deployed vs undeployed)
    List<DeployedAppHolder> appHolders = Lists.newLinkedList();
    for (SearchHit hit : response.getHits().getHits()) {
        String id = hit.getId();
        String appName = (String) hit.getSource().get("name");
        boolean isDeployed = alienDAO.buildQuery(Deployment.class).setFilters(fromKeyValueCouples("sourceId", id, "endDate", null)).count() > 0;
        appHolders.add(new DeployedAppHolder(id, appName, isDeployed));
    }
    // Sort to have first all deployed apps sorted by name and then all undeployed apps sorted by name.
    Collections.sort(appHolders);
    // Compute the list of app ids to fetch based on the query pagination parameters
    List<String> appIdsToFetch = Lists.newArrayList();
    int to = searchRequest.getFrom() + searchRequest.getSize();
    for (int i = searchRequest.getFrom(); i < appHolders.size() && i < to; i++) {
        appIdsToFetch.add(appHolders.get(i).appId);
    }
    List<Application> applications;
    if (appIdsToFetch.size() == 0) {
        applications = Lists.newArrayList();
    } else {
        applications = alienDAO.findByIds(Application.class, appIdsToFetch.toArray(new String[appIdsToFetch.size()]));
    }
    return RestResponseBuilder.<FacetedSearchResult>builder().data(new FacetedSearchResult<>(searchRequest.getFrom(), to, response.getTookInMillis(), appHolders.size(), new String[] { Application.class.getSimpleName() }, applications.toArray(new Application[applications.size()]), Maps.newHashMap())).build();
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) Deployment(alien4cloud.model.deployment.Deployment) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) Application(alien4cloud.model.application.Application) FacetedSearchResult(alien4cloud.dao.model.FacetedSearchResult) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Example 13 with FilterBuilder

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

the class OrchestratorController method search.

@ApiOperation(value = "Search for orchestrators.")
@RequestMapping(method = RequestMethod.GET)
@PreAuthorize("isAuthenticated()")
public RestResponse<GetMultipleDataResult<Orchestrator>> search(@ApiParam(value = "Query text.") @RequestParam(required = false) String query, @ApiParam(value = "If true only connected orchestrators will be retrieved.") @RequestParam(required = false, defaultValue = "false") boolean connectedOnly, @ApiParam(value = "Query from the given index.") @RequestParam(required = false, defaultValue = "0") int from, @ApiParam(value = "Maximum number of results to retrieve.") @RequestParam(required = false, defaultValue = "20") int size) {
    FilterBuilder authorizationFilter = AuthorizationUtil.getResourceAuthorizationFilters();
    OrchestratorState filterStatus = connectedOnly ? OrchestratorState.CONNECTED : null;
    GetMultipleDataResult<Orchestrator> result = orchestratorService.search(query, filterStatus, from, size, authorizationFilter);
    return RestResponseBuilder.<GetMultipleDataResult<Orchestrator>>builder().data(result).build();
}
Also used : OrchestratorState(alien4cloud.model.orchestrators.OrchestratorState) FilterBuilder(org.elasticsearch.index.query.FilterBuilder) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) Orchestrator(alien4cloud.model.orchestrators.Orchestrator) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 14 with FilterBuilder

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

the class QuickSearchController method search.

@ApiOperation(value = "Search for applications or tosca elements in ALIEN's repository.")
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@PreAuthorize("isAuthenticated()")
public RestResponse<GetMultipleDataResult> search(@RequestBody BasicSearchRequest requestObject) {
    Set<String> authoIndexes = Sets.newHashSet();
    Set<Class<?>> classes = Sets.newHashSet();
    // First phase : COMPONENTS search, needed role Role.COMPONENTS_BROWSER or Role.ADMIN
    if (AuthorizationUtil.hasOneRoleIn(Role.COMPONENTS_BROWSER)) {
        authoIndexes.add(ElasticSearchDAO.TOSCA_ELEMENT_INDEX);
        classes.add(NodeType.class);
    }
    GetMultipleDataResult searchResultComponents = searchByType(requestObject, authoIndexes, classes, null, null);
    // Second phase : APPLICATION search (with rights filter) or with the Role.ADMIN
    authoIndexes.clear();
    classes.clear();
    authoIndexes.add(Application.class.getSimpleName().toLowerCase());
    classes.add(Application.class);
    // Adding filters to get only authorized applications
    // only filter on users roles on the application if the current user is not an ADMIN
    FilterBuilder authorizationFilter = AuthorizationUtil.getResourceAuthorizationFilters();
    GetMultipleDataResult<?> searchResultApplications = searchByType(requestObject, authoIndexes, classes, null, authorizationFilter);
    // Final merge result : COMPONENTS + APPLICATIONS
    GetMultipleDataResult searchResult = new GetMultipleDataResult();
    searchResult.setQueryDuration(searchResultComponents.getQueryDuration() + searchResultApplications.getQueryDuration());
    searchResult.setTypes(ArrayUtils.addAll(searchResultComponents.getTypes(), searchResultApplications.getTypes()));
    searchResult.setData(ArrayUtils.addAll(searchResultComponents.getData(), searchResultApplications.getData()));
    searchResult.setTotalResults(searchResultComponents.getTotalResults() + searchResultApplications.getTotalResults());
    return RestResponseBuilder.<GetMultipleDataResult>builder().data(searchResult).build();
}
Also used : FilterBuilder(org.elasticsearch.index.query.FilterBuilder) GetMultipleDataResult(alien4cloud.dao.model.GetMultipleDataResult) ApiOperation(io.swagger.annotations.ApiOperation) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 15 with FilterBuilder

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

the class CsarService method getDependantCsars.

/**
 * @return an array of CSARs that depend on this name:version.
 */
public Csar[] getDependantCsars(String name, String version) {
    FilterBuilder notSelf = FilterBuilders.notFilter(FilterBuilders.andFilter(FilterBuilders.termFilter("name", name), FilterBuilders.termFilter("version", version)));
    GetMultipleDataResult<Csar> result = csarDAO.buildQuery(Csar.class).prepareSearch().setFilters(fromKeyValueCouples("dependencies.name", name, "dependencies.version", version), notSelf).search(0, Integer.MAX_VALUE);
    return result.getData();
}
Also used : Csar(org.alien4cloud.tosca.model.Csar) FilterBuilder(org.elasticsearch.index.query.FilterBuilder)

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