Search in sources :

Example 41 with QueryBuilder

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

the class QueryProfilerIT method testProfileMatchesRegular.

/**
     * This test generates 1-10 random queries and executes a profiled and non-profiled
     * search for each query.  It then does some basic sanity checking of score and hits
     * to make sure the profiling doesn't interfere with the hits being returned
     */
public void testProfileMatchesRegular() throws Exception {
    createIndex("test");
    ensureGreen();
    int numDocs = randomIntBetween(100, 150);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
        docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i), "field2", i);
    }
    List<String> stringFields = Arrays.asList("field1");
    List<String> numericFields = Arrays.asList("field2");
    indexRandom(true, docs);
    refresh();
    int iters = between(1, 10);
    for (int i = 0; i < iters; i++) {
        QueryBuilder q = randomQueryBuilder(stringFields, numericFields, numDocs, 3);
        logger.info("Query: {}", q);
        SearchRequestBuilder vanilla = client().prepareSearch("test").setQuery(q).setProfile(false).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
        SearchRequestBuilder profile = client().prepareSearch("test").setQuery(q).setProfile(true).addSort("_uid", SortOrder.ASC).setPreference("_primary").setSearchType(SearchType.QUERY_THEN_FETCH);
        MultiSearchResponse.Item[] responses = client().prepareMultiSearch().add(vanilla).add(profile).execute().actionGet().getResponses();
        SearchResponse vanillaResponse = responses[0].getResponse();
        SearchResponse profileResponse = responses[1].getResponse();
        float vanillaMaxScore = vanillaResponse.getHits().getMaxScore();
        float profileMaxScore = profileResponse.getHits().getMaxScore();
        if (Float.isNaN(vanillaMaxScore)) {
            assertTrue("Vanilla maxScore is NaN but Profile is not [" + profileMaxScore + "]", Float.isNaN(profileMaxScore));
        } else {
            assertTrue("Profile maxScore of [" + profileMaxScore + "] is not close to Vanilla maxScore [" + vanillaMaxScore + "]", nearlyEqual(vanillaMaxScore, profileMaxScore, 0.001));
        }
        assertThat("Profile totalHits of [" + profileResponse.getHits().getTotalHits() + "] is not close to Vanilla totalHits [" + vanillaResponse.getHits().getTotalHits() + "]", vanillaResponse.getHits().getTotalHits(), equalTo(profileResponse.getHits().getTotalHits()));
        SearchHit[] vanillaHits = vanillaResponse.getHits().getHits();
        SearchHit[] profileHits = profileResponse.getHits().getHits();
        for (int j = 0; j < vanillaHits.length; j++) {
            assertThat("Profile hit #" + j + " has a different ID from Vanilla", vanillaHits[j].getId(), equalTo(profileHits[j].getId()));
        }
    }
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) RandomQueryGenerator.randomQueryBuilder(org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder)

Example 42 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project zipkin by openzipkin.

the class ElasticsearchSpanStore method getSpanNames.

@Override
public ListenableFuture<List<String>> getSpanNames(String serviceName) {
    if (Strings.isNullOrEmpty(serviceName)) {
        return EMPTY_LIST;
    }
    serviceName = serviceName.toLowerCase();
    QueryBuilder filter = boolQuery().should(nestedQuery("annotations", termQuery("annotations.endpoint.serviceName", serviceName))).should(nestedQuery("binaryAnnotations", termQuery("binaryAnnotations.endpoint.serviceName", serviceName)));
    return client.collectBucketKeys(catchAll, boolQuery().must(matchAllQuery()).filter(filter), AggregationBuilders.terms("name_agg").field("name").size(Integer.MAX_VALUE));
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder)

Example 43 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project graylog2-server by Graylog2.

the class Searches method standardSearchRequest.

private SearchRequestBuilder standardSearchRequest(String query, Set<String> indices, int limit, int offset, TimeRange range, String filter, Sorting sort, boolean highlight) {
    if (query == null || query.trim().isEmpty()) {
        query = "*";
    }
    final QueryBuilder queryBuilder;
    if ("*".equals(query.trim())) {
        queryBuilder = matchAllQuery();
    } else {
        queryBuilder = queryStringQuery(query).allowLeadingWildcard(configuration.isAllowLeadingWildcardSearches());
    }
    final SearchRequestBuilder srb = c.prepareSearch(indices.toArray(new String[indices.size()])).setIndicesOptions(IndicesOptions.lenientExpandOpen()).setQuery(QueryBuilders.boolQuery().must(queryBuilder).filter(standardFilters(range, filter))).setFrom(offset);
    if (limit > 0) {
        srb.setSize(limit);
    }
    if (sort != null) {
        srb.addSort(sort.getField(), sort.asElastic());
    }
    if (highlight && configuration.isAllowHighlighting()) {
        srb.setHighlighterRequireFieldMatch(false);
        srb.addHighlightedField("*", 0, 0);
    }
    return srb;
}
Also used : SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryStringQueryBuilder(org.elasticsearch.index.query.QueryStringQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 44 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project fess by codelibs.

the class BsLabelToRoleCB method build.

// ===================================================================================
//                                                                               Build
//                                                                               =====
@Override
public SearchRequestBuilder build(SearchRequestBuilder builder) {
    if (_conditionQuery != null) {
        QueryBuilder queryBuilder = _conditionQuery.getQuery();
        if (queryBuilder != null) {
            builder.setQuery(queryBuilder);
        }
        _conditionQuery.getFieldSortBuilderList().forEach(sort -> {
            builder.addSort(sort);
        });
    }
    if (_conditionAggregation != null) {
        _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation);
    }
    if (_specification != null) {
        builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null);
    }
    return builder;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

Example 45 with QueryBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.index.query.QueryBuilder in project fess by codelibs.

the class BsWebConfigToLabelCB method build.

// ===================================================================================
//                                                                               Build
//                                                                               =====
@Override
public SearchRequestBuilder build(SearchRequestBuilder builder) {
    if (_conditionQuery != null) {
        QueryBuilder queryBuilder = _conditionQuery.getQuery();
        if (queryBuilder != null) {
            builder.setQuery(queryBuilder);
        }
        _conditionQuery.getFieldSortBuilderList().forEach(sort -> {
            builder.addSort(sort);
        });
    }
    if (_conditionAggregation != null) {
        _conditionAggregation.getAggregationBuilderList().forEach(builder::addAggregation);
    }
    if (_specification != null) {
        builder.setFetchSource(_specification.columnList.toArray(new String[_specification.columnList.size()]), null);
    }
    return builder;
}
Also used : QueryBuilder(org.elasticsearch.index.query.QueryBuilder)

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