Search in sources :

Example 26 with TermQueryBuilder

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

the class FunctionScoreQueryBuilderTests method testParseSingleFunction.

public void testParseSingleFunction() throws IOException {
    String functionScoreQuery = "{\n" + "    \"function_score\":{\n" + "        \"query\":{\n" + "            \"term\":{\n" + "                \"field1\":\"value1\"\n" + "            }\n" + "        },\n" + "        \"gauss\":  {\n" + "            \"field_name\":  {\n" + "                \"origin\":0.5,\n" + "                \"scale\":0.6\n" + "            }\n" + "         },\n" + "        \"boost\" : 3,\n" + "        \"score_mode\" : \"avg\",\n" + "        \"boost_mode\" : \"replace\",\n" + "        \"max_boost\" : 10\n" + "    }\n" + "}";
    QueryBuilder queryBuilder = parseQuery(functionScoreQuery);
    /*
         * given that we copy part of the decay functions as bytes, we test that fromXContent and toXContent both work no matter what the
         * initial format was
         */
    for (int i = 0; i <= XContentType.values().length; i++) {
        assertThat(queryBuilder, instanceOf(FunctionScoreQueryBuilder.class));
        FunctionScoreQueryBuilder functionScoreQueryBuilder = (FunctionScoreQueryBuilder) queryBuilder;
        assertThat(functionScoreQueryBuilder.query(), instanceOf(TermQueryBuilder.class));
        TermQueryBuilder termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.query();
        assertThat(termQueryBuilder.fieldName(), equalTo("field1"));
        assertThat(termQueryBuilder.value(), equalTo("value1"));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders().length, equalTo(1));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter(), instanceOf(MatchAllQueryBuilder.class));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(GaussDecayFunctionBuilder.class));
        GaussDecayFunctionBuilder gaussDecayFunctionBuilder = (GaussDecayFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
        assertThat(gaussDecayFunctionBuilder.getFieldName(), equalTo("field_name"));
        assertThat(gaussDecayFunctionBuilder.getWeight(), nullValue());
        assertThat(functionScoreQueryBuilder.boost(), equalTo(3f));
        assertThat(functionScoreQueryBuilder.scoreMode(), equalTo(FiltersFunctionScoreQuery.ScoreMode.AVG));
        assertThat(functionScoreQueryBuilder.boostMode(), equalTo(CombineFunction.REPLACE));
        assertThat(functionScoreQueryBuilder.maxBoost(), equalTo(10f));
        if (i < XContentType.values().length) {
            BytesReference bytes = ((AbstractQueryBuilder) queryBuilder).buildAsBytes(XContentType.values()[i]);
            try (XContentParser parser = createParser(XContentType.values()[i].xContent(), bytes)) {
                queryBuilder = parseQuery(parser);
            }
        }
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) RandomQueryBuilder(org.elasticsearch.index.query.RandomQueryBuilder) WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) GeoPoint(org.elasticsearch.common.geo.GeoPoint) XContentParser(org.elasticsearch.common.xcontent.XContentParser) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 27 with TermQueryBuilder

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

the class SamplerIT method testPartiallyUnmappedChildAggNoDiversity.

public void testPartiallyUnmappedChildAggNoDiversity() throws Exception {
    SamplerAggregationBuilder sampleAgg = sampler("sample").shardSize(100);
    sampleAgg.subAggregation(terms("authors").field("author"));
    SearchResponse response = client().prepareSearch("idx_unmapped", "test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("genre", "fantasy")).setFrom(0).setSize(60).setExplain(true).addAggregation(sampleAgg).execute().actionGet();
    assertSearchResponse(response);
    Sampler sample = response.getAggregations().get("sample");
    assertThat(sample.getDocCount(), greaterThan(0L));
    Terms authors = sample.getAggregations().get("authors");
    assertThat(authors.getBuckets().size(), greaterThan(0));
}
Also used : Sampler(org.elasticsearch.search.aggregations.bucket.sampler.Sampler) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) SamplerAggregationBuilder(org.elasticsearch.search.aggregations.bucket.sampler.SamplerAggregationBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 28 with TermQueryBuilder

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

the class SignificantTermsIT method testTextAnalysisChiSquare.

public void testTextAnalysisChiSquare() 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 ChiSquare(false, true)).minDocCount(2)).execute().actionGet();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    checkExpectedStringTermsFound(topTerms);
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) ChiSquare(org.elasticsearch.search.aggregations.bucket.significant.heuristics.ChiSquare) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 29 with TermQueryBuilder

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

the class SignificantTermsIT method testIncludeExclude.

public void testIncludeExclude() throws Exception {
    SearchResponse response = client().prepareSearch("test").setQuery(new TermQueryBuilder("description", "weller")).addAggregation(significantTerms("mySignificantTerms").field("description").executionHint(randomExecutionHint()).includeExclude(new IncludeExclude(null, "weller"))).get();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    Set<String> terms = new HashSet<>();
    for (Bucket topTerm : topTerms) {
        terms.add(topTerm.getKeyAsString());
    }
    assertThat(terms, hasSize(6));
    assertThat(terms.contains("jam"), is(true));
    assertThat(terms.contains("council"), is(true));
    assertThat(terms.contains("style"), is(true));
    assertThat(terms.contains("paul"), is(true));
    assertThat(terms.contains("of"), is(true));
    assertThat(terms.contains("the"), is(true));
    response = client().prepareSearch("test").setQuery(new TermQueryBuilder("description", "weller")).addAggregation(significantTerms("mySignificantTerms").field("description").executionHint(randomExecutionHint()).includeExclude(new IncludeExclude("weller", null))).get();
    assertSearchResponse(response);
    topTerms = response.getAggregations().get("mySignificantTerms");
    terms = new HashSet<>();
    for (Bucket topTerm : topTerms) {
        terms.add(topTerm.getKeyAsString());
    }
    assertThat(terms, hasSize(1));
    assertThat(terms.contains("weller"), is(true));
}
Also used : SignificantTerms(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms) Bucket(org.elasticsearch.search.aggregations.bucket.significant.SignificantTerms.Bucket) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) HashSet(java.util.HashSet)

Example 30 with TermQueryBuilder

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

the class SignificantTermsIT method testFilteredAnalysis.

public void testFilteredAnalysis() throws Exception {
    SearchResponse response = client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(new TermQueryBuilder("description", "weller")).setFrom(0).setSize(60).setExplain(true).addAggregation(significantTerms("mySignificantTerms").field("description").minDocCount(1).backgroundFilter(QueryBuilders.termsQuery("description", "paul"))).execute().actionGet();
    assertSearchResponse(response);
    SignificantTerms topTerms = response.getAggregations().get("mySignificantTerms");
    HashSet<String> topWords = new HashSet<String>();
    for (Bucket topTerm : topTerms) {
        topWords.add(topTerm.getKeyAsString());
    }
    //The word "paul" should be a constant of all docs in the background set and therefore not seen as significant
    assertFalse(topWords.contains("paul"));
    //"Weller" is the only Paul who was in The Jam and therefore this should be identified as a differentiator from the background of all other Pauls.
    assertTrue(topWords.contains("jam"));
}
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) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) HashSet(java.util.HashSet)

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