use of org.opensearch.search.internal.SearchContext in project OpenSearch by opensearch-project.
the class TransportValidateQueryAction method shardOperation.
@Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request, Task task) throws IOException {
boolean valid;
String explanation = null;
String error = null;
ShardSearchRequest shardSearchLocalRequest = new ShardSearchRequest(request.shardId(), request.nowInMillis(), request.filteringAliases());
SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT);
try {
ParsedQuery parsedQuery = searchContext.getQueryShardContext().toQuery(request.query());
searchContext.parsedQuery(parsedQuery);
searchContext.preProcess(request.rewrite());
valid = true;
explanation = explain(searchContext, request.rewrite());
} catch (QueryShardException | ParsingException e) {
valid = false;
error = e.getDetailedMessage();
} catch (AssertionError e) {
valid = false;
error = e.getMessage();
} finally {
Releasables.close(searchContext);
}
return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
}
use of org.opensearch.search.internal.SearchContext in project OpenSearch by opensearch-project.
the class SearchSlowLogTests method testMultipleSlowLoggersUseSingleLog4jLogger.
public void testMultipleSlowLoggersUseSingleLog4jLogger() {
LoggerContext context = (LoggerContext) LogManager.getContext(false);
SearchContext ctx1 = searchContextWithSourceAndTask(createIndex("index-1"));
IndexSettings settings1 = new IndexSettings(createIndexMetadata(SlowLogLevel.WARN, "index-1", UUIDs.randomBase64UUID()), Settings.EMPTY);
SearchSlowLog log1 = new SearchSlowLog(settings1);
int numberOfLoggersBefore = context.getLoggers().size();
SearchContext ctx2 = searchContextWithSourceAndTask(createIndex("index-2"));
IndexSettings settings2 = new IndexSettings(createIndexMetadata(SlowLogLevel.TRACE, "index-2", UUIDs.randomBase64UUID()), Settings.EMPTY);
SearchSlowLog log2 = new SearchSlowLog(settings2);
int numberOfLoggersAfter = context.getLoggers().size();
assertThat(numberOfLoggersAfter, equalTo(numberOfLoggersBefore));
}
use of org.opensearch.search.internal.SearchContext 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.search.internal.SearchContext in project OpenSearch by opensearch-project.
the class SearchSlowLogTests method testTwoLoggersDifferentLevel.
public void testTwoLoggersDifferentLevel() {
SearchContext ctx1 = searchContextWithSourceAndTask(createIndex("index-1"));
SearchContext ctx2 = searchContextWithSourceAndTask(createIndex("index-2"));
IndexSettings settings1 = new IndexSettings(createIndexMetadata(SlowLogLevel.WARN, "index-1", UUIDs.randomBase64UUID()), Settings.EMPTY);
SearchSlowLog log1 = new SearchSlowLog(settings1);
IndexSettings settings2 = new IndexSettings(createIndexMetadata(SlowLogLevel.TRACE, "index-2", UUIDs.randomBase64UUID()), Settings.EMPTY);
SearchSlowLog log2 = new SearchSlowLog(settings2);
{
// level set WARN, should not log
log1.onQueryPhase(ctx1, 11L);
assertNull(appender.getLastEventAndReset());
log1.onFetchPhase(ctx1, 11L);
assertNull(appender.getLastEventAndReset());
// level set TRACE, should log
log2.onQueryPhase(ctx2, 11L);
assertNotNull(appender.getLastEventAndReset());
log2.onFetchPhase(ctx2, 11L);
assertNotNull(appender.getLastEventAndReset());
}
}
use of org.opensearch.search.internal.SearchContext 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;
}
Aggregations