Search in sources :

Example 1 with SearchShardTask

use of org.opensearch.action.search.SearchShardTask in project OpenSearch by opensearch-project.

the class SearchSlowLogTests method testSlowLogsWithStats.

public void testSlowLogsWithStats() throws IOException {
    IndexService index = createIndex("foo");
    SearchContext searchContext = createSearchContext(index, "group1");
    SearchSourceBuilder source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
    searchContext.request().source(source);
    searchContext.setTask(new SearchShardTask(0, "n/a", "n/a", "test", null, Collections.singletonMap(Task.X_OPAQUE_ID, "my_id")));
    SearchSlowLog.SearchSlowLogMessage p = new SearchSlowLog.SearchSlowLogMessage(searchContext, 10);
    assertThat(p.getValueFor("stats"), equalTo("[\\\"group1\\\"]"));
    searchContext = createSearchContext(index, "group1", "group2");
    source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
    searchContext.request().source(source);
    searchContext.setTask(new SearchShardTask(0, "n/a", "n/a", "test", null, Collections.singletonMap(Task.X_OPAQUE_ID, "my_id")));
    p = new SearchSlowLog.SearchSlowLogMessage(searchContext, 10);
    assertThat(p.getValueFor("stats"), equalTo("[\\\"group1\\\", \\\"group2\\\"]"));
}
Also used : SearchContext(org.opensearch.search.internal.SearchContext) TestSearchContext(org.opensearch.test.TestSearchContext) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchShardTask(org.opensearch.action.search.SearchShardTask)

Example 2 with SearchShardTask

use of org.opensearch.action.search.SearchShardTask in project OpenSearch by opensearch-project.

the class SearchSlowLogTests method searchContextWithSourceAndTask.

private SearchContext searchContextWithSourceAndTask(IndexService index) {
    SearchContext ctx = createSearchContext(index);
    SearchSourceBuilder source = SearchSourceBuilder.searchSource().query(QueryBuilders.matchAllQuery());
    ctx.request().source(source);
    ctx.setTask(new SearchShardTask(0, "n/a", "n/a", "test", null, Collections.singletonMap(Task.X_OPAQUE_ID, "my_id")));
    return ctx;
}
Also used : SearchContext(org.opensearch.search.internal.SearchContext) TestSearchContext(org.opensearch.test.TestSearchContext) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) SearchShardTask(org.opensearch.action.search.SearchShardTask)

Example 3 with SearchShardTask

use of org.opensearch.action.search.SearchShardTask in project OpenSearch by opensearch-project.

the class SearchServiceTests method testMatchNoDocsEmptyResponse.

public void testMatchNoDocsEmptyResponse() throws InterruptedException {
    createIndex("index");
    Thread currentThread = Thread.currentThread();
    SearchService service = getInstanceFromNode(SearchService.class);
    IndicesService indicesService = getInstanceFromNode(IndicesService.class);
    IndexService indexService = indicesService.indexServiceSafe(resolveIndex("index"));
    IndexShard indexShard = indexService.getShard(0);
    SearchRequest searchRequest = new SearchRequest().allowPartialSearchResults(false).source(new SearchSourceBuilder().aggregation(AggregationBuilders.count("count").field("value")));
    ShardSearchRequest shardRequest = new ShardSearchRequest(OriginalIndices.NONE, searchRequest, indexShard.shardId(), 5, AliasFilter.EMPTY, 1.0f, 0, null, null);
    SearchShardTask task = new SearchShardTask(123L, "", "", "", null, Collections.emptyMap());
    {
        CountDownLatch latch = new CountDownLatch(1);
        shardRequest.source().query(new MatchAllQueryBuilder());
        service.executeQueryPhase(shardRequest, randomBoolean(), task, new ActionListener<SearchPhaseResult>() {

            @Override
            public void onResponse(SearchPhaseResult result) {
                try {
                    assertNotSame(Thread.currentThread(), currentThread);
                    assertThat(Thread.currentThread().getName(), startsWith("opensearch[node_s_0][search]"));
                    assertThat(result, instanceOf(QuerySearchResult.class));
                    assertFalse(result.queryResult().isNull());
                    assertNotNull(result.queryResult().topDocs());
                    assertNotNull(result.queryResult().aggregations());
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onFailure(Exception exc) {
                try {
                    throw new AssertionError(exc);
                } finally {
                    latch.countDown();
                }
            }
        });
        latch.await();
    }
    {
        CountDownLatch latch = new CountDownLatch(1);
        shardRequest.source().query(new MatchNoneQueryBuilder());
        service.executeQueryPhase(shardRequest, randomBoolean(), task, new ActionListener<SearchPhaseResult>() {

            @Override
            public void onResponse(SearchPhaseResult result) {
                try {
                    assertNotSame(Thread.currentThread(), currentThread);
                    assertThat(Thread.currentThread().getName(), startsWith("opensearch[node_s_0][search]"));
                    assertThat(result, instanceOf(QuerySearchResult.class));
                    assertFalse(result.queryResult().isNull());
                    assertNotNull(result.queryResult().topDocs());
                    assertNotNull(result.queryResult().aggregations());
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onFailure(Exception exc) {
                try {
                    throw new AssertionError(exc);
                } finally {
                    latch.countDown();
                }
            }
        });
        latch.await();
    }
    {
        CountDownLatch latch = new CountDownLatch(1);
        shardRequest.canReturnNullResponseIfMatchNoDocs(true);
        service.executeQueryPhase(shardRequest, randomBoolean(), task, new ActionListener<SearchPhaseResult>() {

            @Override
            public void onResponse(SearchPhaseResult result) {
                try {
                    // make sure we don't use the search threadpool
                    assertSame(Thread.currentThread(), currentThread);
                    assertThat(result, instanceOf(QuerySearchResult.class));
                    assertTrue(result.queryResult().isNull());
                } finally {
                    latch.countDown();
                }
            }

            @Override
            public void onFailure(Exception e) {
                try {
                    throw new AssertionError(e);
                } finally {
                    latch.countDown();
                }
            }
        });
        latch.await();
    }
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) IndexService(org.opensearch.index.IndexService) IndexShard(org.opensearch.index.shard.IndexShard) IndicesService(org.opensearch.indices.IndicesService) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) ActionListener(org.opensearch.action.ActionListener) QuerySearchResult(org.opensearch.search.query.QuerySearchResult) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchShardTask(org.opensearch.action.search.SearchShardTask) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 4 with SearchShardTask

use of org.opensearch.action.search.SearchShardTask in project OpenSearch by opensearch-project.

the class SearchServiceTests method testSearchWhileIndexDeleted.

public void testSearchWhileIndexDeleted() throws InterruptedException {
    createIndex("index");
    client().prepareIndex("index").setId("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").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, 1000);
        SearchRequest searchRequest = new SearchRequest().allowPartialSearchResults(true);
        SearchRequest scrollSearchRequest = new SearchRequest().allowPartialSearchResults(true).scroll(new Scroll(TimeValue.timeValueMinutes(1)));
        for (int i = 0; i < rounds; i++) {
            try {
                try {
                    PlainActionFuture<SearchPhaseResult> result = new PlainActionFuture<>();
                    final boolean useScroll = randomBoolean();
                    service.executeQueryPhase(new ShardSearchRequest(OriginalIndices.NONE, useScroll ? scrollSearchRequest : searchRequest, indexShard.shardId(), 1, new AliasFilter(null, Strings.EMPTY_ARRAY), 1.0f, -1, null, null), true, new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()), result);
                    SearchPhaseResult searchPhaseResult = result.get();
                    IntArrayList intCursors = new IntArrayList(1);
                    intCursors.add(0);
                    ShardFetchRequest req = new ShardFetchRequest(searchPhaseResult.getContextId(), intCursors, null);
                    PlainActionFuture<FetchSearchResult> listener = new PlainActionFuture<>();
                    service.executeFetchPhase(req, new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()), listener);
                    listener.get();
                    if (useScroll) {
                        // have to free context since this test does not remove the index from IndicesService.
                        service.freeReaderContext(searchPhaseResult.getContextId());
                    }
                } catch (ExecutionException ex) {
                    assertThat(ex.getCause(), instanceOf(RuntimeException.class));
                    throw ((RuntimeException) ex.getCause());
                }
            } catch (AlreadyClosedException ex) {
                throw ex;
            } catch (IllegalStateException ex) {
                assertEquals("reader_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);
    }
    assertEquals(0, service.getActiveContexts());
    SearchStats.Stats totalStats = indexShard.searchStats().getTotal();
    assertEquals(0, totalStats.getQueryCurrent());
    assertEquals(0, totalStats.getScrollCurrent());
    assertEquals(0, totalStats.getFetchCurrent());
}
Also used : ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) AliasFilter(org.opensearch.search.internal.AliasFilter) IndexService(org.opensearch.index.IndexService) FetchSearchResult(org.opensearch.search.fetch.FetchSearchResult) Semaphore(java.util.concurrent.Semaphore) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) SearchShardTask(org.opensearch.action.search.SearchShardTask) IndexShard(org.opensearch.index.shard.IndexShard) IndicesService(org.opensearch.indices.IndicesService) ShardFetchRequest(org.opensearch.search.fetch.ShardFetchRequest) SearchStats(org.opensearch.index.search.stats.SearchStats) CountDownLatch(java.util.concurrent.CountDownLatch) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) IndexNotFoundException(org.opensearch.index.IndexNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexResponse(org.opensearch.action.index.IndexResponse) PlainActionFuture(org.opensearch.action.support.PlainActionFuture) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 5 with SearchShardTask

use of org.opensearch.action.search.SearchShardTask in project OpenSearch by opensearch-project.

the class QueryPhaseTests method testMinScore.

public void testMinScore() throws Exception {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig();
    RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
    for (int i = 0; i < 10; i++) {
        Document doc = new Document();
        doc.add(new StringField("foo", "bar", Store.NO));
        doc.add(new StringField("filter", "f1", Store.NO));
        w.addDocument(doc);
    }
    w.close();
    IndexReader reader = DirectoryReader.open(dir);
    TestSearchContext context = new TestSearchContext(null, indexShard, newContextSearcher(reader));
    context.parsedQuery(new ParsedQuery(new BooleanQuery.Builder().add(new TermQuery(new Term("foo", "bar")), Occur.MUST).add(new TermQuery(new Term("filter", "f1")), Occur.SHOULD).build()));
    context.minimumScore(0.01f);
    context.setTask(new SearchShardTask(123L, "", "", "", null, Collections.emptyMap()));
    context.setSize(1);
    context.trackTotalHitsUpTo(5);
    QueryPhase.executeInternal(context);
    assertEquals(10, context.queryResult().topDocs().topDocs.totalHits.value);
    reader.close();
    dir.close();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) MultiTermQuery(org.apache.lucene.search.MultiTermQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) TermQuery(org.apache.lucene.search.TermQuery) ParsedQuery(org.opensearch.index.query.ParsedQuery) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) LatLonPoint(org.apache.lucene.document.LatLonPoint) LongPoint(org.apache.lucene.document.LongPoint) TestSearchContext(org.opensearch.test.TestSearchContext) StringField(org.apache.lucene.document.StringField) IndexReader(org.apache.lucene.index.IndexReader) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig) SearchShardTask(org.opensearch.action.search.SearchShardTask)

Aggregations

SearchShardTask (org.opensearch.action.search.SearchShardTask)20 TestSearchContext (org.opensearch.test.TestSearchContext)15 IndexReader (org.apache.lucene.index.IndexReader)13 ParsedQuery (org.opensearch.index.query.ParsedQuery)13 Document (org.apache.lucene.document.Document)12 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)12 Directory (org.apache.lucene.store.Directory)12 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)11 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)11 LatLonPoint (org.apache.lucene.document.LatLonPoint)10 LongPoint (org.apache.lucene.document.LongPoint)10 Sort (org.apache.lucene.search.Sort)8 SortField (org.apache.lucene.search.SortField)8 IOException (java.io.IOException)5 StringField (org.apache.lucene.document.StringField)5 Term (org.apache.lucene.index.Term)5 ExecutionException (java.util.concurrent.ExecutionException)4 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)4 BooleanQuery (org.apache.lucene.search.BooleanQuery)4 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)4