ES查询(难易度偏上)
###1.多文档查询
- 标识符过滤器(ids):指定的标识符是文档的_id,为文档唯一标识 GET/…/…/_search
- multi Get:多字段查询可以设置多个文档查询条件,每个查询条件在结构上都比较类似 GET /_mget
MultiGetResponse multiGetResponse = client.prepareMultiGet()
.add("ecommerce", "product", "1")
.add("ecommerce", "product", "1","2", "3")
.get();
for (MultiGetItemResponse itemResponse : multiGetResponse) {
GetResponse response = itemResponse.getResponse();
if (response.isExists()) {
String sourceAsString = response.getSourceAsString();
System.out.println(sourceAsString);
}
}
- _source过滤:包括_source,_source_include,_source_exclude
{
"_index" : "test",
"_type" : "type",
"_id" : "3",
"_source" : {
"include": ["user"],
"exclude": ["user.location"]
}
}
###2.分页查询
- scroll性能较高
/**
* 1:通过 setting对象来指定集群配置信息
*/
// 先构建client
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").build();
/**
* 2:创建客户端 通过setting来创建,若不指定则默认链接的集群名为elasticsearch 链接使用tcp协议即9300
*/
TransportClient client = new PreBuiltTransportClient(settings);
TransportAddress transportAddress = new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300);
client.addTransportAddresses(transportAddress);
/**
* 3:查看集群信息 注意我的集群结构是: 131的elasticsearch.yml中指定为主节点不能存储数据,
* 128的elasticsearch.yml中指定不为主节点只能存储数据。 所有控制台只打印了192.168.79.128,只能获取数据节点
*/
List<DiscoveryNode> connectedNodes = client.connectedNodes();
for (DiscoveryNode node : connectedNodes) {
System.out.println(node.getHostAddress());
}
System.out.println("scroll 模式启动!");
begin = new Date();
SearchResponse scrollResponse = client.prepareSearch(INDEX)
.setSearchType(SearchType.SCAN).setSize(10000).setScroll(TimeValue.timeValueMi