Search in sources :

Example 6 with SearchRequest

use of org.opensearch.action.search.SearchRequest in project veilarbportefolje by navikt.

the class OpensearchService method search.

@SneakyThrows
private <T> T search(SearchSourceBuilder searchSourceBuilder, String indexAlias, Class<T> clazz) {
    SearchRequest request = new SearchRequest().indices(indexAlias).source(searchSourceBuilder);
    SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
    return JsonUtils.fromJson(response.toString(), clazz);
}
Also used : SearchRequest(org.opensearch.action.search.SearchRequest) SearchResponse(org.opensearch.action.search.SearchResponse) SneakyThrows(lombok.SneakyThrows)

Example 7 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 8 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 9 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 10 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)407 SearchSourceBuilder (org.opensearch.search.builder.SearchSourceBuilder)213 SearchResponse (org.opensearch.action.search.SearchResponse)149 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