Search in sources :

Example 66 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project muikku by otavanopisto.

the class ElasticSearchProvider method findUser.

@Override
public SearchResult findUser(Long userEntityId, boolean includeInactive) {
    // Query that checks activity based on user having a study end date set
    BoolQueryBuilder query = boolQuery();
    if (!includeInactive) {
        query.mustNot(existsQuery("studyEndDate"));
    }
    query.must(termQuery("userEntityId", userEntityId));
    // Search
    SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes("User");
    // Results processing
    SearchResponse response = requestBuilder.setQuery(query).execute().actionGet();
    List<Map<String, Object>> searchResults = new ArrayList<Map<String, Object>>();
    SearchHits searchHits = response.getHits();
    long totalHitCount = searchHits.getTotalHits();
    SearchHit[] results = searchHits.getHits();
    for (SearchHit hit : results) {
        Map<String, Object> hitSource = hit.getSource();
        if (hitSource == null) {
            hitSource = new HashMap<>();
            for (String key : hit.getFields().keySet()) {
                hitSource.put(key, hit.getFields().get(key).getValue().toString());
            }
        }
        hitSource.put("indexType", hit.getType());
        searchResults.add(hitSource);
    }
    return new SearchResult(0, searchResults.size(), searchResults, totalHitCount);
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResult(fi.otavanopisto.muikku.search.SearchResult) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 67 with BoolQueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.BoolQueryBuilder in project muikku by otavanopisto.

the class ElasticSearchProvider method search.

@Override
public SearchResult search(String query, String[] fields, int start, int maxResults, Class<?>... types) {
    try {
        query = sanitizeSearchString(query);
        String[] typenames = new String[types.length];
        for (int i = 0; i < types.length; i++) {
            typenames[i] = types[i].getSimpleName();
        }
        SearchRequestBuilder requestBuilder = elasticClient.prepareSearch("muikku").setTypes(typenames).setFrom(start).setSize(maxResults);
        BoolQueryBuilder boolQuery = boolQuery();
        for (String field : fields) {
            boolQuery.should(prefixQuery(field, query));
        }
        SearchResponse response = requestBuilder.setQuery(boolQuery).execute().actionGet();
        List<Map<String, Object>> searchResults = new ArrayList<Map<String, Object>>();
        SearchHits searchHits = response.getHits();
        long totalHitCount = searchHits.getTotalHits();
        SearchHit[] results = searchHits.getHits();
        for (SearchHit hit : results) {
            Map<String, Object> hitSource = hit.getSource();
            hitSource.put("indexType", hit.getType());
            searchResults.add(hitSource);
        }
        SearchResult result = new SearchResult(start, maxResults, searchResults, totalHitCount);
        return result;
    } catch (Exception e) {
        logger.log(Level.SEVERE, "ElasticSearch query failed unexpectedly", e);
        return new SearchResult(0, 0, new ArrayList<Map<String, Object>>(), 0);
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) SearchResult(fi.otavanopisto.muikku.search.SearchResult) UnknownHostException(java.net.UnknownHostException) SearchResponse(org.elasticsearch.action.search.SearchResponse) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 68 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
        searchRequestBuilder.setHighlighterNumOfFragments(HIGHLIGHTER_NUM_OF_FRAGMENTS).addHighlightedField("*");
    }
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 69 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 70 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 QueryBuilder appendSearchQuery(final SearchQuery searchQuery, final BoolQueryBuilder filterBoolQueryBuilder) {
    final QueryBuilder queryBuilder = translateToQueryBuilder(searchQuery.getListFilter());
    filterBoolQueryBuilder.must(queryBuilder);
    return queryBuilder;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

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