Search in sources :

Example 41 with QueryBuilder

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

the class RandomQueryGenerator method randomDisMaxQuery.

private static QueryBuilder randomDisMaxQuery(List<String> stringFields, List<String> numericFields, int numDocs, int depth) {
    QueryBuilder q = QueryBuilders.disMaxQuery();
    int numClauses = randomIntBetween(1, 10);
    for (int i = 0; i < numClauses; i++) {
        ((DisMaxQueryBuilder) q).add(randomQueryBuilder(stringFields, numericFields, numDocs, depth - 1));
    }
    if (randomBoolean()) {
        ((DisMaxQueryBuilder) q).boost(randomFloat());
    }
    if (randomBoolean()) {
        ((DisMaxQueryBuilder) q).tieBreaker(randomFloat());
    }
    return q;
}
Also used : DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder) CommonTermsQueryBuilder(org.elasticsearch.index.query.CommonTermsQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FuzzyQueryBuilder(org.elasticsearch.index.query.FuzzyQueryBuilder)

Example 42 with QueryBuilder

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

the class RandomQueryGenerator method randomBoolQuery.

private static QueryBuilder randomBoolQuery(List<String> stringFields, List<String> numericFields, int numDocs, int depth) {
    QueryBuilder q = QueryBuilders.boolQuery();
    int numClause = randomIntBetween(0, 5);
    for (int i = 0; i < numClause; i++) {
        ((BoolQueryBuilder) q).must(randomQueryBuilder(stringFields, numericFields, numDocs, depth - 1));
    }
    numClause = randomIntBetween(0, 5);
    for (int i = 0; i < numClause; i++) {
        ((BoolQueryBuilder) q).should(randomQueryBuilder(stringFields, numericFields, numDocs, depth - 1));
    }
    numClause = randomIntBetween(0, 5);
    for (int i = 0; i < numClause; i++) {
        ((BoolQueryBuilder) q).mustNot(randomQueryBuilder(stringFields, numericFields, numDocs, depth - 1));
    }
    return q;
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) DisMaxQueryBuilder(org.elasticsearch.index.query.DisMaxQueryBuilder) CommonTermsQueryBuilder(org.elasticsearch.index.query.CommonTermsQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) FuzzyQueryBuilder(org.elasticsearch.index.query.FuzzyQueryBuilder)

Example 43 with QueryBuilder

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

the class QueryProfilerIT method testCollapsingBool.

/**
     * Tests a series of three nested boolean queries with a single "leaf" match query.
     * The rewrite process will "collapse" this down to a single bool, so this tests to make sure
     * nothing catastrophic happens during that fairly substantial rewrite
     */
public void testCollapsingBool() 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);
    }
    indexRandom(true, docs);
    refresh();
    QueryBuilder q = QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().must(QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("field1", "one"))));
    logger.info("Query: {}", q);
    SearchResponse resp = client().prepareSearch().setQuery(q).setProfile(true).setSearchType(SearchType.QUERY_THEN_FETCH).execute().actionGet();
    assertNotNull("Profile response element should not be null", resp.getProfileResults());
    assertThat("Profile response should not be an empty array", resp.getProfileResults().size(), not(0));
    for (Map.Entry<String, ProfileShardResult> shardResult : resp.getProfileResults().entrySet()) {
        for (QueryProfileShardResult searchProfiles : shardResult.getValue().getQueryProfileResults()) {
            for (ProfileResult result : searchProfiles.getQueryResults()) {
                assertNotNull(result.getQueryName());
                assertNotNull(result.getLuceneDescription());
                assertThat(result.getTime(), greaterThan(0L));
                assertNotNull(result.getTimeBreakdown());
            }
            CollectorResult result = searchProfiles.getCollectorResult();
            assertThat(result.getName(), not(isEmptyOrNullString()));
            assertThat(result.getTime(), greaterThan(0L));
        }
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) ProfileResult(org.elasticsearch.search.profile.ProfileResult) RandomQueryGenerator.randomQueryBuilder(org.elasticsearch.search.profile.query.RandomQueryGenerator.randomQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Matchers.isEmptyOrNullString(org.hamcrest.Matchers.isEmptyOrNullString) Map(java.util.Map) MultiSearchResponse(org.elasticsearch.action.search.MultiSearchResponse) SearchResponse(org.elasticsearch.action.search.SearchResponse) ProfileShardResult(org.elasticsearch.search.profile.ProfileShardResult)

Example 44 with QueryBuilder

use of 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 45 with QueryBuilder

use of 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)

Aggregations

QueryBuilder (org.elasticsearch.index.query.QueryBuilder)344 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)233 Test (org.testng.annotations.Test)156 DynamicEntity (org.molgenis.data.support.DynamicEntity)137 SearchResponse (org.elasticsearch.action.search.SearchResponse)52 QueryImpl (org.molgenis.data.support.QueryImpl)36 ArrayList (java.util.ArrayList)28 Map (java.util.Map)28 RangeQueryBuilder (org.elasticsearch.index.query.RangeQueryBuilder)27 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)26 HashMap (java.util.HashMap)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)22 SearchHit (org.elasticsearch.search.SearchHit)20 IOException (java.io.IOException)19 AbstractQueryBuilder (org.elasticsearch.index.query.AbstractQueryBuilder)19 MatchQueryBuilder (org.elasticsearch.index.query.MatchQueryBuilder)17 Test (org.junit.Test)16 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)15 List (java.util.List)14 HasParentQueryBuilder (org.elasticsearch.index.query.HasParentQueryBuilder)14