Search in sources :

Example 76 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project api-core by ca-cwds.

the class ElasticsearchDaoTest method buildBoolQueryFromTwoSsnBuildsQueryWithTwoSsnClauses.

@Test
public void buildBoolQueryFromTwoSsnBuildsQueryWithTwoSsnClauses() {
    BoolQueryBuilder actualQuery = target.buildBoolQueryFromSearchTerms("123456789   111223333 ");
    QueryBuilder expectedQuery = QueryBuilders.boolQuery().should(QueryBuilders.prefixQuery(ESColumn.SSN.getCol(), "123456789")).should(QueryBuilders.prefixQuery(ESColumn.SSN.getCol(), "111223333"));
    assertThat(actualQuery.toString(), is(equalTo(expectedQuery.toString())));
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.junit.Test)

Example 77 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder 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);
}
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 78 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder 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 79 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder 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 80 with QueryBuilder

use of org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.QueryBuilder in project alien4cloud by alien4cloud.

the class PluginController method tryReusePreviousVersionConf.

@SuppressWarnings({ "rawtypes", "unchecked" })
private void tryReusePreviousVersionConf(Plugin plugin) {
    // search for a previous version
    // TODO manage the case there are many previous versions
    QueryBuilder macthNameQuerybuilder = QueryBuilders.matchQuery("descriptor.id", plugin.getDescriptor().getId());
    QueryBuilder idQueryBuilder = QueryBuilders.idsQuery(MappingBuilder.indexTypeFromClass(Plugin.class)).ids(plugin.getId());
    QueryBuilder boolQuery = QueryBuilders.boolQuery().must(macthNameQuerybuilder).mustNot(idQueryBuilder);
    Plugin oldVersionPlugin = alienDAO.customFind(Plugin.class, boolQuery);
    if (oldVersionPlugin != null && oldVersionPlugin.isConfigurable()) {
        // get the configuration type
        Class<?> configType = pluginManager.getConfigurationType(plugin.getId());
        PluginConfiguration oldPluginConf = alienDAO.findById(PluginConfiguration.class, oldVersionPlugin.getId());
        // try to de-serialize it into the config of the new version
        if (oldPluginConf != null && oldPluginConf.getConfiguration() != null) {
            try {
                Object configObject = JsonUtil.readObject(JsonUtil.toString(oldPluginConf.getConfiguration()), configType);
                IPluginConfigurator configurator = pluginManager.getConfiguratorFor(plugin.getId());
                configurator.setConfiguration(configObject);
                PluginConfiguration pluginConf = new PluginConfiguration(plugin.getId(), configObject);
                alienDAO.save(pluginConf);
            } catch (IOException e) {
                log.warn("Plugin [" + plugin.getId() + "]: Failed to re-use the configuration of the previous version " + oldVersionPlugin.getId() + ". The configuration beans are not comptible", e);
            } catch (PluginConfigurationException e) {
                log.warn("Plugin [" + plugin.getId() + "]: Failed to re-use the configuration of the previous version " + oldVersionPlugin.getId() + ". Error while applying the configuration", e);
            }
        }
    }
}
Also used : IPluginConfigurator(alien4cloud.plugin.IPluginConfigurator) PluginConfiguration(alien4cloud.plugin.model.PluginConfiguration) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) PluginConfigurationException(alien4cloud.plugin.exception.PluginConfigurationException) Plugin(alien4cloud.plugin.Plugin)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)357 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)245 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)55 QueryImpl (org.molgenis.data.support.QueryImpl)36 Map (java.util.Map)32 ArrayList (java.util.ArrayList)30 HashMap (java.util.HashMap)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)27 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 Test (org.junit.Test)25 SearchHit (org.elasticsearch.search.SearchHit)21 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)20 IOException (java.io.IOException)19 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)17 List (java.util.List)16 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)14