public void testSimpleMultiSearchMoreRequests() { createIndex("test"); int numDocs = randomIntBetween(0, 16); for (int i = 0; i < numDocs; i++) { client().prepareIndex("test", "type", Integer.toString(i)).setSource("{}", XContentType.JSON).get(); } refresh(); int numSearchRequests = randomIntBetween(1, 64); MultiSearchRequest request = new MultiSearchRequest(); if (randomBoolean()) { request.maxConcurrentSearchRequests(randomIntBetween(1, numSearchRequests)); } for (int i = 0; i < numSearchRequests; i++) { request.add(client().prepareSearch("test")); } MultiSearchResponse response = client().multiSearch(request).actionGet(); assertThat(response.getResponses().length, equalTo(numSearchRequests)); for (MultiSearchResponse.Item item : response) { assertNoFailures(item.getResponse()); assertHitCount(item.getResponse(), numDocs); } }
public void testSimpleMultiSearch() { createIndex("test"); ensureGreen(); client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet(); client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet(); refresh(); MultiSearchResponse response = client().prepareMultiSearch() .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("field", "xxx"))) .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("field", "yyy"))) .add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())) .execute().actionGet(); for (MultiSearchResponse.Item item : response) { assertNoFailures(item.getResponse()); } assertThat(response.getResponses().length, equalTo(3)); assertHitCount(response.getResponses()[0].getResponse(), 1L); assertHitCount(response.getResponses()[1].getResponse(), 1L); assertHitCount(response.getResponses()[2].getResponse(), 2L); assertFirstHit(response.getResponses()[0].getResponse(), hasId("1")); assertFirstHit(response.getResponses()[1].getResponse(), hasId("2")); }
public void testFailedMultiSearchWithWrongQuery() throws Exception { prepareData(); logger.info("Start Testing failed multi search with a wrong query"); MultiSearchResponse response = client().prepareMultiSearch() .add(client().prepareSearch("test").setQuery(new MatchQueryBuilder("foo", "biz"))) .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2))) .add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())) .execute().actionGet(); assertThat(response.getResponses().length, equalTo(3)); assertThat(response.getResponses()[0].getFailureMessage(), notNullValue()); assertThat(response.getResponses()[1].getFailureMessage(), nullValue()); assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1)); assertThat(response.getResponses()[2].getFailureMessage(), nullValue()); assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10)); logger.info("Done Testing failed search"); }
public void testFailedMultiSearchWithWrongQueryWithFunctionScore() throws Exception { prepareData(); logger.info("Start Testing failed multi search with a wrong query"); MultiSearchResponse response = client().prepareMultiSearch() // Add custom score query with bogus script .add(client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("nid", 1), new ScriptScoreFunctionBuilder(new Script(ScriptType.INLINE, "bar", "foo", Collections.emptyMap()))))) .add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2))) .add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery())) .execute().actionGet(); assertThat(response.getResponses().length, equalTo(3)); assertThat(response.getResponses()[0].getFailureMessage(), notNullValue()); assertThat(response.getResponses()[1].getFailureMessage(), nullValue()); assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1)); assertThat(response.getResponses()[2].getFailureMessage(), nullValue()); assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10)); logger.info("Done Testing failed search"); }
private ListStoresActionResponse toResponse(MultiSearchResponse response, List<Tuple<String, Integer>> versions) { assert versions.size() == response.getResponses().length; Iterator<Tuple<String, Integer>> vs = versions.iterator(); Iterator<MultiSearchResponse.Item> rs = response.iterator(); List<ListStoresAction.IndexStoreInfo> infos = new ArrayList<>(versions.size()); while (vs.hasNext() && rs.hasNext()) { MultiSearchResponse.Item it = rs.next(); Tuple<String, Integer> idxAndVersion = vs.next(); Map<String, Integer> counts = Collections.emptyMap(); if (!it.isFailure()) { Terms aggs = it.getResponse() .getAggregations() .get("type"); counts = aggs .getBuckets() .stream() .collect(toMap(MultiBucketsAggregation.Bucket::getKeyAsString, (b) -> (int) b.getDocCount())); } infos.add(new ListStoresAction.IndexStoreInfo(idxAndVersion.v1(), idxAndVersion.v2(), counts)); } return new ListStoresActionResponse(infos); }
/** * Execute query request with filter and aggregations. * * @param queries * list of queries to execute * @return ES search response * @throws ODataApplicationException * if any error appeared during executing request */ public MultiSearchResponse executeRequest(List<SearchQuery> queries) throws ODataApplicationException { MultiSearchRequestBuilder multiSearchRequestBuilder = client.prepareMultiSearch(); for (SearchQuery query : queries) { Pagination pagination = query.getPagination(); SearchRequestBuilder requestBuilder = client.prepareSearch(query.getIndex()) .setTypes(query.getTypes()).setQuery(query.getQueryBuilder()); if (pagination != null) { List<Sort> orderBy = pagination.getOrderBy(); for (Sort sort : orderBy) { FieldSortBuilder sortQuery = SortBuilders.fieldSort(sort.getProperty()) .order(SortOrder.valueOf(sort.getDirection().toString())); requestBuilder.addSort(sortQuery); } requestBuilder.setSize(pagination.getTop()).setFrom(pagination.getSkip()); } Set<String> fields = query.getFields(); if (fields != null && !fields.isEmpty()) { requestBuilder.setFetchSource(fields.toArray(new String[fields.size()]), null); } multiSearchRequestBuilder.add(requestBuilder); } return executeRequest(multiSearchRequestBuilder); }
@Test public void testMultiSearch() throws Exception { //first, INDEX two values Map<String, Object> headers = new HashMap<String, Object>(); node.client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet(); node.client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet(); //now, verify MULTISEARCH headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_MULTISEARCH); SearchRequestBuilder srb1 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "xxx")); SearchRequestBuilder srb2 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "yyy")); SearchRequestBuilder srb3 = node.client().prepareSearch("instagram") .setTypes("type").setQuery(QueryBuilders.termQuery("test-multisearchkey", "test-multisearchvalue")); List<SearchRequest> list = new ArrayList<>(); list.add(srb1.request()); list.add(srb2.request()); list.add(srb3.request()); MultiSearchResponse response = template.requestBodyAndHeaders("direct:multisearch", list, headers, MultiSearchResponse.class); MultiSearchResponse.Item[] responses = response.getResponses(); assertNotNull("response should not be null", response); assertEquals("response should contains three multiSearchResponse object", 3, response.getResponses().length); assertFalse("response 1 should be ok", responses[0].isFailure()); assertFalse("response 2 should be ok", responses[1].isFailure()); assertTrue("response 3 should be failed", responses[2].isFailure()); }
@Test public void testMultiSearch() { // query string type SearchRequestBuilder srb1 = client.prepareSearch(indexShakeSpeare).setQuery(QueryBuilders.queryString("elephant")) // size: 10 is the default .setSize(10); // match query type SearchRequestBuilder srb2 = client.prepareSearch(indexShakeSpeare) .setQuery(QueryBuilders.matchQuery(fieldShakeSpeare, "havoc")).setSize(10); // correlate the srbs MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet(); // get all individual responses from MultiSearchResponse long totalHits = 0; for (MultiSearchResponse.Item item : sr.getResponses()) { SearchResponse response = item.getResponse(); totalHits += response.getHits().getTotalHits(); } System.out.println("Total matching for the elephant and havoc: " + totalHits); }
private void testMultiSearch() { SearchRequestBuilder srb1 = client.prepareSearch() // .setQuery(QueryBuilders.queryString("elasticsearch")) // .setSize(1); SearchRequestBuilder srb2 = client.prepareSearch() // .setQuery(QueryBuilders.matchQuery("name", "kimchy")) // .setSize(1); MultiSearchResponse sr = client.prepareMultiSearch() // .add(srb1) // .add(srb2) // .execute() // .actionGet(); // You will get all individual responses from // MultiSearchResponse#responses() long nbHits = 0; for (MultiSearchResponse.Item item : sr.getResponses()) { SearchResponse response = item.getResponse(); nbHits += response.getHits().totalHits(); } }
@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)); }
public static void main(String[] args) throws UnknownHostException { // InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 9300); InetAddress byName = InetAddress.getByName("127.0.0.1"); InetSocketTransportAddress address = new InetSocketTransportAddress(byName, 9300); TransportClient client = new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(address); System.out.println(client); // client.prepareGet("", "", "") MultiSearchResponse response = client.prepareMultiSearch() .add(client.prepareSearch("zsxtest")).execute().actionGet(); System.out.println(response); }
@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)); }
@Test public void testMultiSearch() { SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1); SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1); MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet(); // You will get all individual responses from // MultiSearchResponse#getResponses() long nbHits = 0; for (MultiSearchResponse.Item item : sr.getResponses()) { SearchResponse response = item.getResponse(); nbHits += response.getHits().getTotalHits(); } }
/** * * @param queryString */ private static void multiSearch(String queryString){ Client client = createTransportClient(); SearchRequestBuilder srb1 = client.prepareSearch() .setQuery(QueryBuilders.queryStringQuery(queryString)); SearchRequestBuilder srb2 = client.prepareSearch() .setQuery(QueryBuilders.matchQuery("desc", queryString)); MultiSearchResponse sr = client.prepareMultiSearch() .add(srb1) .add(srb2) .execute().actionGet(); long nbHits = 0; for (MultiSearchResponse.Item item : sr.getResponses()) { SearchResponse response = item.getResponse(); nbHits += response.getHits().getTotalHits(); System.out.println("本次查询共匹配到:"+nbHits+"记录"); SearchHits searchHits = response.getHits(); System.out.println("-----------------搜索关键字为:["+queryString+"]---------------------"); System.out.println("共匹配到:"+searchHits.getTotalHits()+"条记录!"); SearchHit[] hits = searchHits.getHits(); for (SearchHit searchHit : hits) { Map<String, Object> sourceAsMap = searchHit.sourceAsMap(); Set<String> keySet = sourceAsMap.keySet(); for (String string : keySet) { System.out.println(string+":"+sourceAsMap.get(string)); } System.out.println(); } } }
/** * Create response based on elastic multi search response (does not add UUID for now) * * @param response * @param responseUuid * @return */ public StreamingOutput createResponse(final MultiSearchResponse response, final String responseUuid) { return new StreamingOutput() { @Override public void write(OutputStream output) throws IOException, WebApplicationException { XContentBuilder builder = XContentFactory.jsonBuilder(output); builder.startObject(); if (responseUuid != null) builder.field("uuid", responseUuid); response.toXContent(builder, ToXContent.EMPTY_PARAMS); builder.endObject(); builder.close(); } }; }
public MultiSearchResponse executeSearch(Client elasticClient,RelatedItemSearch[] searches) { MultiSearchRequestBuilder multiSearch = elasticClient.prepareMultiSearch(); for(RelatedItemSearch search : searches) { if(search.getRelatedItemSearchType() == RelatedItemSearchType.FREQUENTLY_RELATED_WITH) { multiSearch.add(createFrequentlyRelatedContentSearch(search,elasticClient)); } } log.debug("executing search {} request(s)",searches.length); return multiSearch.execute().actionGet(searchTimeout, TimeUnit.MILLISECONDS); }
@Override public SearchResultEventWithSearchRequestKey<FrequentlyRelatedSearchResult[]>[] processMultiSearchResponse(RelatedItemSearch[] searches,MultiSearchResponse searchResponse) { int i = 0; SearchResultEventWithSearchRequestKey[] results = new SearchResultEventWithSearchRequestKey[searches.length]; for(MultiSearchResponse.Item item : searchResponse.getResponses()) { SearchRequestLookupKey key = searches[i].getLookupKey(); results[i] = frequentlyRelatedWithResultsConverter(key,item.getResponse(),item.getFailureMessage(),item.isFailure(),searches[i++].getStartOfRequestNanos()); } return results; }
@Test public void testFailureIsReturnedOnException() { ElasticSearchFrequentlyRelatedItemSearchProcessor processor = mock(ElasticSearchFrequentlyRelatedItemSearchProcessor.class); doThrow(new RuntimeException()).when(processor).executeSearch(any(Client.class), any(RelatedItemSearch[].class)); ElasticSearchRelatedItemSearchRepository repository = new ElasticSearchRelatedItemSearchRepository(factory,processor); RelatedItemSearch[] searches = createSearch(); SearchResultEventWithSearchRequestKey[] results = repository.findRelatedItems(configuration, searches); assertEquals(2,results.length); System.out.println("testFailureIsReturnedOnException, Results 0 outcometype: " + results[0].getResponse().getOutcomeType()); System.out.println("testFailureIsReturnedOnException, Results 1 outcometype: " + results[1].getResponse().getOutcomeType()); assertSame(SearchResultsEvent.EMPTY_FAILED_FREQUENTLY_RELATED_SEARCH_RESULTS, results[0].getResponse()); assertSame(SearchResultsEvent.EMPTY_FAILED_FREQUENTLY_RELATED_SEARCH_RESULTS, results[1].getResponse()); reset(processor); MultiSearchResponse res1 = mock(MultiSearchResponse.class); when(processor.executeSearch(any(Client.class), any(RelatedItemSearch[].class))).thenReturn(res1); doThrow(new RuntimeException()).when(processor).processMultiSearchResponse(searches, res1); results = repository.findRelatedItems(configuration, searches); assertEquals(2,results.length); System.out.println("testFailureIsReturnedOnException, Results 0 outcometype: " + results[0].getResponse().getOutcomeType()); System.out.println("testFailureIsReturnedOnException, Results 1 outcometype: " + results[1].getResponse().getOutcomeType()); assertSame(SearchResultsEvent.EMPTY_FAILED_FREQUENTLY_RELATED_SEARCH_RESULTS, results[0].getResponse()); assertSame(SearchResultsEvent.EMPTY_FAILED_FREQUENTLY_RELATED_SEARCH_RESULTS, results[1].getResponse()); }
public void testPercolatorQueryViaMultiSearch() throws Exception { createIndex("test", client().admin().indices().prepareCreate("test") .addMapping("type", "field1", "type=text") .addMapping("queries", "query", "type=percolator") ); client().prepareIndex("test", "queries", "1") .setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "b")).field("a", "b").endObject()) .execute().actionGet(); client().prepareIndex("test", "queries", "2") .setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "c")).endObject()) .execute().actionGet(); client().prepareIndex("test", "queries", "3") .setSource(jsonBuilder().startObject().field("query", boolQuery() .must(matchQuery("field1", "b")) .must(matchQuery("field1", "c")) ).endObject()) .execute().actionGet(); client().prepareIndex("test", "queries", "4") .setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject()) .execute().actionGet(); client().prepareIndex("test", "type", "1") .setSource(jsonBuilder().startObject().field("field1", "c").endObject()) .execute().actionGet(); client().admin().indices().prepareRefresh().get(); MultiSearchResponse response = client().prepareMultiSearch() .add(client().prepareSearch("test") .setQuery(new PercolateQueryBuilder("query", "type", jsonBuilder().startObject().field("field1", "b").endObject().bytes(), XContentType.JSON))) .add(client().prepareSearch("test") .setQuery(new PercolateQueryBuilder("query", "type", yamlBuilder().startObject().field("field1", "c").endObject().bytes(), XContentType.JSON))) .add(client().prepareSearch("test") .setQuery(new PercolateQueryBuilder("query", "type", smileBuilder().startObject().field("field1", "b c").endObject().bytes(), XContentType.JSON))) .add(client().prepareSearch("test") .setQuery(new PercolateQueryBuilder("query", "type", jsonBuilder().startObject().field("field1", "d").endObject().bytes(), XContentType.JSON))) .add(client().prepareSearch("test") .setQuery(new PercolateQueryBuilder("query", "type", "test", "type", "1", null, null, null))) .add(client().prepareSearch("test") // non existing doc, so error element .setQuery(new PercolateQueryBuilder("query", "type", "test", "type", "2", null, null, null))) .get(); MultiSearchResponse.Item item = response.getResponses()[0]; assertHitCount(item.getResponse(), 2L); assertSearchHits(item.getResponse(), "1", "4"); assertThat(item.getFailureMessage(), nullValue()); item = response.getResponses()[1]; assertHitCount(item.getResponse(), 2L); assertSearchHits(item.getResponse(), "2", "4"); assertThat(item.getFailureMessage(), nullValue()); item = response.getResponses()[2]; assertHitCount(item.getResponse(), 4L); assertSearchHits(item.getResponse(), "1", "2", "3", "4"); assertThat(item.getFailureMessage(), nullValue()); item = response.getResponses()[3]; assertHitCount(item.getResponse(), 1L); assertSearchHits(item.getResponse(), "4"); assertThat(item.getFailureMessage(), nullValue()); item = response.getResponses()[4]; assertHitCount(item.getResponse(), 2L); assertSearchHits(item.getResponse(), "2", "4"); assertThat(item.getFailureMessage(), nullValue()); item = response.getResponses()[5]; assertThat(item.getResponse(), nullValue()); assertThat(item.getFailureMessage(), notNullValue()); assertThat(item.getFailureMessage(), equalTo("all shards failed")); assertThat(ExceptionsHelper.unwrapCause(item.getFailure().getCause()).getMessage(), containsString("[test/type/2] couldn't be found")); }
@Override public ActionFuture<MultiSearchResponse> multiSearch(MultiSearchRequest request) { return execute(MultiSearchAction.INSTANCE, request); }
@Override public void multiSearch(MultiSearchRequest request, ActionListener<MultiSearchResponse> listener) { execute(MultiSearchAction.INSTANCE, request, listener); }
/** * This test generates 1-10 random queries and executes a profiled and non-profiled * search for each query. It then does some basic sanity checking of score and hits * to make sure the profiling doesn't interfere with the hits being returned */ public void testProfileMatchesRegular() throws Exception { createIndex("test"); ensureGreen(); int numDocs = randomIntBetween(100, 150); IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs]; for (int i = 0; i < numDocs; i++) { docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource( "field1", English.intToEnglish(i), "field2", i ); } List<String> stringFields = Arrays.asList("field1"); List<String> numericFields = Arrays.asList("field2"); indexRandom(true, docs); refresh(); int iters = between(1, 10); for (int i = 0; i < iters; i++) { QueryBuilder q = randomQueryBuilder(stringFields, numericFields, numDocs, 3); logger.info("Query: {}", q); SearchRequestBuilder vanilla = client().prepareSearch("test") .setQuery(q) .setProfile(false) .addSort("_uid", SortOrder.ASC) .setPreference("_primary") .setSearchType(SearchType.QUERY_THEN_FETCH); SearchRequestBuilder profile = client().prepareSearch("test") .setQuery(q) .setProfile(true) .addSort("_uid", SortOrder.ASC) .setPreference("_primary") .setSearchType(SearchType.QUERY_THEN_FETCH); MultiSearchResponse.Item[] responses = client().prepareMultiSearch() .add(vanilla) .add(profile) .execute().actionGet().getResponses(); SearchResponse vanillaResponse = responses[0].getResponse(); SearchResponse profileResponse = responses[1].getResponse(); float vanillaMaxScore = vanillaResponse.getHits().getMaxScore(); float profileMaxScore = profileResponse.getHits().getMaxScore(); if (Float.isNaN(vanillaMaxScore)) { assertTrue("Vanilla maxScore is NaN but Profile is not [" + profileMaxScore + "]", Float.isNaN(profileMaxScore)); } else { assertTrue("Profile maxScore of [" + profileMaxScore + "] is not close to Vanilla maxScore [" + vanillaMaxScore + "]", nearlyEqual(vanillaMaxScore, profileMaxScore, 0.001)); } assertThat( "Profile totalHits of [" + profileResponse.getHits().getTotalHits() + "] is not close to Vanilla totalHits [" + vanillaResponse.getHits().getTotalHits() + "]", vanillaResponse.getHits().getTotalHits(), equalTo(profileResponse.getHits().getTotalHits())); SearchHit[] vanillaHits = vanillaResponse.getHits().getHits(); SearchHit[] profileHits = profileResponse.getHits().getHits(); for (int j = 0; j < vanillaHits.length; j++) { assertThat("Profile hit #" + j + " has a different ID from Vanilla", vanillaHits[j].getId(), equalTo(profileHits[j].getId())); } } }
@Override public MultiSearchResponse execute() throws ODataApplicationException { return ESClient.getInstance().executeRequest(getQueries()); }
@Override public ActionFuture<MultiSearchResponse> multiSearch(MultiSearchRequest request) { // TODO Auto-generated method stub return null; }
@Override public void multiSearch(MultiSearchRequest request, ActionListener<MultiSearchResponse> listener) { // TODO Auto-generated method stub }
CoordinateMultiSearchResponse() { // hack: empty constructor is private, use this one instead // we use an empty array to avoid NPE during serialization super(new MultiSearchResponse.Item[0]); }
public CoordinateMultiSearchResponse(Item[] items) { super(new MultiSearchResponse.Item[0]); // hack: empty constructor is private this.items = items; }
@Override public Iterator<MultiSearchResponse.Item> iterator() { return Iterators.forArray((MultiSearchResponse.Item[]) items); }
@Override public void execute(final ActionListener<MultiSearchResponse> listener) { client.execute(CoordinateMultiSearchAction.INSTANCE, beforeExecute(request), listener); }
@Test public void testSimpleJoinWithIntegerFields() throws Exception { Settings settings = Settings.settingsBuilder().put("number_of_shards", 1).build(); assertAcked(prepareCreate("index1").setSettings(settings).addMapping("type", "id", "type=integer", "foreign_key", "type=integer")); assertAcked(prepareCreate("index2").setSettings(settings).addMapping("type", "id", "type=integer", "foreign_key", "type=integer", "tag", "type=string")); assertAcked(prepareCreate("index3").setSettings(settings).addMapping("type", "id", "type=integer", "tag", "type=string")); ensureGreen(); indexRandom(true, client().prepareIndex("index1", "type", "1").setSource("id", "1", "foreign_key", new String[]{"1", "3"}), client().prepareIndex("index1", "type", "2").setSource("id", "2"), client().prepareIndex("index1", "type", "3").setSource("id", "3", "foreign_key", new String[]{"2"}), client().prepareIndex("index1", "type", "4").setSource("id", "4", "foreign_key", new String[]{"1", "4"}), client().prepareIndex("index2", "type", "1").setSource("id", "1", "tag", "aaa"), client().prepareIndex("index2", "type", "2").setSource("id", "2", "tag", "aaa"), client().prepareIndex("index2", "type", "3").setSource("id", "3", "foreign_key", new String[]{"2"}, "tag", "bbb"), client().prepareIndex("index2", "type", "4").setSource("id", "4", "tag", "ccc"), client().prepareIndex("index3", "type", "1").setSource("id", "1", "tag", "aaa"), client().prepareIndex("index3", "type", "2").setSource("id", "2", "tag", "aaa"), client().prepareIndex("index3", "type", "3").setSource("id", "3", "tag", "bbb"), client().prepareIndex("index3", "type", "4").setSource("id", "4", "tag", "ccc")); MultiSearchResponse rsp = new CoordinateMultiSearchRequestBuilder(client()) .add( client().prepareSearch("index1").setQuery( boolQuery().filter( QueryBuilders.filterJoin("foreign_key").indices("index2").types("type").path("id").query( boolQuery().filter( QueryBuilders.filterJoin("foreign_key").indices("index3").types("type").path("id").query( boolQuery().filter(termQuery("tag", "aaa")) ) ) ) ) .filter( termQuery("id", "1") ) ) ) .add( client().prepareSearch("index1").setQuery( boolQuery().filter( QueryBuilders.filterJoin("foreign_key").indices("index2").types("type").path("id").query( boolQuery().filter( QueryBuilders.filterJoin("foreign_key").indices("index3").types("type").path("id").query( boolQuery().filter(termQuery("tag", "aaa")) ) ) ) ) ) ).execute().actionGet(); assertEquals(2, rsp.getResponses().length); assertHitCount(rsp.getResponses()[0].getResponse(), 1L); assertSearchHits(rsp.getResponses()[0].getResponse(), "1"); assertHitCount(rsp.getResponses()[1].getResponse(), 1L); assertSearchHits(rsp.getResponses()[1].getResponse(), "1"); }
@Override protected void doExecute(final MultiSearchScriptTemplateRequest request, final ActionListener<MultiSearchScriptTemplateResponse> listener) { final List<Integer> originalSlots = new ArrayList<>(); final MultiSearchRequest multiSearchRequest = new MultiSearchRequest(); multiSearchRequest.indicesOptions(request.indicesOptions()); if (request.maxConcurrentSearchRequests() != 0) { multiSearchRequest.maxConcurrentSearchRequests(request.maxConcurrentSearchRequests()); } final MultiSearchScriptTemplateResponse.Item[] items = new MultiSearchScriptTemplateResponse.Item[request.requests().size()]; for (int i = 0; i < items.length; i++) { final SearchScriptTemplateRequest searchTemplateRequest = request.requests().get(i); final SearchScriptTemplateResponse searchTemplateResponse = new SearchScriptTemplateResponse(); SearchRequest searchRequest; try { searchRequest = convert(searchTemplateRequest, searchTemplateResponse, scriptService, xContentRegistry); } catch (final Exception e) { items[i] = new MultiSearchScriptTemplateResponse.Item(null, e); continue; } items[i] = new MultiSearchScriptTemplateResponse.Item(searchTemplateResponse, null); if (searchRequest != null) { multiSearchRequest.add(searchRequest); originalSlots.add(i); } } multiSearchAction.execute(multiSearchRequest, ActionListener.wrap(r -> { for (int i = 0; i < r.getResponses().length; i++) { final MultiSearchResponse.Item item = r.getResponses()[i]; final int originalSlot = originalSlots.get(i); if (item.isFailure()) { items[originalSlot] = new MultiSearchScriptTemplateResponse.Item(null, item.getFailure()); } else { items[originalSlot].getResponse().setResponse(item.getResponse()); } } listener.onResponse(new MultiSearchScriptTemplateResponse(items)); }, listener::onFailure)); }
@Override public void execute(Tuple input) { _collector.ack(input); // this bolt can be connected to anything // we just want to trigger a new search when the input is a tick tuple if (!TupleUtils.isTick(input)) { return; } Status[] slist = new Status[] { Status.DISCOVERED, Status.ERROR, Status.FETCH_ERROR, Status.FETCHED, Status.REDIRECTION }; MultiSearchRequestBuilder multi = connection.getClient() .prepareMultiSearch(); // should be faster than running the aggregations // sent as a single multisearch for (Status s : slist) { SearchRequestBuilder request = connection.getClient() .prepareSearch(indexName).setTypes(docType).setFrom(0) .setSize(0).setExplain(false); request.setQuery(QueryBuilders.termQuery("status", s.name())); multi.add(request); } long start = System.currentTimeMillis(); MultiSearchResponse response = multi.get(); long end = System.currentTimeMillis(); LOG.info("Multiquery returned in {} msec", end - start); for (int i = 0; i < response.getResponses().length; i++) { SearchResponse res = response.getResponses()[i].getResponse(); long total = res.getHits().getTotalHits(); latestStatusCounts.put(slist[i].name(), total); } }
/** * Tests that a failure to connect to ES is caught, and reported back. */ @Test public void testSearchFailureIsCaught() { setIndexTemplate(); indexDoc(); assertTrue(esServer.indexExists(configuration.getStorageIndexNamePrefix() + "-2013-02-11")); ElasticSearchFrequentlyRelatedItemSearchProcessor searcher = new ElasticSearchFrequentlyRelatedItemSearchProcessor(configuration,new FrequentRelatedSearchRequestBuilder(configuration),RelatedItemNoopGetRepository.INSTANCE); RelatedItemSearch[] search = new RelatedItemSearch[] {createIdSearch("emmerdale")}; shutdownElastic(); MultiSearchResponse response = searcher.executeSearch(clientFactory.getClient(),search); SearchResultEventWithSearchRequestKey[] result = searcher.processMultiSearchResponse(search,response); SearchRequestLookupKey key = result[0].getRequest(); SearchResultsEvent<FrequentlyRelatedSearchResult[]> event = result[0].getResponse(); assertEquals(0, event.getSearchResults().length); assertEquals(SearchResultsOutcome.FAILED_REQUEST, event.getOutcomeType()); }
/** * Tests that an empty results set is returned when no related products are found */ @Test public void testNoRelatedItems() { setIndexTemplate(); indexDoc(); assertTrue(esServer.indexExists(configuration.getStorageIndexNamePrefix() + "-2013-02-11")); ElasticSearchFrequentlyRelatedItemSearchProcessor searcher = new ElasticSearchFrequentlyRelatedItemSearchProcessor(configuration,new FrequentRelatedSearchRequestBuilder(configuration),RelatedItemNoopGetRepository.INSTANCE); RelatedItemSearch[] search = new RelatedItemSearch[] {createIdSearch("elf")}; MultiSearchResponse response = searcher.executeSearch(clientFactory.getClient(),search); SearchResultEventWithSearchRequestKey<FrequentlyRelatedSearchResult[]>[] result = searcher.processMultiSearchResponse(search,response); SearchRequestLookupKey key = result[0].getRequest(); SearchResultsEvent<FrequentlyRelatedSearchResult[]> event = result[0].getResponse(); assertEquals(0, event.getSearchResults().length); assertEquals(SearchResultsOutcome.EMPTY_RESULTS,event.getOutcomeType()); }
public void allReader(ElastisearchCrawlerMultipleHits crawler) throws Exception { try { MultiSearchResponse.Item[] items = Elasticsearch.withRetry(new ElasticsearchWithRetry<Item[]>() { public Item[] call(Client client) throws NoNodeAvailableException { MultiSearchRequestBuilder multisearchrequestbuilder = new MultiSearchRequestBuilder(client); for (int pos = 0; pos < queries.size(); pos++) { multisearchrequestbuilder.add(queries.get(pos).getRequest()); } return multisearchrequestbuilder.execute().actionGet().getResponses(); } }); SearchHit[] hits; SearchResponse response; for (int pos_response = 0; pos_response < items.length; pos_response++) { if (items[pos_response].isFailure()) { Loggers.ElasticSearch.warn("Search failed", new Exception(items[pos_response].getFailureMessage())); continue; } response = items[pos_response].getResponse(); if (response == null) { continue; } if (response.getHits() == null) { continue; } hits = response.getHits().hits(); if (hits.length == 0) { continue; } if (crawler.onMultipleResponse(response, Arrays.asList(hits)) == false) { return; } } } catch (IndexMissingException ime) { /** * No items == no callbacks */ Loggers.ElasticSearch.debug("Index missing", ime); return; } catch (SearchPhaseExecutionException e) { Loggers.ElasticSearch.debug("No datas", e); } }
@Override protected ActionFuture<MultiSearchResponse> doExecute(MultiSearchRequest request) { return client.multiSearch(request); }
@Override protected XContentBuilder toXContent(MultiSearchRequest request, MultiSearchResponse response, XContentBuilder builder) throws IOException { return super.toXContent(request, response, builder.startObject()).endObject(); }
@Test public void testSearchById() { setIndexTemplate(); indexDoc(); assertTrue(esServer.indexExists(configuration.getStorageIndexNamePrefix() + "-2013-02-11")); ElasticSearchFrequentlyRelatedItemSearchProcessor searcher = new ElasticSearchFrequentlyRelatedItemSearchProcessor(configuration,new FrequentRelatedSearchRequestBuilder(configuration),RelatedItemNoopGetRepository.INSTANCE); RelatedItemSearch[] search = new RelatedItemSearch[] {createIdSearch("emmerdale")}; MultiSearchResponse response = searcher.executeSearch(clientFactory.getClient(),search); assertTrue(response != null); System.out.println(response); assertEquals("Did not return the expected 1 search result, for emmerdale search", 1, response.getResponses().length); System.out.println(response.getResponses()[0].getFailureMessage()); assertTrue("Search Response should not be null", response.getResponses()[0].getResponse() != null); assertEquals(1,response.getResponses()[0].getResponse().getFacets().getFacets().size()); Facet f = response.getResponses()[0].getResponse().getFacets().getFacets().get(configuration.getStorageFrequentlyRelatedItemsFacetResultsFacetName()); assertTrue(f instanceof TermsFacet); TermsFacet tf = (TermsFacet)f; assertEquals(1,tf.getEntries().size()); assertEquals("the bill",tf.getEntries().get(0).getTerm().string()); SearchResultEventWithSearchRequestKey[] results = searcher.processMultiSearchResponse(search,response); assertTrue(results != null); assertEquals("Should have a result",1,results.length); verifyTermsInOutput(results[0].getResponse(),tf); }
/** * Performs multiple search requests. */ ActionFuture<MultiSearchResponse> multiSearch(MultiSearchRequest request);