Search in sources :

Example 11 with TermQueryBuilder

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

the class SignificantTermsIT method testTextAnalysisPercentageScore.

public void testTextAnalysisPercentageScore() throws Exception {
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("description", "terje")).setFrom(0).setSize(60).setExplain(true).addAggregation(significantTerms("mySignificantTerms").field("description").executionHint(randomExecutionHint()).significanceHeuristic(new PercentageScore()).minDocCount(2)).execute().actionGet();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    checkExpectedStringTermsFound(topTerms);
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) PercentageScore(org.elasticsearch.search.aggregations.bucket.significant.heuristics.PercentageScore) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 12 with TermQueryBuilder

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

the class SignificantTermsIT method testMutualInformation.

public void testMutualInformation() throws Exception {
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("description", "terje")).setFrom(0).setSize(60).setExplain(true).addAggregation(significantTerms("mySignificantTerms").field("description").executionHint(randomExecutionHint()).significanceHeuristic(new MutualInformation(false, true)).minDocCount(1)).execute().actionGet();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    checkExpectedStringTermsFound(topTerms);
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) MutualInformation(org.elasticsearch.search.aggregations.bucket.significant.heuristics.MutualInformation) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 13 with TermQueryBuilder

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

the class SignificantTermsIT method testBadFilteredAnalysis.

public void testBadFilteredAnalysis() throws Exception {
    // Deliberately using a bad choice of filter here for the background context in order
    // to test robustness.
    // We search for the name of a snowboarder but use music-related content (fact_category:1)
    // as the background source of term statistics.
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("description", "terje")).setFrom(0).setSize(60).setExplain(true).addAggregation(significantTerms("mySignificantTerms").field("description").minDocCount(2).backgroundFilter(QueryBuilders.termQuery("fact_category", 1))).execute().actionGet();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    // We expect at least one of the significant terms to have been selected on the basis
    // that it is present in the foreground selection but entirely missing from the filtered
    // background used as context.
    boolean hasMissingBackgroundTerms = false;
    for (Bucket topTerm : topTerms) {
        if (topTerm.getSupersetDf() == 0) {
            hasMissingBackgroundTerms = true;
            break;
        }
    }
    assertTrue(hasMissingBackgroundTerms);
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) Bucket(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 14 with TermQueryBuilder

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

the class HighlightBuilderTests method mutateCommonOptions.

@SuppressWarnings({ "unchecked", "rawtypes" })
private static void mutateCommonOptions(AbstractHighlighterBuilder highlightBuilder) {
    switch(randomIntBetween(1, 16)) {
        case 1:
            highlightBuilder.preTags(randomStringArray(4, 6));
            break;
        case 2:
            highlightBuilder.postTags(randomStringArray(4, 6));
            break;
        case 3:
            highlightBuilder.fragmentSize(randomIntBetween(101, 200));
            break;
        case 4:
            highlightBuilder.numOfFragments(randomIntBetween(11, 20));
            break;
        case 5:
            highlightBuilder.highlighterType(randomAsciiOfLengthBetween(11, 20));
            break;
        case 6:
            highlightBuilder.fragmenter(randomAsciiOfLengthBetween(11, 20));
            break;
        case 7:
            highlightBuilder.highlightQuery(new TermQueryBuilder(randomAsciiOfLengthBetween(11, 20), randomAsciiOfLengthBetween(11, 20)));
            break;
        case 8:
            if (highlightBuilder.order() == Order.NONE) {
                highlightBuilder.order(Order.SCORE);
            } else {
                highlightBuilder.order(Order.NONE);
            }
            break;
        case 9:
            highlightBuilder.highlightFilter(toggleOrSet(highlightBuilder.highlightFilter()));
            break;
        case 10:
            highlightBuilder.forceSource(toggleOrSet(highlightBuilder.forceSource()));
            break;
        case 11:
            highlightBuilder.boundaryMaxScan(randomIntBetween(11, 20));
            break;
        case 12:
            highlightBuilder.boundaryChars(randomAsciiOfLengthBetween(11, 20).toCharArray());
            break;
        case 13:
            highlightBuilder.noMatchSize(randomIntBetween(11, 20));
            break;
        case 14:
            highlightBuilder.phraseLimit(randomIntBetween(11, 20));
            break;
        case 15:
            int items = 6;
            Map<String, Object> options = new HashMap<>(items);
            for (int i = 0; i < items; i++) {
                options.put(randomAsciiOfLengthBetween(1, 10), randomAsciiOfLengthBetween(1, 10));
            }
            highlightBuilder.options(options);
            break;
        case 16:
            highlightBuilder.requireFieldMatch(toggleOrSet(highlightBuilder.requireFieldMatch()));
            break;
    }
}
Also used : HashMap(java.util.HashMap) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder)

Example 15 with TermQueryBuilder

use of org.elasticsearch.index.query.TermQueryBuilder 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)

Aggregations

TermQueryBuilder (org.elasticsearch.index.query.TermQueryBuilder)38 SearchResponse (org.elasticsearch.action.search.SearchResponse)20 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)20 SignificantTerms (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms)14 BytesReference (org.elasticsearch.common.bytes.BytesReference)7 MatchAllQueryBuilder (org.elasticsearch.index.query.MatchAllQueryBuilder)6 Sampler (org.elasticsearch.search.aggregations.bucket.sampler.Sampler)6 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 DirectoryReader (org.apache.lucene.index.DirectoryReader)5 IndexWriter (org.apache.lucene.index.IndexWriter)5 Directory (org.apache.lucene.store.Directory)5 ElasticsearchDirectoryReader (org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader)5 ShardRequestCache (org.elasticsearch.index.cache.request.ShardRequestCache)5 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)5 WrapperQueryBuilder (org.elasticsearch.index.query.WrapperQueryBuilder)5 ShardId (org.elasticsearch.index.shard.ShardId)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Term (org.apache.lucene.index.Term)4 Bucket (org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket)4