Search in sources :

Example 91 with SearchRequest

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()));
}
Also used : AsynchronousSearchResponse(org.opensearch.search.asynchronous.response.AsynchronousSearchResponse) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) SubmitAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest) GetAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.GetAsynchronousSearchRequest) AsynchronousSearchState(org.opensearch.search.asynchronous.context.state.AsynchronousSearchState) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) DeleteAsynchronousSearchRequest(org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)

Example 92 with SearchRequest

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));
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TemplateScript(org.opensearch.script.TemplateScript) Script(org.opensearch.script.Script) BytesArray(org.opensearch.common.bytes.BytesArray) IOException(java.io.IOException) IOException(java.io.IOException) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) TemplateScript(org.opensearch.script.TemplateScript) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 93 with SearchRequest

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);
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest)

Example 94 with SearchRequest

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());
    });
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) HighlightBuilder(org.opensearch.search.fetch.subphase.highlight.HighlightBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Example 95 with SearchRequest

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);
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) CardinalityAggregationBuilder(org.opensearch.search.aggregations.metrics.CardinalityAggregationBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

SearchRequest (org.opensearch.action.search.SearchRequest)409 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)213 SearchResponse (org.opensearch.action.search.SearchResponse)151 SubmitAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.SubmitAsynchronousSearchRequest)91 ActionListener (org.opensearch.action.ActionListener)77 IOException (java.io.IOException)76 List (java.util.List)68 ClusterService (org.opensearch.cluster.service.ClusterService)67 ArrayList (java.util.ArrayList)64 TimeValue (org.opensearch.common.unit.TimeValue)62 AsynchronousSearchResponse (org.opensearch.search.asynchronous.response.AsynchronousSearchResponse)62 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)61 Map (java.util.Map)55 CountDownLatch (java.util.concurrent.CountDownLatch)55 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)52 HashMap (java.util.HashMap)51 Client (org.opensearch.client.Client)48 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)48 Settings (org.opensearch.common.settings.Settings)46 DeleteAsynchronousSearchRequest (org.opensearch.search.asynchronous.request.DeleteAsynchronousSearchRequest)46