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\\\"]"));
}
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;
}
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();
}
}
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());
}
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();
}
Aggregations