Java Code Examples for org.elasticsearch.action.search.MultiSearchRequest#add()

The following examples show how to use org.elasticsearch.action.search.MultiSearchRequest#add() . 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 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 2
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 3
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 4
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 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: 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 7
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 8
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());
}