use of org.opensearch.action.search.SearchRequest in project asynchronous-search by opensearch-project.
the class AsynchronousSearchRestIT method testSubmitWithRetainedResponse.
public void testSubmitWithRetainedResponse() throws IOException {
SearchRequest searchRequest = new SearchRequest("test");
searchRequest.source(new SearchSourceBuilder());
SubmitAsynchronousSearchRequest submitAsynchronousSearchRequest = new SubmitAsynchronousSearchRequest(searchRequest);
submitAsynchronousSearchRequest.keepOnCompletion(true);
submitAsynchronousSearchRequest.waitForCompletionTimeout(TimeValue.timeValueMillis(randomLongBetween(1, 500)));
AsynchronousSearchResponse submitResponse = executeSubmitAsynchronousSearch(submitAsynchronousSearchRequest);
List<AsynchronousSearchState> legalStates = Arrays.asList(AsynchronousSearchState.RUNNING, AsynchronousSearchState.SUCCEEDED, AsynchronousSearchState.PERSIST_SUCCEEDED, AsynchronousSearchState.PERSISTING, AsynchronousSearchState.CLOSED, AsynchronousSearchState.STORE_RESIDENT);
assertNotNull(submitResponse.getId());
assertTrue(submitResponse.getState().name(), legalStates.contains(submitResponse.getState()));
GetAsynchronousSearchRequest getAsynchronousSearchRequest = new GetAsynchronousSearchRequest(submitResponse.getId());
AsynchronousSearchResponse getResponse;
do {
getResponse = getAssertedAsynchronousSearchResponse(submitResponse, getAsynchronousSearchRequest);
if (getResponse.getState() == AsynchronousSearchState.RUNNING && getResponse.getSearchResponse() != null) {
assertEquals(getResponse.getSearchResponse().getHits().getHits().length, 0);
} else {
assertNotNull(getResponse.getSearchResponse());
assertNotEquals(getResponse.getSearchResponse().getTook(), -1L);
}
} while (AsynchronousSearchState.STORE_RESIDENT.equals(getResponse.getState()) == false);
getResponse = getAssertedAsynchronousSearchResponse(submitResponse, getAsynchronousSearchRequest);
assertNotNull(getResponse.getSearchResponse());
assertEquals(AsynchronousSearchState.STORE_RESIDENT, getResponse.getState());
assertHitCount(getResponse.getSearchResponse(), 5);
executeDeleteAsynchronousSearch(new DeleteAsynchronousSearchRequest(submitResponse.getId()));
}
use of org.opensearch.action.search.SearchRequest in project OpenSearch by opensearch-project.
the class TransportRankEvalAction method doExecute.
@Override
protected void doExecute(Task task, RankEvalRequest request, ActionListener<RankEvalResponse> listener) {
RankEvalSpec evaluationSpecification = request.getRankEvalSpec();
EvaluationMetric metric = evaluationSpecification.getMetric();
List<RatedRequest> ratedRequests = evaluationSpecification.getRatedRequests();
Map<String, Exception> errors = new ConcurrentHashMap<>(ratedRequests.size());
Map<String, TemplateScript.Factory> scriptsWithoutParams = new HashMap<>();
for (Entry<String, Script> entry : evaluationSpecification.getTemplates().entrySet()) {
scriptsWithoutParams.put(entry.getKey(), scriptService.compile(entry.getValue(), TemplateScript.CONTEXT));
}
MultiSearchRequest msearchRequest = new MultiSearchRequest();
msearchRequest.maxConcurrentSearchRequests(evaluationSpecification.getMaxConcurrentSearches());
List<RatedRequest> ratedRequestsInSearch = new ArrayList<>();
for (RatedRequest ratedRequest : ratedRequests) {
SearchSourceBuilder evaluationRequest = ratedRequest.getEvaluationRequest();
if (evaluationRequest == null) {
Map<String, Object> params = ratedRequest.getParams();
String templateId = ratedRequest.getTemplateId();
TemplateScript.Factory templateScript = scriptsWithoutParams.get(templateId);
String resolvedRequest = templateScript.newInstance(params).execute();
try (XContentParser subParser = createParser(namedXContentRegistry, LoggingDeprecationHandler.INSTANCE, new BytesArray(resolvedRequest), XContentType.JSON)) {
evaluationRequest = SearchSourceBuilder.fromXContent(subParser, false);
// check for parts that should not be part of a ranking evaluation request
validateEvaluatedQuery(evaluationRequest);
} catch (IOException e) {
// if we fail parsing, put the exception into the errors map and continue
errors.put(ratedRequest.getId(), e);
continue;
}
}
if (metric.forcedSearchSize().isPresent()) {
evaluationRequest.size(metric.forcedSearchSize().getAsInt());
}
ratedRequestsInSearch.add(ratedRequest);
List<String> summaryFields = ratedRequest.getSummaryFields();
if (summaryFields.isEmpty()) {
evaluationRequest.fetchSource(false);
} else {
evaluationRequest.fetchSource(summaryFields.toArray(new String[summaryFields.size()]), new String[0]);
}
SearchRequest searchRequest = new SearchRequest(request.indices(), evaluationRequest);
searchRequest.indicesOptions(request.indicesOptions());
searchRequest.searchType(request.searchType());
msearchRequest.add(searchRequest);
}
assert ratedRequestsInSearch.size() == msearchRequest.requests().size();
client.multiSearch(msearchRequest, new RankEvalActionListener(listener, metric, ratedRequestsInSearch.toArray(new RatedRequest[ratedRequestsInSearch.size()]), errors));
}
use of org.opensearch.action.search.SearchRequest in project OpenSearch by opensearch-project.
the class CCSDuelIT method testMatchAll.
public void testMatchAll() throws Exception {
assumeMultiClusterSetup();
// verify that the order in which documents are returned when they all have the same score is the same
SearchRequest searchRequest = initSearchRequest();
duelSearch(searchRequest, CCSDuelIT::assertHits);
}
use of org.opensearch.action.search.SearchRequest in project OpenSearch by opensearch-project.
the class CCSDuelIT method testHighlighting.
public void testHighlighting() throws Exception {
assumeMultiClusterSetup();
SearchRequest searchRequest = initSearchRequest();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.highlighter(new HighlightBuilder().field("tags"));
sourceBuilder.query(QueryBuilders.matchQuery("tags", "xml"));
searchRequest.source(sourceBuilder);
duelSearch(searchRequest, response -> {
assertHits(response);
assertFalse(response.getHits().getHits()[0].getHighlightFields().isEmpty());
});
}
use of org.opensearch.action.search.SearchRequest in project OpenSearch by opensearch-project.
the class CCSDuelIT method testCardinalityAgg.
public void testCardinalityAgg() throws Exception {
assumeMultiClusterSetup();
SearchRequest searchRequest = initSearchRequest();
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.size(0);
searchRequest.source(sourceBuilder);
CardinalityAggregationBuilder tags = new CardinalityAggregationBuilder("tags").userValueTypeHint(ValueType.STRING);
tags.field("tags.keyword");
sourceBuilder.aggregation(tags);
duelSearch(searchRequest, CCSDuelIT::assertAggs);
}
Aggregations