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