Search in sources :

Example 1 with SearchTask

use of org.elasticsearch.action.search.SearchTask in project elasticsearch by elastic.

the class SearchServiceTests method testSearchWhileIndexDeleted.

public void testSearchWhileIndexDeleted() throws IOException, InterruptedException {
    createIndex("index");
    client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).get();
    SearchService service = getInstanceFromNode(SearchService.class);
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    IndexShard indexShard = indexService.getShard(0);
    AtomicBoolean running = new AtomicBoolean(true);
    CountDownLatch startGun = new CountDownLatch(1);
    Semaphore semaphore = new Semaphore(Integer.MAX_VALUE);
    final Thread thread = new Thread() {

        @Override
        public void run() {
            startGun.countDown();
            while (running.get()) {
                service.afterIndexRemoved(indexService.index(), indexService.getIndexSettings(), DELETED);
                if (randomBoolean()) {
                    // context in a non-sane way.
                    try {
                        semaphore.acquire();
                    } catch (InterruptedException e) {
                        throw new AssertionError(e);
                    }
                    client().prepareIndex("index", "type").setSource("field", "value").setRefreshPolicy(randomFrom(WriteRequest.RefreshPolicy.values())).execute(new ActionListener<IndexResponse>() {

                        @Override
                        public void onResponse(IndexResponse indexResponse) {
                            semaphore.release();
                        }

                        @Override
                        public void onFailure(Exception e) {
                            semaphore.release();
                        }
                    });
                }
            }
        }
    };
    thread.start();
    startGun.await();
    try {
        final int rounds = scaledRandomIntBetween(100, 10000);
        for (int i = 0; i < rounds; i++) {
            try {
                QuerySearchResultProvider querySearchResultProvider = service.executeQueryPhase(new ShardSearchLocalRequest(indexShard.shardId(), 1, SearchType.DEFAULT, new SearchSourceBuilder(), new String[0], false, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f), new SearchTask(123L, "", "", "", null));
                IntArrayList intCursors = new IntArrayList(1);
                intCursors.add(0);
                ShardFetchRequest req = new ShardFetchRequest(querySearchResultProvider.id(), intCursors, null);
                service.executeFetchPhase(req, new SearchTask(123L, "", "", "", null));
            } catch (AlreadyClosedException ex) {
                throw ex;
            } catch (IllegalStateException ex) {
                assertEquals("search context is already closed can't increment refCount current count [0]", ex.getMessage());
            } catch (SearchContextMissingException ex) {
            // that's fine
            }
        }
    } finally {
        running.set(false);
        thread.join();
        semaphore.acquire(Integer.MAX_VALUE);
    }
}
Also used : AliasFilter(org.elasticsearch.search.internal.AliasFilter) ShardSearchLocalRequest(org.elasticsearch.search.internal.ShardSearchLocalRequest) IndexService(org.elasticsearch.index.IndexService) Semaphore(java.util.concurrent.Semaphore) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchTask(org.elasticsearch.action.search.SearchTask) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) IndexShard(org.elasticsearch.index.shard.IndexShard) IndicesService(org.elasticsearch.indices.IndicesService) ShardFetchRequest(org.elasticsearch.search.fetch.ShardFetchRequest) CountDownLatch(java.util.concurrent.CountDownLatch) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IOException(java.io.IOException) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ExecutionException(java.util.concurrent.ExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexResponse(org.elasticsearch.action.index.IndexResponse) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 2 with SearchTask

use of org.elasticsearch.action.search.SearchTask in project elasticsearch by elastic.

the class QueryPhaseTests method countTestCase.

private void countTestCase(Query query, IndexReader reader, boolean shouldCollect) throws Exception {
    TestSearchContext context = new TestSearchContext(null);
    context.parsedQuery(new ParsedQuery(query));
    context.setSize(0);
    context.setTask(new SearchTask(123L, "", "", "", null));
    IndexSearcher searcher = new IndexSearcher(reader);
    final AtomicBoolean collected = new AtomicBoolean();
    IndexSearcher contextSearcher = new IndexSearcher(reader) {

        protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
            collected.set(true);
            super.search(leaves, weight, collector);
        }
    };
    final boolean rescore = QueryPhase.execute(context, contextSearcher);
    assertFalse(rescore);
    assertEquals(searcher.count(query), context.queryResult().topDocs().totalHits);
    assertEquals(shouldCollect, collected.get());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSearchContext(org.elasticsearch.test.TestSearchContext) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) SearchTask(org.elasticsearch.action.search.SearchTask) Collector(org.apache.lucene.search.Collector) List(java.util.List) Weight(org.apache.lucene.search.Weight)

Example 3 with SearchTask

use of org.elasticsearch.action.search.SearchTask in project elasticsearch by elastic.

the class QueryPhaseTests method testPostFilterDisablesCountOptimization.

public void testPostFilterDisablesCountOptimization() throws Exception {
    TestSearchContext context = new TestSearchContext(null);
    context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery()));
    context.setSize(0);
    context.setTask(new SearchTask(123L, "", "", "", null));
    final AtomicBoolean collected = new AtomicBoolean();
    IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) {

        protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
            collected.set(true);
            super.search(leaves, weight, collector);
        }
    };
    QueryPhase.execute(context, contextSearcher);
    assertEquals(0, context.queryResult().topDocs().totalHits);
    assertFalse(collected.get());
    context.parsedPostFilter(new ParsedQuery(new MatchNoDocsQuery()));
    QueryPhase.execute(context, contextSearcher);
    assertEquals(0, context.queryResult().topDocs().totalHits);
    assertTrue(collected.get());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSearchContext(org.elasticsearch.test.TestSearchContext) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) SearchTask(org.elasticsearch.action.search.SearchTask) MultiReader(org.apache.lucene.index.MultiReader) MatchNoDocsQuery(org.apache.lucene.search.MatchNoDocsQuery) Collector(org.apache.lucene.search.Collector) List(java.util.List) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Weight(org.apache.lucene.search.Weight)

Example 4 with SearchTask

use of org.elasticsearch.action.search.SearchTask in project elasticsearch by elastic.

the class QueryPhaseTests method testMinScoreDisablesCountOptimization.

public void testMinScoreDisablesCountOptimization() throws Exception {
    TestSearchContext context = new TestSearchContext(null);
    context.parsedQuery(new ParsedQuery(new MatchAllDocsQuery()));
    context.setSize(0);
    context.setTask(new SearchTask(123L, "", "", "", null));
    final AtomicBoolean collected = new AtomicBoolean();
    IndexSearcher contextSearcher = new IndexSearcher(new MultiReader()) {

        protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
            collected.set(true);
            super.search(leaves, weight, collector);
        }
    };
    QueryPhase.execute(context, contextSearcher);
    assertEquals(0, context.queryResult().topDocs().totalHits);
    assertFalse(collected.get());
    context.minimumScore(1);
    QueryPhase.execute(context, contextSearcher);
    assertEquals(0, context.queryResult().topDocs().totalHits);
    assertTrue(collected.get());
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSearchContext(org.elasticsearch.test.TestSearchContext) ParsedQuery(org.elasticsearch.index.query.ParsedQuery) SearchTask(org.elasticsearch.action.search.SearchTask) MultiReader(org.apache.lucene.index.MultiReader) Collector(org.apache.lucene.search.Collector) List(java.util.List) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) Weight(org.apache.lucene.search.Weight)

Aggregations

AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 SearchTask (org.elasticsearch.action.search.SearchTask)4 List (java.util.List)3 Collector (org.apache.lucene.search.Collector)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 Weight (org.apache.lucene.search.Weight)3 ParsedQuery (org.elasticsearch.index.query.ParsedQuery)3 TestSearchContext (org.elasticsearch.test.TestSearchContext)3 MultiReader (org.apache.lucene.index.MultiReader)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 IntArrayList (com.carrotsearch.hppc.IntArrayList)1 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutionException (java.util.concurrent.ExecutionException)1 Semaphore (java.util.concurrent.Semaphore)1 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)1 AlreadyClosedException (org.apache.lucene.store.AlreadyClosedException)1 IndexResponse (org.elasticsearch.action.index.IndexResponse)1 SearchPhaseExecutionException (org.elasticsearch.action.search.SearchPhaseExecutionException)1 IndexService (org.elasticsearch.index.IndexService)1