Search in sources :

Example 1 with SearchContext

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);
}
Also used : ParsedQuery(org.opensearch.index.query.ParsedQuery) ParsingException(org.opensearch.common.ParsingException) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchContext(org.opensearch.search.internal.SearchContext) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 2 with SearchContext

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));
}
Also used : SearchContext(org.opensearch.search.internal.SearchContext) TestSearchContext(org.opensearch.test.TestSearchContext) LoggerContext(org.apache.logging.log4j.core.LoggerContext)

Example 3 with SearchContext

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\\\"]"));
}
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 4 with SearchContext

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());
    }
}
Also used : SearchContext(org.opensearch.search.internal.SearchContext) TestSearchContext(org.opensearch.test.TestSearchContext)

Example 5 with SearchContext

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;
}
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)

Aggregations

SearchContext (org.opensearch.search.internal.SearchContext)30 ShardSearchRequest (org.opensearch.search.internal.ShardSearchRequest)10 TestSearchContext (org.opensearch.test.TestSearchContext)9 IOException (java.io.IOException)8 ReaderContext (org.opensearch.search.internal.ReaderContext)8 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)7 OpenSearchException (org.opensearch.OpenSearchException)6 IndexShard (org.opensearch.index.shard.IndexShard)6 ExecutionException (java.util.concurrent.ExecutionException)5 Releasable (org.opensearch.common.lease.Releasable)5 OpenSearchRejectedExecutionException (org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException)5 IndexNotFoundException (org.opensearch.index.IndexNotFoundException)5 AggregationInitializationException (org.opensearch.search.aggregations.AggregationInitializationException)5 LegacyReaderContext (org.opensearch.search.internal.LegacyReaderContext)5 HashMap (java.util.HashMap)4 SearchRequest (org.opensearch.action.search.SearchRequest)4 IndexService (org.opensearch.index.IndexService)4 IndicesService (org.opensearch.indices.IndicesService)4 AliasFilter (org.opensearch.search.internal.AliasFilter)4 Map (java.util.Map)3