Search in sources :

Example 76 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project symphony by b3log.

the class ElasticsearchTestCase method QueryDslBuilder.

@Test
public void QueryDslBuilder() {
    QueryBuilder qb = multiMatchQuery("test", "test1", "test2");
    System.out.println(qb.toString());
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Test(org.testng.annotations.Test)

Example 77 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.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 78 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.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 79 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.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 80 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.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)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)371 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)58 QueryImpl (org.molgenis.data.support.QueryImpl)36 Map (java.util.Map)32 ArrayList (java.util.ArrayList)30 Test (org.junit.Test)30 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)29 HashMap (java.util.HashMap)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)27 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 IOException (java.io.IOException)23 SearchHit (org.elasticsearch.search.SearchHit)22 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)19 List (java.util.List)17 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15