use of org.elasticsearch.search.internal.SearchContext in project elasticsearch by elastic.
the class SearchService method executeFetchPhase.
public ScrollQueryFetchSearchResult executeFetchPhase(InternalScrollSearchRequest request, SearchTask task) {
final SearchContext context = findContext(request.id());
context.incRef();
try {
context.setTask(task);
contextProcessing(context);
SearchOperationListener operationListener = context.indexShard().getSearchOperationListener();
processScroll(request, context);
operationListener.onPreQueryPhase(context);
final long time = System.nanoTime();
try {
queryPhase.execute(context);
} catch (Exception e) {
operationListener.onFailedQueryPhase(context);
throw ExceptionsHelper.convertToRuntime(e);
}
long afterQueryTime = System.nanoTime();
operationListener.onQueryPhase(context, afterQueryTime - time);
QueryFetchSearchResult fetchSearchResult = executeFetchPhase(context, operationListener, afterQueryTime);
return new ScrollQueryFetchSearchResult(fetchSearchResult, context.shardTarget());
} catch (Exception e) {
logger.trace("Fetch phase failed", e);
processFailure(context, e);
throw ExceptionsHelper.convertToRuntime(e);
} finally {
cleanContext(context);
}
}
use of org.elasticsearch.search.internal.SearchContext in project elasticsearch by elastic.
the class TransportValidateQueryAction method shardOperation.
@Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request) throws IOException {
boolean valid;
String explanation = null;
String error = null;
ShardSearchLocalRequest shardSearchLocalRequest = new ShardSearchLocalRequest(request.shardId(), request.types(), request.nowInMillis(), request.filteringAliases());
SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT, null);
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 | IOException e) {
valid = false;
error = e.getMessage();
} finally {
Releasables.close(searchContext);
}
return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
}
use of org.elasticsearch.search.internal.SearchContext 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());
}
use of org.elasticsearch.search.internal.SearchContext in project elasticsearch by elastic.
the class ExtendedBoundsTests method testParseAndValidate.
public void testParseAndValidate() {
long now = randomLong();
Settings indexSettings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT).put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1).build();
SearchContext context = mock(SearchContext.class);
QueryShardContext qsc = new QueryShardContext(0, new IndexSettings(IndexMetaData.builder("foo").settings(indexSettings).build(), indexSettings), null, null, null, null, null, xContentRegistry(), null, null, () -> now);
when(context.getQueryShardContext()).thenReturn(qsc);
FormatDateTimeFormatter formatter = Joda.forPattern("dateOptionalTime");
DocValueFormat format = new DocValueFormat.DateTime(formatter, DateTimeZone.UTC);
ExtendedBounds expected = randomParsedExtendedBounds();
ExtendedBounds parsed = unparsed(expected).parseAndValidate("test", context, format);
// parsed won't *equal* expected because equal includes the String parts
assertEquals(expected.getMin(), parsed.getMin());
assertEquals(expected.getMax(), parsed.getMax());
parsed = new ExtendedBounds("now", null).parseAndValidate("test", context, format);
assertEquals(now, (long) parsed.getMin());
assertNull(parsed.getMax());
parsed = new ExtendedBounds(null, "now").parseAndValidate("test", context, format);
assertNull(parsed.getMin());
assertEquals(now, (long) parsed.getMax());
SearchParseException e = expectThrows(SearchParseException.class, () -> new ExtendedBounds(100L, 90L).parseAndValidate("test", context, format));
assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
e = expectThrows(SearchParseException.class, () -> unparsed(new ExtendedBounds(100L, 90L)).parseAndValidate("test", context, format));
assertEquals("[extended_bounds.min][100] cannot be greater than [extended_bounds.max][90] for histogram aggregation [test]", e.getMessage());
}
use of org.elasticsearch.search.internal.SearchContext in project elasticsearch by elastic.
the class PercolatorHighlightSubFetchPhaseTests method testHitsExecutionNeeded.
public void testHitsExecutionNeeded() {
PercolateQuery percolateQuery = new PercolateQuery("", ctx -> null, new BytesArray("{}"), new MatchAllDocsQuery(), Mockito.mock(IndexSearcher.class), new MatchAllDocsQuery());
PercolatorHighlightSubFetchPhase subFetchPhase = new PercolatorHighlightSubFetchPhase(Settings.EMPTY, emptyMap());
SearchContext searchContext = Mockito.mock(SearchContext.class);
Mockito.when(searchContext.highlight()).thenReturn(new SearchContextHighlight(Collections.emptyList()));
Mockito.when(searchContext.query()).thenReturn(new MatchAllDocsQuery());
assertThat(subFetchPhase.hitsExecutionNeeded(searchContext), is(false));
Mockito.when(searchContext.query()).thenReturn(percolateQuery);
assertThat(subFetchPhase.hitsExecutionNeeded(searchContext), is(true));
}
Aggregations