Search in sources :

Example 6 with TestSearchContext

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

Example 7 with TestSearchContext

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

the class SearchOperationListenerTests method testListenersAreExecuted.

// this test also tests if calls are correct if one or more listeners throw exceptions
public void testListenersAreExecuted() {
    AtomicInteger preQuery = new AtomicInteger();
    AtomicInteger failedQuery = new AtomicInteger();
    AtomicInteger onQuery = new AtomicInteger();
    AtomicInteger onFetch = new AtomicInteger();
    AtomicInteger preFetch = new AtomicInteger();
    AtomicInteger failedFetch = new AtomicInteger();
    AtomicInteger newContext = new AtomicInteger();
    AtomicInteger freeContext = new AtomicInteger();
    AtomicInteger newScrollContext = new AtomicInteger();
    AtomicInteger freeScrollContext = new AtomicInteger();
    AtomicInteger timeInNanos = new AtomicInteger(randomIntBetween(0, 10));
    SearchOperationListener listener = new SearchOperationListener() {

        @Override
        public void onPreQueryPhase(SearchContext searchContext) {
            assertNotNull(searchContext);
            preQuery.incrementAndGet();
        }

        @Override
        public void onFailedQueryPhase(SearchContext searchContext) {
            assertNotNull(searchContext);
            failedQuery.incrementAndGet();
        }

        @Override
        public void onQueryPhase(SearchContext searchContext, long tookInNanos) {
            assertEquals(timeInNanos.get(), tookInNanos);
            assertNotNull(searchContext);
            onQuery.incrementAndGet();
        }

        @Override
        public void onPreFetchPhase(SearchContext searchContext) {
            assertNotNull(searchContext);
            preFetch.incrementAndGet();
        }

        @Override
        public void onFailedFetchPhase(SearchContext searchContext) {
            assertNotNull(searchContext);
            failedFetch.incrementAndGet();
        }

        @Override
        public void onFetchPhase(SearchContext searchContext, long tookInNanos) {
            assertEquals(timeInNanos.get(), tookInNanos);
            onFetch.incrementAndGet();
        }

        @Override
        public void onNewContext(SearchContext context) {
            assertNotNull(context);
            newContext.incrementAndGet();
        }

        @Override
        public void onFreeContext(SearchContext context) {
            assertNotNull(context);
            freeContext.incrementAndGet();
        }

        @Override
        public void onNewScrollContext(SearchContext context) {
            assertNotNull(context);
            newScrollContext.incrementAndGet();
        }

        @Override
        public void onFreeScrollContext(SearchContext context) {
            assertNotNull(context);
            freeScrollContext.incrementAndGet();
        }
    };
    SearchOperationListener throwingListener = (SearchOperationListener) Proxy.newProxyInstance(SearchOperationListener.class.getClassLoader(), new Class[] { SearchOperationListener.class }, (a, b, c) -> {
        throw new RuntimeException();
    });
    final List<SearchOperationListener> indexingOperationListeners = new ArrayList<>(Arrays.asList(listener, listener));
    if (randomBoolean()) {
        indexingOperationListeners.add(throwingListener);
        if (randomBoolean()) {
            indexingOperationListeners.add(throwingListener);
        }
    }
    Collections.shuffle(indexingOperationListeners, random());
    SearchOperationListener.CompositeListener compositeListener = new SearchOperationListener.CompositeListener(indexingOperationListeners, logger);
    SearchContext ctx = new TestSearchContext(null);
    compositeListener.onQueryPhase(ctx, timeInNanos.get());
    assertEquals(0, preFetch.get());
    assertEquals(0, preQuery.get());
    assertEquals(0, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(0, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFetchPhase(ctx, timeInNanos.get());
    assertEquals(0, preFetch.get());
    assertEquals(0, preQuery.get());
    assertEquals(0, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onPreQueryPhase(ctx);
    assertEquals(0, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(0, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onPreFetchPhase(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(0, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFailedFetchPhase(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(0, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFailedQueryPhase(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(0, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onNewContext(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(2, newContext.get());
    assertEquals(0, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onNewScrollContext(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(2, newContext.get());
    assertEquals(2, newScrollContext.get());
    assertEquals(0, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFreeContext(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(2, newContext.get());
    assertEquals(2, newScrollContext.get());
    assertEquals(2, freeContext.get());
    assertEquals(0, freeScrollContext.get());
    compositeListener.onFreeScrollContext(ctx);
    assertEquals(2, preFetch.get());
    assertEquals(2, preQuery.get());
    assertEquals(2, failedFetch.get());
    assertEquals(2, failedQuery.get());
    assertEquals(2, onQuery.get());
    assertEquals(2, onFetch.get());
    assertEquals(2, newContext.get());
    assertEquals(2, newScrollContext.get());
    assertEquals(2, freeContext.get());
    assertEquals(2, freeScrollContext.get());
}
Also used : Arrays(java.util.Arrays) List(java.util.List) Proxy(java.lang.reflect.Proxy) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Client(org.elasticsearch.client.Client) Term(org.apache.lucene.index.Term) SearchContext(org.elasticsearch.search.internal.SearchContext) ESTestCase(org.elasticsearch.test.ESTestCase) Collections(java.util.Collections) Engine(org.elasticsearch.index.engine.Engine) TestSearchContext(org.elasticsearch.test.TestSearchContext) ArrayList(java.util.ArrayList) TestSearchContext(org.elasticsearch.test.TestSearchContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) SearchContext(org.elasticsearch.search.internal.SearchContext) TestSearchContext(org.elasticsearch.test.TestSearchContext)

Example 8 with TestSearchContext

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

the class ExceptionSerializationTests method testSearchParseException.

public void testSearchParseException() throws IOException {
    SearchContext ctx = new TestSearchContext(null);
    SearchParseException ex = serialize(new SearchParseException(ctx, "foo", new XContentLocation(66, 666)));
    assertEquals("foo", ex.getMessage());
    assertEquals(66, ex.getLineNumber());
    assertEquals(666, ex.getColumnNumber());
    assertEquals(ctx.shardTarget(), ex.shard());
}
Also used : TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchParseException(org.elasticsearch.search.SearchParseException) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchContext(org.elasticsearch.search.internal.SearchContext) XContentLocation(org.elasticsearch.common.xcontent.XContentLocation)

Example 9 with TestSearchContext

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

the class ElasticsearchExceptionTests method randomExceptions.

public static Tuple<Throwable, ElasticsearchException> randomExceptions() {
    Throwable actual;
    ElasticsearchException expected;
    int type = randomIntBetween(0, 5);
    switch(type) {
        case 0:
            actual = new ClusterBlockException(singleton(DiscoverySettings.NO_MASTER_BLOCK_WRITES));
            expected = new ElasticsearchException("Elasticsearch exception [type=cluster_block_exception, " + "reason=blocked by: [SERVICE_UNAVAILABLE/2/no master];]");
            break;
        case 1:
            actual = new CircuitBreakingException("Data too large", 123, 456);
            expected = new ElasticsearchException("Elasticsearch exception [type=circuit_breaking_exception, reason=Data too large]");
            break;
        case 2:
            actual = new SearchParseException(new TestSearchContext(null), "Parse failure", new XContentLocation(12, 98));
            expected = new ElasticsearchException("Elasticsearch exception [type=search_parse_exception, reason=Parse failure]");
            break;
        case 3:
            actual = new IllegalArgumentException("Closed resource", new RuntimeException("Resource"));
            expected = new ElasticsearchException("Elasticsearch exception [type=illegal_argument_exception, reason=Closed resource]", new ElasticsearchException("Elasticsearch exception [type=runtime_exception, reason=Resource]"));
            break;
        case 4:
            actual = new SearchPhaseExecutionException("search", "all shards failed", new ShardSearchFailure[] { new ShardSearchFailure(new ParsingException(1, 2, "foobar", null), new SearchShardTarget("node_1", new Index("foo", "_na_"), 1)) });
            expected = new ElasticsearchException("Elasticsearch exception [type=search_phase_execution_exception, " + "reason=all shards failed]");
            expected.addMetadata("es.phase", "search");
            break;
        case 5:
            actual = new ElasticsearchException("Parsing failed", new ParsingException(9, 42, "Wrong state", new NullPointerException("Unexpected null value")));
            ElasticsearchException expectedCause = new ElasticsearchException("Elasticsearch exception [type=parsing_exception, " + "reason=Wrong state]", new ElasticsearchException("Elasticsearch exception [type=null_pointer_exception, " + "reason=Unexpected null value]"));
            expected = new ElasticsearchException("Elasticsearch exception [type=exception, reason=Parsing failed]", expectedCause);
            break;
        default:
            throw new UnsupportedOperationException("No randomized exceptions generated for type [" + type + "]");
    }
    if (actual instanceof ElasticsearchException) {
        ElasticsearchException actualException = (ElasticsearchException) actual;
        if (randomBoolean()) {
            int nbHeaders = randomIntBetween(1, 5);
            Map<String, List<String>> randomHeaders = new HashMap<>(nbHeaders);
            for (int i = 0; i < nbHeaders; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAsciiOfLength(5) : "");
                }
                randomHeaders.put("header_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomHeaders.entrySet()) {
                actualException.addHeader(entry.getKey(), entry.getValue());
                expected.addHeader(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null headers are not printed out by the toXContent method
                actualException.addHeader("ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbMetadata = randomIntBetween(1, 5);
            Map<String, List<String>> randomMetadata = new HashMap<>(nbMetadata);
            for (int i = 0; i < nbMetadata; i++) {
                List<String> values = new ArrayList<>();
                int nbValues = randomIntBetween(1, 3);
                for (int j = 0; j < nbValues; j++) {
                    values.add(frequently() ? randomAsciiOfLength(5) : "");
                }
                randomMetadata.put("es.metadata_" + i, values);
            }
            for (Map.Entry<String, List<String>> entry : randomMetadata.entrySet()) {
                actualException.addMetadata(entry.getKey(), entry.getValue());
                expected.addMetadata(entry.getKey(), entry.getValue());
            }
            if (rarely()) {
                // Empty or null metadata are not printed out by the toXContent method
                actualException.addMetadata("es.ignored", randomBoolean() ? emptyList() : null);
            }
        }
        if (randomBoolean()) {
            int nbResources = randomIntBetween(1, 5);
            for (int i = 0; i < nbResources; i++) {
                String resourceType = "type_" + i;
                String[] resourceIds = new String[randomIntBetween(1, 3)];
                for (int j = 0; j < resourceIds.length; j++) {
                    resourceIds[j] = frequently() ? randomAsciiOfLength(5) : "";
                }
                actualException.setResources(resourceType, resourceIds);
                expected.setResources(resourceType, resourceIds);
            }
        }
    }
    return new Tuple<>(actual, expected);
}
Also used : HashMap(java.util.HashMap) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ArrayList(java.util.ArrayList) Index(org.elasticsearch.index.Index) ParsingException(org.elasticsearch.common.ParsingException) Collections.singletonList(java.util.Collections.singletonList) Collections.emptyList(java.util.Collections.emptyList) List(java.util.List) ArrayList(java.util.ArrayList) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) XContentLocation(org.elasticsearch.common.xcontent.XContentLocation) ClusterBlockException(org.elasticsearch.cluster.block.ClusterBlockException) TestSearchContext(org.elasticsearch.test.TestSearchContext) SearchParseException(org.elasticsearch.search.SearchParseException) CircuitBreakingException(org.elasticsearch.common.breaker.CircuitBreakingException) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Map(java.util.Map) HashMap(java.util.HashMap) Tuple(org.elasticsearch.common.collect.Tuple)

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