Search in sources :

Example 21 with TermQueryBuilder

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

the class IndicesRequestCacheTests method testEviction.

public void testEviction() throws Exception {
    final ByteSizeValue size;
    {
        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);
        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());
        size = requestCacheStats.stats().getMemorySize();
        IOUtils.close(reader, secondReader, writer, dir, cache);
    }
    IndicesRequestCache cache = new IndicesRequestCache(Settings.builder().put(IndicesRequestCache.INDICES_CACHE_QUERY_SIZE.getKey(), size.getBytes() + 1 + "b").build());
    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));
    TestEntity thirddEntity = new TestEntity(requestCacheStats, indexShard);
    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(2, cache.count());
    assertEquals(1, requestCacheStats.stats().getEvictions());
    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) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) 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)

Example 22 with TermQueryBuilder

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

the class IndicesRequestCacheTests method testCacheDifferentReaders.

public void testCacheDifferentReaders() 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");
    writer.updateDocument(new Term("id", "0"), newDoc(0, "bar"));
    DirectoryReader secondReader = ElasticsearchDirectoryReader.wrap(DirectoryReader.open(writer), new ShardId("foo", "bar", 1));
    // 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());
    assertTrue(requestCacheStats.stats().getMemorySize().bytesAsInt() > value.length());
    final int cacheSize = requestCacheStats.stats().getMemorySize().bytesAsInt();
    assertEquals(1, cache.numRegisteredCloseListeners());
    // cache the second
    TestEntity secondEntity = new TestEntity(requestCacheStats, indexShard);
    loader = new Loader(secondReader, 0);
    value = cache.getOrCompute(entity, loader, secondReader, termQuery.buildAsBytes());
    assertEquals("bar", value.streamInput().readString());
    assertEquals(0, requestCacheStats.stats().getHitCount());
    assertEquals(2, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertFalse(loader.loadedFromCache);
    assertEquals(2, cache.count());
    assertTrue(requestCacheStats.stats().getMemorySize().bytesAsInt() > cacheSize + value.length());
    assertEquals(2, cache.numRegisteredCloseListeners());
    secondEntity = new TestEntity(requestCacheStats, indexShard);
    loader = new Loader(secondReader, 0);
    value = cache.getOrCompute(secondEntity, loader, secondReader, termQuery.buildAsBytes());
    assertEquals("bar", value.streamInput().readString());
    assertEquals(1, requestCacheStats.stats().getHitCount());
    assertEquals(2, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertTrue(loader.loadedFromCache);
    assertEquals(2, cache.count());
    entity = new TestEntity(requestCacheStats, indexShard);
    loader = new Loader(reader, 0);
    value = cache.getOrCompute(entity, loader, reader, termQuery.buildAsBytes());
    assertEquals("foo", value.streamInput().readString());
    assertEquals(2, requestCacheStats.stats().getHitCount());
    assertEquals(2, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertTrue(loader.loadedFromCache);
    assertEquals(2, cache.count());
    // Closing the cache doesn't change returned entities
    reader.close();
    cache.cleanCache();
    assertEquals(2, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertTrue(loader.loadedFromCache);
    assertEquals(1, cache.count());
    assertEquals(cacheSize, requestCacheStats.stats().getMemorySize().bytesAsInt());
    assertEquals(1, cache.numRegisteredCloseListeners());
    // release
    if (randomBoolean()) {
        secondReader.close();
    } else {
        // closed shard but reader is still open
        indexShard.set(false);
        cache.clear(secondEntity);
    }
    cache.cleanCache();
    assertEquals(2, requestCacheStats.stats().getMissCount());
    assertEquals(0, requestCacheStats.stats().getEvictions());
    assertTrue(loader.loadedFromCache);
    assertEquals(0, cache.count());
    assertEquals(0, requestCacheStats.stats().getMemorySize().bytesAsInt());
    IOUtils.close(secondReader, writer, dir, cache);
    assertEquals(0, cache.numRegisteredCloseListeners());
}
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)

Example 23 with TermQueryBuilder

use of org.elasticsearch.index.query.TermQueryBuilder in project storm by apache.

the class EsIndexBoltTest method testEsIndexBolt.

@Test
public void testEsIndexBolt() throws Exception {
    String index = "index1";
    String type = "type1";
    Tuple tuple = createTestTuple(index, type);
    bolt.execute(tuple);
    verify(outputCollector).ack(tuple);
    node.client().admin().indices().prepareRefresh(index).execute().actionGet();
    CountResponse resp = node.client().prepareCount(index).setQuery(new TermQueryBuilder("_type", type)).execute().actionGet();
    Assert.assertEquals(1, resp.getCount());
}
Also used : CountResponse(org.elasticsearch.action.count.CountResponse) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder) Tuple(org.apache.storm.tuple.Tuple) IntegrationTest(org.apache.storm.testing.IntegrationTest) Test(org.junit.Test)

Example 24 with TermQueryBuilder

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

the class SearchQueryIT method testPassQueryOrFilterAsJSONString.

public void testPassQueryOrFilterAsJSONString() throws Exception {
    createIndex("test");
    client().prepareIndex("test", "type1", "1").setSource("field1", "value1_1", "field2", "value2_1").setRefreshPolicy(IMMEDIATE).get();
    WrapperQueryBuilder wrapper = new WrapperQueryBuilder("{ \"term\" : { \"field1\" : \"value1_1\" } }");
    assertHitCount(client().prepareSearch().setQuery(wrapper).get(), 1L);
    BoolQueryBuilder bool = boolQuery().must(wrapper).must(new TermQueryBuilder("field2", "value2_1"));
    assertHitCount(client().prepareSearch().setQuery(bool).get(), 1L);
    WrapperQueryBuilder wrapperFilter = wrapperQuery("{ \"term\" : { \"field1\" : \"value1_1\" } }");
    assertHitCount(client().prepareSearch().setPostFilter(wrapperFilter).get(), 1L);
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) QueryBuilders.spanMultiTermQueryBuilder(org.elasticsearch.index.query.QueryBuilders.spanMultiTermQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder)

Example 25 with TermQueryBuilder

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

the class FunctionScoreQueryBuilderTests method testRewrite.

public void testRewrite() throws IOException {
    FunctionScoreQueryBuilder functionScoreQueryBuilder = new FunctionScoreQueryBuilder(new WrapperQueryBuilder(new TermQueryBuilder("foo", "bar").toString())).boostMode(CombineFunction.REPLACE).scoreMode(FiltersFunctionScoreQuery.ScoreMode.SUM).setMinScore(1).maxBoost(100);
    FunctionScoreQueryBuilder rewrite = (FunctionScoreQueryBuilder) functionScoreQueryBuilder.rewrite(createShardContext());
    assertNotSame(functionScoreQueryBuilder, rewrite);
    assertEquals(rewrite.query(), new TermQueryBuilder("foo", "bar"));
    assertEquals(rewrite.boostMode(), CombineFunction.REPLACE);
    assertEquals(rewrite.scoreMode(), FiltersFunctionScoreQuery.ScoreMode.SUM);
    assertEquals(rewrite.getMinScore(), 1f, 0.0001);
    assertEquals(rewrite.maxBoost(), 100f, 0.0001);
}
Also used : WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) TermQueryBuilder(org.elasticsearch.index.query.TermQueryBuilder)

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