Search in sources :

Example 1 with TestSearchContext

use of org.elasticsearch.test.TestSearchContext in project elasticsearch by elastic.

the class MockSearchServiceTests method testAssertNoInFlightContext.

public void testAssertNoInFlightContext() {
    final long nowInMillis = randomNonNegativeLong();
    SearchContext s = new TestSearchContext(new QueryShardContext(0, new IndexSettings(EMPTY_INDEX_METADATA, Settings.EMPTY), null, null, null, null, null, xContentRegistry(), null, null, () -> nowInMillis)) {

        @Override
        public SearchShardTarget shardTarget() {
            return new SearchShardTarget("node", new Index("idx", "ignored"), 0);
        }

        @Override
        public SearchType searchType() {
            return SearchType.DEFAULT;
        }

        @Override
        public Query query() {
            return Queries.newMatchAllQuery();
        }
    };
    MockSearchService.addActiveContext(s);
    try {
        Throwable e = expectThrows(AssertionError.class, () -> MockSearchService.assertNoInFlightContext());
        assertEquals("There are still [1] in-flight contexts. The first one's creation site is listed as the cause of this exception.", e.getMessage());
        e = e.getCause();
        // The next line with throw an exception if the date looks wrong
        assertEquals("[node][idx][0] query=[*:*]", e.getMessage());
        assertEquals(MockSearchService.class.getName(), e.getStackTrace()[0].getClassName());
        assertEquals(MockSearchServiceTests.class.getName(), e.getStackTrace()[1].getClassName());
    } finally {
        MockSearchService.removeActiveContext(s);
    }
}
Also used : TestSearchContext(org.elasticsearch.test.TestSearchContext) IndexSettings(org.elasticsearch.index.IndexSettings) SearchContext(org.elasticsearch.search.internal.SearchContext) TestSearchContext(org.elasticsearch.test.TestSearchContext) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) Index(org.elasticsearch.index.Index)

Example 2 with TestSearchContext

use of org.elasticsearch.test.TestSearchContext in project elasticsearch by elastic.

the class IndexModuleTests method testAddSearchOperationListener.

public void testAddSearchOperationListener() throws IOException {
    IndexModule module = new IndexModule(IndexSettingsModule.newIndexSettings(index, settings), new AnalysisRegistry(environment, emptyMap(), emptyMap(), emptyMap(), emptyMap(), emptyMap()));
    AtomicBoolean executed = new AtomicBoolean(false);
    SearchOperationListener listener = new SearchOperationListener() {

        @Override
        public void onNewContext(SearchContext context) {
            executed.set(true);
        }
    };
    module.addSearchOperationListener(listener);
    expectThrows(IllegalArgumentException.class, () -> module.addSearchOperationListener(listener));
    expectThrows(IllegalArgumentException.class, () -> module.addSearchOperationListener(null));
    IndexService indexService = newIndexService(module);
    assertEquals(2, indexService.getSearchOperationListener().size());
    assertEquals(SearchSlowLog.class, indexService.getSearchOperationListener().get(0).getClass());
    assertSame(listener, indexService.getSearchOperationListener().get(1));
    for (SearchOperationListener l : indexService.getSearchOperationListener()) {
        l.onNewContext(new TestSearchContext(null));
    }
    assertTrue(executed.get());
    indexService.close("simon says", false);
}
Also used : AnalysisRegistry(org.elasticsearch.index.analysis.AnalysisRegistry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestSearchContext(org.elasticsearch.test.TestSearchContext) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchContext(org.elasticsearch.search.internal.SearchContext) SearchOperationListener(org.elasticsearch.index.shard.SearchOperationListener)

Example 3 with TestSearchContext

use of org.elasticsearch.test.TestSearchContext 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 4 with TestSearchContext

use of org.elasticsearch.test.TestSearchContext in project elasticsearch by elastic.

the class SearchSlowLogTests method createSearchContext.

@Override
protected SearchContext createSearchContext(IndexService indexService) {
    BigArrays bigArrays = indexService.getBigArrays();
    ThreadPool threadPool = indexService.getThreadPool();
    return new TestSearchContext(threadPool, bigArrays, indexService) {

        final ShardSearchRequest request = new ShardSearchRequest() {

            private SearchSourceBuilder searchSourceBuilder;

            @Override
            public ShardId shardId() {
                return new ShardId(indexService.index(), 0);
            }

            @Override
            public String[] types() {
                return new String[0];
            }

            @Override
            public SearchSourceBuilder source() {
                return searchSourceBuilder;
            }

            @Override
            public void source(SearchSourceBuilder source) {
                searchSourceBuilder = source;
            }

            @Override
            public int numberOfShards() {
                return 0;
            }

            @Override
            public SearchType searchType() {
                return null;
            }

            @Override
            public QueryBuilder filteringAliases() {
                return null;
            }

            @Override
            public float indexBoost() {
                return 1.0f;
            }

            @Override
            public long nowInMillis() {
                return 0;
            }

            @Override
            public Boolean requestCache() {
                return null;
            }

            @Override
            public Scroll scroll() {
                return null;
            }

            @Override
            public void setProfile(boolean profile) {
            }

            @Override
            public boolean isProfile() {
                return false;
            }

            @Override
            public BytesReference cacheKey() throws IOException {
                return null;
            }

            @Override
            public void rewrite(QueryShardContext context) throws IOException {
            }
        };

        @Override
        public ShardSearchRequest request() {
            return request;
        }
    };
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Scroll(org.elasticsearch.search.Scroll) ThreadPool(org.elasticsearch.threadpool.ThreadPool) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) IOException(java.io.IOException) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) ShardId(org.elasticsearch.index.shard.ShardId) BigArrays(org.elasticsearch.common.util.BigArrays) TestSearchContext(org.elasticsearch.test.TestSearchContext) ShardSearchRequest(org.elasticsearch.search.internal.ShardSearchRequest) QueryShardContext(org.elasticsearch.index.query.QueryShardContext) SearchType(org.elasticsearch.action.search.SearchType)

Example 5 with TestSearchContext

use of org.elasticsearch.test.TestSearchContext 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)

Aggregations

TestSearchContext (org.elasticsearch.test.TestSearchContext)9 List (java.util.List)5 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 SearchContext (org.elasticsearch.search.internal.SearchContext)4 Collector (org.apache.lucene.search.Collector)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 Weight (org.apache.lucene.search.Weight)3 SearchTask (org.elasticsearch.action.search.SearchTask)3 ParsedQuery (org.elasticsearch.index.query.ParsedQuery)3 ArrayList (java.util.ArrayList)2 MultiReader (org.apache.lucene.index.MultiReader)2 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)2 XContentLocation (org.elasticsearch.common.xcontent.XContentLocation)2 Index (org.elasticsearch.index.Index)2 QueryShardContext (org.elasticsearch.index.query.QueryShardContext)2 SearchParseException (org.elasticsearch.search.SearchParseException)2 IOException (java.io.IOException)1 Proxy (java.lang.reflect.Proxy)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1