Search in sources :

Example 16 with TermQueryBuilder

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

the class FunctionScoreQueryBuilderTests method testParseFunctionsArray.

public void testParseFunctionsArray() throws IOException {
    String functionScoreQuery = "{\n" + "    \"function_score\":{\n" + "        \"query\":{\n" + "            \"term\":{\n" + "                \"field1\":\"value1\"\n" + "            }\n" + "        },\n" + "        \"functions\":  [\n" + "            {\n" + "                \"random_score\":  {\n" + "                    \"seed\":123456\n" + "                },\n" + "                \"weight\": 3,\n" + "                \"filter\": {\n" + "                    \"term\":{\n" + "                        \"field2\":\"value2\"\n" + "                    }\n" + "                }\n" + "            },\n" + "            {\n" + "                \"filter\": {\n" + "                    \"term\":{\n" + "                        \"field3\":\"value3\"\n" + "                    }\n" + "                },\n" + "                \"weight\": 9\n" + "            },\n" + "            {\n" + "                \"gauss\":  {\n" + "                    \"field_name\":  {\n" + "                        \"origin\":0.5,\n" + "                        \"scale\":0.6\n" + "                    }\n" + "                }\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(3));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter(), instanceOf(TermQueryBuilder.class));
        termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getFilter();
        assertThat(termQueryBuilder.fieldName(), equalTo("field2"));
        assertThat(termQueryBuilder.value(), equalTo("value2"));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[1].getFilter(), instanceOf(TermQueryBuilder.class));
        termQueryBuilder = (TermQueryBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[1].getFilter();
        assertThat(termQueryBuilder.fieldName(), equalTo("field3"));
        assertThat(termQueryBuilder.value(), equalTo("value3"));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[2].getFilter(), instanceOf(MatchAllQueryBuilder.class));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction(), instanceOf(RandomScoreFunctionBuilder.class));
        RandomScoreFunctionBuilder randomScoreFunctionBuilder = (RandomScoreFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[0].getScoreFunction();
        assertThat(randomScoreFunctionBuilder.getSeed(), equalTo(123456));
        assertThat(randomScoreFunctionBuilder.getWeight(), equalTo(3f));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[1].getScoreFunction(), instanceOf(WeightBuilder.class));
        WeightBuilder weightBuilder = (WeightBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[1].getScoreFunction();
        assertThat(weightBuilder.getWeight(), equalTo(9f));
        assertThat(functionScoreQueryBuilder.filterFunctionBuilders()[2].getScoreFunction(), instanceOf(GaussDecayFunctionBuilder.class));
        GaussDecayFunctionBuilder gaussDecayFunctionBuilder = (GaussDecayFunctionBuilder) functionScoreQueryBuilder.filterFunctionBuilders()[2].getScoreFunction();
        assertThat(gaussDecayFunctionBuilder.getFieldName(), equalTo("field_name"));
        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 17 with TermQueryBuilder

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

the class FunctionScoreQueryBuilderTests method testRewriteWithFunction.

public void testRewriteWithFunction() throws IOException {
    QueryBuilder firstFunction = new WrapperQueryBuilder(new TermQueryBuilder("tq", "1").toString());
    TermQueryBuilder secondFunction = new TermQueryBuilder("tq", "2");
    QueryBuilder queryBuilder = randomBoolean() ? new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString()) : new TermQueryBuilder("foo", "bar");
    FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(queryBuilder, new FunctionScoreQueryBuilder.FilterFunctionBuilder[] { new FunctionScoreQueryBuilder.FilterFunctionBuilder(firstFunction, new RandomScoreFunctionBuilder()), new FunctionScoreQueryBuilder.FilterFunctionBuilder(secondFunction, new RandomScoreFunctionBuilder()) });
    FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(createShardContext());
    assertNotSame(functionScoreQueryBuilder, rewrite);
    assertEquals(rewrite.query(), new TermQueryBuilder("foo", "bar"));
    assertEquals(rewrite.filterFunctionBuilders()[0].getFilter(), new TermQueryBuilder("tq", "1"));
    assertSame(rewrite.filterFunctionBuilders()[1].getFilter(), secondFunction);
}
Also used : WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) 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) FilterFunctionBuilder(org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder.FilterFunctionBuilder)

Example 18 with TermQueryBuilder

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

the class NestedHelperTests method testNested.

public void testNested() throws IOException {
    QueryShardContext context = indexService.newQueryShardContext(0, new MultiReader(), () -> 0);
    NestedQueryBuilder queryBuilder = new NestedQueryBuilder("nested1", new MatchAllQueryBuilder(), ScoreMode.Avg);
    ESToParentBlockJoinQuery query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    Query expectedChildQuery = new BooleanQuery.Builder().add(new MatchAllDocsQuery(), Occur.MUST).add(new TermQuery(new Term("_type", "__nested1")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested1", new TermQueryBuilder("nested1.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // this time we do not add a filter since the inner query only matches inner docs
    expectedChildQuery = new TermQuery(new Term("nested1.foo", "bar"));
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested2", new TermQueryBuilder("nested2.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // we need to add the filter again because of include_in_parent
    expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested2.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested2")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
    queryBuilder = new NestedQueryBuilder("nested3", new TermQueryBuilder("nested3.foo", "bar"), ScoreMode.Avg);
    query = (ESToParentBlockJoinQuery) queryBuilder.toQuery(context);
    // we need to add the filter again because of include_in_root
    expectedChildQuery = new BooleanQuery.Builder().add(new TermQuery(new Term("nested3.foo", "bar")), Occur.MUST).add(new TermQuery(new Term("_type", "__nested3")), Occur.FILTER).build();
    assertEquals(expectedChildQuery, query.getChildQuery());
    assertFalse(new NestedHelper(mapperService).mightMatchNestedDocs(query));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested1"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested2"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested3"));
    assertTrue(new NestedHelper(mapperService).mightMatchNonNestedDocs(query, "nested_missing"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiReader(org.apache.lucene.index.MultiReader) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) NestedQueryBuilder(org.elasticsearch.index.query.NestedQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder) Term(org.apache.lucene.index.Term) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) NestedQueryBuilder(org.elasticsearch.index.query.NestedQueryBuilder) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) MatchAllQueryBuilder(org.elasticsearch.index.query.MatchAllQueryBuilder)

Example 19 with TermQueryBuilder

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

the class IndicesRequestCacheTests method testInvalidate.

public void testInvalidate() throws Exception {
    ShardRequestCache requestCacheStats = new ShardRequestCache();
    IndicesRequestCache cache = new IndicesRequestCache(Settings.EMPTY);
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig());
    writer.addDocument(newDoc(0, "foo"));
    DirectoryReader reader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId("foo", "bar", 1));
    TermQueryBuilder termQuery = new TermQueryBuilder("id", "0");
    AtomicBoolean indexShard = new AtomicBoolean(true);
    // initial cache
    TestEntity entity = new TestEntity(requestCacheStats, indexShard);
    Loader loader = new Loader(reader, 0);
    BytesReference value = cache.getOrCompute(entity, loader, reader, termQuery.buildAsBytes());
    assertEquals("foo", value.streamInput().readString());
    assertEquals(0, requestCacheStats.stats().getHitCount());
    assertEquals(1, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertFalse(loader.loadedFromCache);
    assertEquals(1, cache.count());
    // cache hit
    entity = new TestEntity(requestCacheStats, indexShard);
    loader = new Loader(reader, 0);
    value = cache.getOrCompute(entity, loader, reader, termQuery.buildAsBytes());
    assertEquals("foo", value.streamInput().readString());
    assertEquals(1, requestCacheStats.stats().getHitCount());
    assertEquals(1, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertTrue(loader.loadedFromCache);
    assertEquals(1, cache.count());
    assertTrue(requestCacheStats.stats().getMemorySize().bytesAsInt() > value.length());
    assertEquals(1, cache.numRegisteredCloseListeners());
    // load again after invalidate
    entity = new TestEntity(requestCacheStats, indexShard);
    loader = new Loader(reader, 0);
    cache.invalidate(entity, reader, termQuery.buildAsBytes());
    value = cache.getOrCompute(entity, loader, reader, termQuery.buildAsBytes());
    assertEquals("foo", value.streamInput().readString());
    assertEquals(1, requestCacheStats.stats().getHitCount());
    assertEquals(2, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertFalse(loader.loadedFromCache);
    assertEquals(1, cache.count());
    assertTrue(requestCacheStats.stats().getMemorySize().bytesAsInt() > value.length());
    assertEquals(1, cache.numRegisteredCloseListeners());
    // release
    if (randomBoolean()) {
        reader.close();
    } else {
        // closed shard but reader is still open
        indexShard.set(false);
        cache.clear(entity);
    }
    cache.cleanCache();
    assertEquals(1, requestCacheStats.stats().getHitCount());
    assertEquals(2, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertEquals(0, cache.count());
    assertEquals(0, requestCacheStats.stats().getMemorySize().bytesAsInt());
    IOUtils.close(reader, writer, dir, cache);
    assertEquals(0, cache.numRegisteredCloseListeners());
}
Also used : ShardRequestCache(org.elasticsearch.index.cache.request.ShardRequestCache) ShardId(org.elasticsearch.index.shard.ShardId) BytesReference(org.elasticsearch.common.bytes.BytesReference) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Directory(org.apache.lucene.store.Directory)

Example 20 with TermQueryBuilder

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

the class IndicesRequestCacheTests method testClearAllEntityIdentity.

public void testClearAllEntityIdentity() throws Exception {
    IndicesRequestCache cache = new IndicesRequestCache(Settings.EMPTY);
    AtomicBoolean indexShard = new AtomicBoolean(true);
    ShardRequestCache requestCacheStats = new ShardRequestCache();
    Directory dir = newDirectory();
    IndexWriter writer = new IndexWriter(dir, newIndexWriterConfig());
    writer.addDocument(newDoc(0, "foo"));
    DirectoryReader reader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId("foo", "bar", 1));
    TermQueryBuilder termQuery = new TermQueryBuilder("id", "0");
    TestEntity entity = new TestEntity(requestCacheStats, indexShard);
    Loader loader = new Loader(reader, 0);
    writer.updateDocument(new Term("id", "0"), newDoc(0, "bar"));
    DirectoryReader secondReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId("foo", "bar", 1));
    TestEntity secondEntity = new TestEntity(requestCacheStats, indexShard);
    Loader secondLoader = new Loader(secondReader, 0);
    writer.updateDocument(new Term("id", "0"), newDoc(0, "baz"));
    DirectoryReader thirdReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId("foo", "bar", 1));
    AtomicBoolean differentIdentity = new AtomicBoolean(true);
    TestEntity thirddEntity = new TestEntity(requestCacheStats, differentIdentity);
    Loader thirdLoader = new Loader(thirdReader, 0);
    BytesReference value1 = cache.getOrCompute(entity, loader, reader, termQuery.buildAsBytes());
    assertEquals("foo", value1.streamInput().readString());
    BytesReference value2 = cache.getOrCompute(secondEntity, secondLoader, secondReader, termQuery.buildAsBytes());
    assertEquals("bar", value2.streamInput().readString());
    logger.info("Memory size: {}", requestCacheStats.stats().getMemorySize());
    BytesReference value3 = cache.getOrCompute(thirddEntity, thirdLoader, thirdReader, termQuery.buildAsBytes());
    assertEquals("baz", value3.streamInput().readString());
    assertEquals(3, cache.count());
    final long hitCount = requestCacheStats.stats().getHitCount();
    // clear all for the indexShard Idendity even though is't still open
    cache.clear(randomFrom(entity, secondEntity));
    cache.cleanCache();
    assertEquals(1, cache.count());
    // third has not been validated since it's a different identity
    value3 = cache.getOrCompute(thirddEntity, thirdLoader, thirdReader, termQuery.buildAsBytes());
    assertEquals(hitCount + 1, requestCacheStats.stats().getHitCount());
    assertEquals("baz", value3.streamInput().readString());
    IOUtils.close(reader, secondReader, thirdReader, writer, dir, cache);
}
Also used : ShardRequestCache(org.elasticsearch.index.cache.request.ShardRequestCache) BytesReference(org.elasticsearch.common.bytes.BytesReference) DirectoryReader(org.apache.lucene.index.DirectoryReader) ElasticsearchDirectoryReader(org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Term(org.apache.lucene.index.Term) ShardId(org.elasticsearch.index.shard.ShardId) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexWriter(org.apache.lucene.index.IndexWriter) Directory(org.apache.lucene.store.Directory)

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