Search in sources :

Example 31 with QueryBuilder

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

the class FiltersAggregatorTests method testRandom.

public void testRandom() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    int numDocs = randomIntBetween(100, 200);
    int maxTerm = randomIntBetween(10, 50);
    int[] expectedBucketCount = new int[maxTerm];
    Document document = new Document();
    for (int i = 0; i < numDocs; i++) {
        if (frequently()) {
            // make sure we have more than one segment to test the merge
            indexWriter.commit();
        }
        int value = randomInt(maxTerm - 1);
        expectedBucketCount[value] += 1;
        document.add(new Field("field", Integer.toString(value), fieldType));
        indexWriter.addDocument(document);
        document.clear();
    }
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    int numFilters = randomIntBetween(1, 10);
    QueryBuilder[] filters = new QueryBuilder[numFilters];
    int[] filterTerms = new int[numFilters];
    int expectedOtherCount = numDocs;
    Set<Integer> filterSet = new HashSet<>();
    for (int i = 0; i < filters.length; i++) {
        int value = randomInt(maxTerm - 1);
        filters[i] = QueryBuilders.termQuery("field", Integer.toString(value));
        filterTerms[i] = value;
        if (filterSet.contains(value) == false) {
            expectedOtherCount -= expectedBucketCount[value];
            filterSet.add(value);
        }
    }
    FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", filters);
    builder.otherBucket(true);
    builder.otherBucketKey("other");
    for (boolean doReduce : new boolean[] { true, false }) {
        final InternalFilters response;
        if (doReduce) {
            response = searchAndReduce(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
        } else {
            response = search(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
        }
        List<InternalFilters.InternalBucket> buckets = response.getBuckets();
        assertEquals(buckets.size(), filters.length + 1);
        for (InternalFilters.InternalBucket bucket : buckets) {
            if ("other".equals(bucket.getKey())) {
                assertEquals(bucket.getDocCount(), expectedOtherCount);
            } else {
                int index = Integer.parseInt(bucket.getKey());
                assertEquals(bucket.getDocCount(), (long) expectedBucketCount[filterTerms[index]]);
            }
        }
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Field(org.apache.lucene.document.Field) InternalFilters(org.elasticsearch.search.aggregations.bucket.filters.InternalFilters) IndexReader(org.apache.lucene.index.IndexReader) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) HashSet(java.util.HashSet)

Example 32 with QueryBuilder

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

the class FiltersIT method testEmptyFilterDeclarations.

// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
    QueryBuilder emptyFilter = new BoolQueryBuilder();
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("all", emptyFilter), new KeyedFilter("tag1", termQuery("tag", "tag1"))))).execute().actionGet();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    Filters.Bucket allBucket = filters.getBucketByKey("all");
    assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
    Filters.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 33 with QueryBuilder

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

the class HighlightBuilderTests method setRandomCommonOptions.

@SuppressWarnings({ "rawtypes" })
private static void setRandomCommonOptions(AbstractHighlighterBuilder highlightBuilder) {
    if (randomBoolean()) {
        // need to set this together, otherwise parsing will complain
        highlightBuilder.preTags(randomStringArray(0, 3));
        highlightBuilder.postTags(randomStringArray(0, 3));
    }
    if (randomBoolean()) {
        highlightBuilder.fragmentSize(randomIntBetween(0, 100));
    }
    if (randomBoolean()) {
        highlightBuilder.numOfFragments(randomIntBetween(0, 10));
    }
    if (randomBoolean()) {
        highlightBuilder.highlighterType(randomAsciiOfLengthBetween(1, 10));
    }
    if (randomBoolean()) {
        highlightBuilder.fragmenter(randomAsciiOfLengthBetween(1, 10));
    }
    if (randomBoolean()) {
        QueryBuilder highlightQuery;
        switch(randomInt(2)) {
            case 0:
                highlightQuery = new MatchAllQueryBuilder();
                break;
            case 1:
                highlightQuery = new IdsQueryBuilder();
                break;
            default:
            case 2:
                highlightQuery = new TermQueryBuilder(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
                break;
        }
        highlightQuery.boost((float) randomDoubleBetween(0, 10, false));
        highlightBuilder.highlightQuery(highlightQuery);
    }
    if (randomBoolean()) {
        if (randomBoolean()) {
            highlightBuilder.order(randomFrom(Order.values()));
        } else {
            // also test the string setter
            highlightBuilder.order(randomFrom(Order.values()).toString());
        }
    }
    if (randomBoolean()) {
        highlightBuilder.highlightFilter(randomBoolean());
    }
    if (randomBoolean()) {
        highlightBuilder.forceSource(randomBoolean());
    }
    if (randomBoolean()) {
        if (randomBoolean()) {
            highlightBuilder.boundaryScannerType(randomFrom(BoundaryScannerType.values()));
        } else {
            // also test the string setter
            highlightBuilder.boundaryScannerType(randomFrom(BoundaryScannerType.values()).toString());
        }
    }
    if (randomBoolean()) {
        highlightBuilder.boundaryMaxScan(randomIntBetween(0, 10));
    }
    if (randomBoolean()) {
        highlightBuilder.boundaryChars(randomAsciiOfLengthBetween(1, 10).toCharArray());
    }
    if (randomBoolean()) {
        highlightBuilder.boundaryScannerLocale(randomLocale(random()).toLanguageTag());
    }
    if (randomBoolean()) {
        highlightBuilder.noMatchSize(randomIntBetween(0, 10));
    }
    if (randomBoolean()) {
        highlightBuilder.phraseLimit(randomIntBetween(0, 10));
    }
    if (randomBoolean()) {
        int items = randomIntBetween(0, 5);
        Map<String, Object> options = new HashMap<>(items);
        for (int i = 0; i < items; i++) {
            Object value = null;
            switch(randomInt(2)) {
                case 0:
                    value = randomAsciiOfLengthBetween(1, 10);
                    break;
                case 1:
                    value = new Integer(randomInt(1000));
                    break;
                case 2:
                    value = new Boolean(randomBoolean());
                    break;
            }
            options.put(randomAsciiOfLengthBetween(1, 10), value);
        }
    }
    if (randomBoolean()) {
        highlightBuilder.requireFieldMatch(randomBoolean());
    }
}
Also used : IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) HashMap(java.util.HashMap) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 34 with QueryBuilder

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

the class HighlighterSearchIT method testGeoFieldHighlightingWithDifferentHighlighters.

public void testGeoFieldHighlightingWithDifferentHighlighters() throws IOException {
    // check that we do not get an exception for geo_point fields in case someone tries to highlight
    // it accidentially with a wildcard
    // see https://github.com/elastic/elasticsearch/issues/17537
    XContentBuilder mappings = jsonBuilder();
    mappings.startObject();
    mappings.startObject("type").startObject("properties").startObject("geo_point").field("type", "geo_point").endObject().startObject("text").field("type", "text").field("term_vector", "with_positions_offsets_payloads").field("index_options", "offsets").endObject().endObject().endObject();
    mappings.endObject();
    assertAcked(prepareCreate("test").addMapping("type", mappings));
    client().prepareIndex("test", "type", "1").setSource(jsonBuilder().startObject().field("text", "Arbitrary text field which will should not cause a failure").endObject()).get();
    refresh();
    String highlighterType = randomFrom("plain", "fvh", "postings", "unified");
    QueryBuilder query = QueryBuilders.boolQuery().should(QueryBuilders.geoBoundingBoxQuery("geo_point").setCorners(61.10078883158897, -170.15625, -64.92354174306496, 118.47656249999999)).should(QueryBuilders.termQuery("text", "failure"));
    SearchResponse search = client().prepareSearch().setSource(new SearchSourceBuilder().query(query).highlighter(new HighlightBuilder().field("*").highlighterType(highlighterType))).get();
    assertNoFailures(search);
    assertThat(search.getHits().getTotalHits(), equalTo(1L));
    assertThat(search.getHits().getAt(0).getHighlightFields().get("text").fragments().length, equalTo(1));
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 35 with QueryBuilder

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

the class HighlighterSearchIT method testGeoFieldHighlightingWhenQueryGetsRewritten.

public void testGeoFieldHighlightingWhenQueryGetsRewritten() throws IOException {
    // same as above but in this example the query gets rewritten during highlighting
    // see https://github.com/elastic/elasticsearch/issues/17537#issuecomment-244939633
    XContentBuilder mappings = jsonBuilder();
    mappings.startObject();
    mappings.startObject("jobs").startObject("properties").startObject("loc").field("type", "geo_point").endObject().startObject("jd").field("type", "text").endObject().endObject().endObject();
    mappings.endObject();
    assertAcked(prepareCreate("test").addMapping("jobs", mappings));
    ensureYellow();
    client().prepareIndex("test", "jobs", "1").setSource(jsonBuilder().startObject().field("jd", "some आवश्यकता है- आर्य समाज अनाथालय, 68 सिविल लाइन्स, बरेली को एक पुरूष" + " रस text").field("loc", "12.934059,77.610741").endObject()).get();
    refresh();
    QueryBuilder query = QueryBuilders.functionScoreQuery(QueryBuilders.boolQuery().filter(QueryBuilders.geoBoundingBoxQuery("loc").setCorners(new GeoPoint(48.934059, 41.610741), new GeoPoint(-23.065941, 113.610741))));
    SearchResponse search = client().prepareSearch().setSource(new SearchSourceBuilder().query(query).highlighter(new HighlightBuilder().highlighterType("plain").field("jd"))).get();
    assertNoFailures(search);
    assertThat(search.getHits().getTotalHits(), equalTo(1L));
}
Also used : GeoPoint(org.elasticsearch.common.geo.GeoPoint) IdsQueryBuilder(org.elasticsearch.index.query.IdsQueryBuilder) MultiMatchQueryBuilder(org.elasticsearch.index.query.MultiMatchQueryBuilder) MatchQueryBuilder(org.elasticsearch.index.query.MatchQueryBuilder) FunctionScoreQueryBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

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