网上搜到批量查询可以通过TransportClient实现,但官方推荐使用RestHighLevelClient实现
注意:
We plan on deprecating the TransportClient in Elasticsearch 7.0 and removing it completely in 8.0. Instead, you should be using the Java High Level REST Client, which executes HTTP requests rather than serialized Java requests.
查找最新的RestHighLevelClient api文档
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.5/java-rest-high-multi-search.html
MultiSearchRequest request = new MultiSearchRequest(); SearchRequest firstSearchRequest = new SearchRequest(); SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("user", "kimchy")); firstSearchRequest.source(searchSourceBuilder); request.add(firstSearchRequest); SearchRequest secondSearchRequest = new SearchRequest(); searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(QueryBuilders.matchQuery("user", "luca")); secondSearchRequest.source(searchSourceBuilder); request.add(secondSearchRequest); MultiSearchResponse response = client.msearch(request, RequestOptions.DEFAULT);
但尝试发现,没有msearch这个方法,发现版本为6.1的 不支持这个方法
https://www.elastic.co/guide/en/elasticsearch/client/java-api/6.1/java-search-msearch.html
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) .get(); // 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(); }
只能使用TransportClient来实现
import java.io.IOException; import java.net.InetAddress; import org.elasticsearch.action.search.MultiSearchResponse; import org.elasticsearch.action.search.MultiSearchResponse.Item; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.TransportAddress; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.transport.client.PreBuiltTransportClient; public class App4 { @SuppressWarnings("resource") public static void main(String[] args) throws IOException { TransportClient client=new PreBuiltTransportClient(Settings.EMPTY) .addTransportAddress(new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); //1st ES Node host and port SearchRequest req=new SearchRequest("person"); SearchSourceBuilder builder=new SearchSourceBuilder(); builder.size(10); req.source(builder); SearchRequest req1=new SearchRequest("posts"); SearchSourceBuilder builder1=new SearchSourceBuilder(); builder.size(10); req.source(builder1); MultiSearchResponse bulkResponse=client.prepareMultiSearch() .add(req) .add(req1) .get(); for (Item item : bulkResponse.getResponses()) { if (item.isFailure()) { System.out.println(item.getFailureMessage()); }else { System.out.println("-----------------------------------------------------------------------"); System.out.println(item.getResponse().toString()); } } /**** Done ****/ System.out.println("Done"); } }
微信公众号: 架构师日常笔记 欢迎关注!