Search in sources :

Example 71 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project vertigo by KleeGroup.

the class ESSearchRequestBuilder method appendSearchQuery.

private static void appendSearchQuery(final SearchQuery searchQuery, final SearchRequestBuilder searchRequestBuilder, final boolean useHighlight) {
    final BoolQueryBuilder filterBoolQueryBuilder = QueryBuilders.boolQuery();
    final BoolQueryBuilder postFilterBoolQueryBuilder = QueryBuilders.boolQuery();
    // on ajoute les critères de la recherche AVEC impact sur le score
    final QueryBuilder queryBuilder = appendSearchQuery(searchQuery, filterBoolQueryBuilder);
    // on ajoute les filtres de sécurité SANS impact sur le score
    appendSecurityFilter(searchQuery.getSecurityListFilter(), filterBoolQueryBuilder);
    // on ajoute les filtres des facettes SANS impact sur le score
    appendSelectedFacetValues(searchQuery.getFacetedQuery(), filterBoolQueryBuilder, postFilterBoolQueryBuilder);
    final QueryBuilder requestQueryBuilder;
    if (searchQuery.isBoostMostRecent()) {
        requestQueryBuilder = appendBoostMostRecent(searchQuery, queryBuilder);
    } else {
        requestQueryBuilder = filterBoolQueryBuilder;
    }
    searchRequestBuilder.setQuery(requestQueryBuilder).setPostFilter(postFilterBoolQueryBuilder);
    if (useHighlight) {
        // .setHighlighterFilter(true) //We don't highlight the security filter
        // .addHighlightedField("*"); HOW TO ?
        searchRequestBuilder.highlighter(new HighlightBuilder().numOfFragments(3));
    }
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) HighlightBuilder(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)

Example 72 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project vertigo by KleeGroup.

the class ESSearchRequestBuilder method appendSecurityFilter.

private static void appendSecurityFilter(final Optional<ListFilter> securityListFilter, final BoolQueryBuilder filterBoolQueryBuilder) {
    if (securityListFilter.isPresent()) {
        final QueryBuilder securityFilterBuilder = translateToQueryBuilder(securityListFilter.get());
        filterBoolQueryBuilder.filter(securityFilterBuilder);
    // use filteredQuery instead of PostFilter in order to filter aggregations too.
    }
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 73 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project main by JohnPeng739.

the class ElasticAccessorRest method search.

private <T extends Base> SearchResponse search(List<GeneralAccessor.ConditionTuple> tuples, Class<T> clazz, Pagination pagination) throws UserInterfaceDalErrorException {
    SearchSourceBuilder builder = new SearchSourceBuilder();
    if (tuples == null || tuples.isEmpty()) {
        builder.query(QueryBuilders.matchAllQuery());
    } else {
        BoolQueryBuilder query = QueryBuilders.boolQuery();
        tuples.forEach(tuple -> query.must(QueryBuilders.termQuery(tuple.field, tuple.value)));
        builder.query(query);
    }
    if (pagination != null) {
        builder.from((pagination.getPage() - 1) * pagination.getSize());
        builder.size(pagination.getSize());
    }
    SearchRequest request = new SearchRequest(index);
    request.types(clazz.getName());
    request.source(builder);
    try {
        SearchResponse response = client.search(request);
        return response;
    } catch (Exception ex) {
        if (logger.isErrorEnabled()) {
            logger.error("Search fail from elastic.", ex);
        }
        throw new UserInterfaceDalErrorException(UserInterfaceDalErrorException.DalErrors.DB_OPERATE_FAIL);
    }
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) IOException(java.io.IOException) UserInterfaceDalErrorException(org.mx.dal.error.UserInterfaceDalErrorException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 74 with BoolQueryBuilder

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

the class ElasticsearchDaoTest method buildBoolQueryFromMoreThanNineDigitSsnBuildsQueryWithNoClauses.

@Test
public void buildBoolQueryFromMoreThanNineDigitSsnBuildsQueryWithNoClauses() {
    BoolQueryBuilder actualQuery = target.buildBoolQueryFromSearchTerms("111223333111 ");
    assertThat(actualQuery.hasClauses(), is(equalTo(false)));
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) Test(org.junit.Test)

Example 75 with BoolQueryBuilder

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

Aggregations

BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)201 SearchResponse (org.elasticsearch.action.search.SearchResponse)73 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)58 SearchHit (org.elasticsearch.search.SearchHit)41 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)38 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)30 Map (java.util.Map)29 ArrayList (java.util.ArrayList)27 HashMap (java.util.HashMap)25 List (java.util.List)23 SearchRequest (org.elasticsearch.action.search.SearchRequest)21 Test (org.junit.Test)18 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)16 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)16 QueryBuilders (org.elasticsearch.index.query.QueryBuilders)14 FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)13 Logger (org.slf4j.Logger)12 LoggerFactory (org.slf4j.LoggerFactory)12 LinkedList (java.util.LinkedList)11 FieldSortBuilder (org.elasticsearch.search.sort.FieldSortBuilder)11