org.elasticsearch.action.search.MultiSearchRequest Java Examples

The following examples show how to use org.elasticsearch.action.search.MultiSearchRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractEs6_4ClientInstrumentationTest.java    From apm-agent-java with Apache License 2.0 6 votes vote down vote up
protected MultiSearchResponse doMultiSearch(MultiSearchRequest searchRequest) throws IOException, ExecutionException, InterruptedException {
    if (async) {
        ClientMethod<MultiSearchRequest, MultiSearchResponse> method =
            (request, options, listener) -> client.msearchAsync(request, options, listener);
        return invokeAsync(searchRequest, method);
    }
    return client.msearch(searchRequest, RequestOptions.DEFAULT);
}
 
Example #2
Source File: AbstractEs6_4ClientInstrumentationTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiSearchRequest_validateSpanContentAndDbContext() throws InterruptedException, ExecutionException, IOException {
    createDocument();
    reporter.reset();

    MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
    SearchRequest firstSearchRequest = new SearchRequest(INDEX);
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.query(QueryBuilders.matchQuery(FOO, BAR));
    firstSearchRequest.source(searchSourceBuilder);
    multiSearchRequest.add(firstSearchRequest);

    MultiSearchResponse response = doMultiSearch(multiSearchRequest);

    List<Span> spans = reporter.getSpans();
    assertThat(spans).hasSize(1);
    Span span = spans.get(0);
    validateSpanContent(span, "Elasticsearch: POST /_msearch", 200, "POST");
    verifyMultiSearchSpanContent(span);

    deleteDocument();
}
 
Example #3
Source File: RestMultiSearchAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
    MultiSearchRequest multiSearchRequest = new MultiSearchRequest();

    String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
    String[] types = Strings.splitStringByCommaToArray(request.param("type"));
    String path = request.path();
    boolean isTemplateRequest = isTemplateRequest(path);
    IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions());
    multiSearchRequest.add(RestActions.getRestContent(request), isTemplateRequest, indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex);

    client.multiSearch(multiSearchRequest, new RestToXContentListener<MultiSearchResponse>(channel));
}
 
Example #4
Source File: RestCoordinateMultiSearchAction.java    From siren-join with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
  MultiSearchRequest multiSearchRequest = new MultiSearchRequest();

  String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
  String[] types = Strings.splitStringByCommaToArray(request.param("type"));
  String path = request.path();
  boolean isTemplateRequest = isTemplateRequest(path);
  IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions());
  multiSearchRequest.add(RestActions.getRestContent(request), isTemplateRequest, indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex);

  client.execute(CoordinateMultiSearchAction.INSTANCE, multiSearchRequest, new RestToXContentListener<MultiSearchResponse>(channel));
}
 
Example #5
Source File: InternalEsClient.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 非同期でドキュメントをマルチ検索.
 * 存在しないインデックスに対して本メソッドを使用すると、TransportSerializationExceptionがスローされるので注意すること
 * @param index インデックス名
 * @param type タイプ名
 * @param routingId routingId
 * @param queryList マルチ検索用のクエリ情報リスト
 * @return 非同期応答
 */
public ActionFuture<MultiSearchResponse> asyncMultiSearch(
        String index,
        String type,
        String routingId,
        List<Map<String, Object>> queryList) {
    MultiSearchRequest mrequest = new MultiSearchRequest();
    if (queryList == null || queryList.size() == 0) {
        throw new EsMultiSearchQueryParseException();
    }
    for (Map<String, Object> query : queryList) {
        SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);
        if (type != null) {
            req.types(type);
        }
        // クエリ指定なしの場合はタイプに対する全件検索を行う
        if (query != null) {
            req.source(query);
        }
        if (routingFlag) {
            req = req.routing(routingId);
        }
        mrequest.add(req);
    }

    ActionFuture<MultiSearchResponse> ret = esTransportClient.multiSearch(mrequest);
    this.fireEvent(Event.afterRequest, index, type, null, JSONArray.toJSONString(queryList), "MultiSearch");
    return ret;
}
 
Example #6
Source File: InternalEsClient.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * 非同期でドキュメントをマルチ検索.
 * 存在しないインデックスに対して本メソッドを使用すると、TransportSerializationExceptionがスローされるので注意すること
 * @param index インデックス名
 * @param type タイプ名
 * @param routingId routingId
 * @param queryList マルチ検索用のクエリ情報リスト
 * @return 非同期応答
 */
public ActionFuture<MultiSearchResponse> asyncMultiSearch(
        String index,
        String type,
        String routingId,
        List<Map<String, Object>> queryList) {
    MultiSearchRequest mrequest = new MultiSearchRequest();
    if (queryList == null || queryList.size() == 0) {
        throw new EsMultiSearchQueryParseException();
    }
    for (Map<String, Object> query : queryList) {
        SearchRequest req = new SearchRequest(index).searchType(SearchType.DEFAULT);
        if (type != null) {
            req.types(type);
        }
        // クエリ指定なしの場合はタイプに対する全件検索を行う
        if (query != null) {
            req.source(query);
        }
        if (routingFlag) {
            req = req.routing(routingId);
        }
        mrequest.add(req);
    }

    ActionFuture<MultiSearchResponse> ret = esTransportClient.multiSearch(mrequest);
    this.fireEvent(Event.afterRequest, index, type, null, JSONArray.toJSONString(queryList), "MultiSearch");
    return ret;
}
 
Example #7
Source File: NestedLoopsElasticExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private int combineResultsFromMultiResponses(List<SearchHit> combinedResults, int totalLimit, int currentCombinedResults, SearchHit[] hits, int currentIndex, MultiSearchRequest multiSearchRequest) {
    MultiSearchResponse.Item[] responses = client.multiSearch(multiSearchRequest).actionGet().getResponses();
    String t1Alias = nestedLoopsRequest.getFirstTable().getAlias();
    String t2Alias = nestedLoopsRequest.getSecondTable().getAlias();

    for(int j =0 ; j < responses.length && currentCombinedResults < totalLimit ; j++){
        SearchHit hitFromFirstTable = hits[currentIndex+j];
        onlyReturnedFields(hitFromFirstTable.getSourceAsMap(), nestedLoopsRequest.getFirstTable().getReturnedFields(),nestedLoopsRequest.getFirstTable().getOriginalSelect().isSelectAll());

        SearchResponse multiItemResponse = responses[j].getResponse();
        updateMetaSearchResults(multiItemResponse);

        //todo: if responseForHit.getHits.length < responseForHit.getTotalHits(). need to fetch more!
        SearchHits responseForHit = multiItemResponse.getHits();

        if(responseForHit.getHits().length == 0 && nestedLoopsRequest.getJoinType() == SQLJoinTableSource.JoinType.LEFT_OUTER_JOIN){
            SearchHit unmachedResult = createUnmachedResult(nestedLoopsRequest.getSecondTable().getReturnedFields(), currentCombinedResults, t1Alias, t2Alias, hitFromFirstTable);
            combinedResults.add(unmachedResult);
            currentCombinedResults++;
            continue;
        }

        for(SearchHit matchedHit : responseForHit.getHits() ){
            SearchHit searchHit = getMergedHit(currentCombinedResults, t1Alias, t2Alias, hitFromFirstTable, matchedHit);
            combinedResults.add(searchHit);
            currentCombinedResults++;
            if(currentCombinedResults >= totalLimit) break;
        }
        if(currentCombinedResults >= totalLimit) break;

    }
    return currentCombinedResults;
}
 
Example #8
Source File: NestedLoopsElasticExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private MultiSearchRequest createMultiSearchRequest(int multiSearchMaxSize, Where connectedWhere, SearchHit[] hits, Select secondTableSelect, Where originalWhere, int currentIndex) throws SqlParseException {
        MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
        for(int i = currentIndex  ; i < currentIndex  + multiSearchMaxSize && i< hits.length ; i++ ){
            Map<String, Object> hitFromFirstTableAsMap = hits[i].getSourceAsMap();
            Where newWhere = Where.newInstance();
            if(originalWhere!=null) newWhere.addWhere(originalWhere);
            if(connectedWhere!=null){
                Where connectedWhereCloned = null;
                try {
                    connectedWhereCloned = (Where) connectedWhere.clone();
                } catch (CloneNotSupportedException e) {
                    e.printStackTrace();
                }
                updateValuesOnWhereConditions(hitFromFirstTableAsMap,connectedWhereCloned);
                newWhere.addWhere(connectedWhereCloned);
            }


//            for(Condition c : conditions){
//                Object value = deepSearchInMap(hitFromFirstTableAsMap,c.getValue().toString());
//                Condition conditionWithValue = new Condition(Where.CONN.AND,c.getName(),c.getOpear(),value);
//                newWhere.addWhere(conditionWithValue);
//            }
            //using the 2nd table select and DefaultAction because we can't just change query on request (need to create lot of requests)
            if(newWhere.getWheres().size() != 0) {
                secondTableSelect.setWhere(newWhere);
            }
            DefaultQueryAction action = new DefaultQueryAction(this.client , secondTableSelect);
            action.explain();
            SearchRequestBuilder secondTableRequest = action.getRequestBuilder();
            Integer secondTableHintLimit = this.nestedLoopsRequest.getSecondTable().getHintLimit();
            if(secondTableHintLimit != null && secondTableHintLimit <= MAX_RESULTS_ON_ONE_FETCH)
                secondTableRequest.setSize(secondTableHintLimit);
            multiSearchRequest.add(secondTableRequest);
        }
        return multiSearchRequest;
    }
 
Example #9
Source File: MultiSearchRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected XContentBuilder toXContent(MultiSearchRequest request, MultiSearchResponse response, XContentBuilder builder) throws IOException {
    return super.toXContent(request, response, builder.startObject()).endObject();
}
 
Example #10
Source File: SearchFeatureDaoTests.java    From anomaly-detection with Apache License 2.0 4 votes vote down vote up
@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    PowerMockito.mockStatic(ParseUtils.class);

    Interpolator interpolator = new LinearUniformInterpolator(new SingleFeatureLinearUniformInterpolator());
    searchFeatureDao = spy(new SearchFeatureDao(client, xContent, interpolator, clientUtil));

    detectionInterval = new IntervalTimeConfiguration(1, ChronoUnit.MINUTES);
    when(detector.getTimeField()).thenReturn("testTimeField");
    when(detector.getIndices()).thenReturn(Arrays.asList("testIndices"));
    when(detector.generateFeatureQuery()).thenReturn(featureQuery);
    when(detector.getDetectionInterval()).thenReturn(detectionInterval);

    searchSourceBuilder = SearchSourceBuilder
        .fromXContent(XContentType.JSON.xContent().createParser(xContent, LoggingDeprecationHandler.INSTANCE, "{}"));
    searchRequestParams = new HashMap<>();
    searchRequest = new SearchRequest(detector.getIndices().toArray(new String[0]));
    aggsMap = new HashMap<>();
    aggsList = new ArrayList<>();

    when(max.getName()).thenReturn(SearchFeatureDao.AGG_NAME_MAX);
    List<Aggregation> list = new ArrayList<>();
    list.add(max);
    Aggregations aggregations = new Aggregations(list);
    SearchHits hits = new SearchHits(new SearchHit[0], new TotalHits(1L, TotalHits.Relation.EQUAL_TO), 1f);
    when(searchResponse.getHits()).thenReturn(hits);

    doReturn(Optional.of(searchResponse))
        .when(clientUtil)
        .timedRequest(eq(searchRequest), anyObject(), Matchers.<BiConsumer<SearchRequest, ActionListener<SearchResponse>>>anyObject());
    when(searchResponse.getAggregations()).thenReturn(aggregations);

    doReturn(Optional.of(searchResponse))
        .when(clientUtil)
        .throttledTimedRequest(
            eq(searchRequest),
            anyObject(),
            Matchers.<BiConsumer<SearchRequest, ActionListener<SearchResponse>>>anyObject(),
            anyObject()
        );

    multiSearchRequest = new MultiSearchRequest();
    SearchRequest request = new SearchRequest(detector.getIndices().toArray(new String[0]));
    multiSearchRequest.add(request);
    doReturn(Optional.of(multiSearchResponse))
        .when(clientUtil)
        .timedRequest(
            eq(multiSearchRequest),
            anyObject(),
            Matchers.<BiConsumer<MultiSearchRequest, ActionListener<MultiSearchResponse>>>anyObject()
        );
    when(multiSearchResponse.getResponses()).thenReturn(new Item[] { multiSearchResponseItem });
    when(multiSearchResponseItem.getResponse()).thenReturn(searchResponse);
}
 
Example #11
Source File: MultiSearchRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected ActionFuture<MultiSearchResponse> doExecute(MultiSearchRequest request) {
    return client.multiSearch(request);
}
 
Example #12
Source File: MultiSearchRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
public MultiSearchRequestBuilder(Client client, JsonToString<JsonInput> jsonToString, StringToJson<JsonOutput> stringToJson) {
    super(client, new MultiSearchRequest(), jsonToString, stringToJson);
}
 
Example #13
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public void multiSearch(final MultiSearchRequest request, final ActionListener<MultiSearchResponse> listener) {
    client.multiSearch(request, listener);
}
 
Example #14
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<MultiSearchResponse> multiSearch(final MultiSearchRequest request) {
    return client.multiSearch(request);
}
 
Example #15
Source File: JoinRequestBuilder.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public void setMulti(MultiSearchRequest multi) {
    this.multi = multi;
}
 
Example #16
Source File: JoinRequestBuilder.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
public MultiSearchRequest getMulti() {
    return multi;
}
 
Example #17
Source File: JoinRequestBuilder.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private void buildMulti() {
    multi = new MultiSearchRequest();
    multi.add(firstTable.getRequestBuilder());
    multi.add(secondTable.getRequestBuilder());
}
 
Example #18
Source File: NestedLoopsElasticExecutor.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
@Override
protected List<SearchHit> innerRun() throws SqlParseException {
    List<SearchHit> combinedResults = new ArrayList<>();
    int totalLimit = nestedLoopsRequest.getTotalLimit();
    int multiSearchMaxSize = nestedLoopsRequest.getMultiSearchMaxSize();
    Select secondTableSelect = nestedLoopsRequest.getSecondTable().getOriginalSelect();
    Where originalSecondTableWhere = secondTableSelect.getWhere();

    orderConditions(nestedLoopsRequest.getFirstTable().getAlias(),nestedLoopsRequest.getSecondTable().getAlias());


    FetchWithScrollResponse fetchWithScrollResponse = firstFetch(this.nestedLoopsRequest.getFirstTable());
    SearchResponse firstTableResponse = fetchWithScrollResponse.getResponse();
    boolean needScrollForFirstTable = fetchWithScrollResponse.isNeedScrollForFirstTable();

    int currentCombinedResults = 0;
    boolean finishedWithFirstTable = false;

    while (totalLimit > currentCombinedResults && !finishedWithFirstTable){

        SearchHit[] hits = firstTableResponse.getHits().getHits();
        boolean finishedMultiSearches = hits.length == 0;
        int currentHitsIndex = 0 ;

        while(!finishedMultiSearches){
            MultiSearchRequest multiSearchRequest = createMultiSearchRequest(multiSearchMaxSize, nestedLoopsRequest.getConnectedWhere(), hits, secondTableSelect, originalSecondTableWhere, currentHitsIndex);
            int multiSearchSize = multiSearchRequest.requests().size();
            currentCombinedResults = combineResultsFromMultiResponses(combinedResults, totalLimit, currentCombinedResults, hits, currentHitsIndex, multiSearchRequest);
            currentHitsIndex += multiSearchSize;
            finishedMultiSearches = currentHitsIndex >= hits.length-1 || currentCombinedResults >= totalLimit;
        }

        if( hits.length < MAX_RESULTS_ON_ONE_FETCH ) needScrollForFirstTable = false;

        if(!finishedWithFirstTable)
        {
            if(needScrollForFirstTable)
                firstTableResponse = client.prepareSearchScroll(firstTableResponse.getScrollId()).setScroll(new TimeValue(600000)).get();
            else finishedWithFirstTable = true;
        }

    }
    return combinedResults;
}
 
Example #19
Source File: ReactorElasticSearchClient.java    From james-project with Apache License 2.0 4 votes vote down vote up
public Mono<MultiSearchResponse> msearch(MultiSearchRequest multiSearchRequest, RequestOptions options) {
    return toReactor(listener -> client.msearchAsync(multiSearchRequest, options, listener));
}
 
Example #20
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void multiSearch(MultiSearchRequest request, ActionListener<MultiSearchResponse> listener) {
	wrapped.multiSearch(request, listener);
}
 
Example #21
Source File: ClientWithStats.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public ActionFuture<MultiSearchResponse> multiSearch(MultiSearchRequest request) {
	return wrapped.multiSearch(request);
}