Elasticsearch学习之Java操作1
1. Elasticsearch为Java用户提供了两种内置客户端
1.1 节点客户端(node client):
节点客户端以无数据节点(none data node)身份加入集群,换言之,它自己不存储任何数据,但是它知道数据在集群中的具体位置,并且能够直接转发请求到对应的节点上。
传输客户端(Transport client):这个更轻量的传输客户端能够发送请求到远程集群。它自己不加入集群,只是简单转发请求给集群中的节点。
两个Java客户端都通过9300端口与集群交互,使用Elasticsearch传输协议(Elasticsearch Transport Protocol)。集群中的节点之间也通过9300端口进行通信。如果此端口未开放,你的节点将不能组成集群。+
Java客户端所在的Elasticsearch版本必须与集群中其他节点一致,否则,它们可能互相无法识别。
1.2 通过transportClient来连接集群(推荐)
public void before11() throws Exception { // 创建客户端, 使用的默认集群名, "elasticSearch" // client = TransportClient.builder().build().addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("www.wenbronk.com"), 9300)); // 通过setting对象指定集群配置信息, 配置的集群名 Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch_wenbronk") // 设置集群名 // .put("client.transport.sniff", true) // 开启嗅探 , 开启后会一直连接不上, 原因未知 // .put("network.host", "192.168.50.37") .put("client.transport.ignore_cluster_name", true) // 忽略集群名字验证, 打开后集群名字不对也能连接上 // .put("client.transport.nodes_sampler_interval", 5) //报错, // .put("client.transport.ping_timeout", 5) // 报错, ping等待时间, .build(); client = TransportClient.builder().settings(settings).build() .addTransportAddress(new InetSocketTransportAddress(new InetSocketAddress("192.168.50.37", 9300))); // 默认5s // 多久打开连接, 默认5s System.out.println("success connect"); }
2. 使用QueryBuilder 进行查询
使用QueryBuilder
2.1 termQuery("key", obj) 完全匹配
2.1 termsQuery("key", obj1, obj2..) 一次匹配多个值
2.3 matchQuery("key", Obj) 单个匹配, field不支持通配符, 前缀具高级特性
2.4 multiMatchQuery("text", "field1", "field2"..); 匹配多个字段, field有通配符就行
2.5 matchAllQuery(); 匹配所有文件
@Test public void testQueryBuilder() { // QueryBuilder queryBuilder = QueryBuilders.termQuery("user", "kimchy"); QueryBUilder queryBuilder = QueryBuilders.termQuery("user", "kimchy", "wenbronk", "vini"); QueryBuilders.termsQuery("user", new ArrayList<String>().add("kimchy")); // QueryBuilder queryBuilder = QueryBuilders.matchQuery("user", "kimchy"); // QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery("kimchy", "user", "message", "gender"); QueryBuilder queryBuilder = QueryBuilders.matchAllQuery(); searchFunction(queryBuilder); }
2.6 查询遍历
private void searchFunction(QueryBuilder queryBuilder) { SearchResponse response = client.prepareSearch("twitter") .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) .setScroll(new TimeValue(60000)) .setQuery(queryBuilder) .setSize(100).execute().actionGet(); while(true) { response = client.prepareSearchScroll(response.getScrollId()) .setScroll(new TimeValue(60000)).execute().actionGet(); for (SearchHit hit : response.getHits()) { Iterator<Entry<String, Object>> iterator = hit.getSource().entrySet().iterator(); while(iterator.hasNext()) { Entry<String, Object> next = iterator.next(); System.out.println(next.getKey() + ": " + next.getValue()); if(response.getHits().hits().length == 0) { break; } } } break; } // testResponse(response); }
2.7 组合查询
must(QueryBuilders) : AND
mustNot(QueryBuilders): NOT
should: : OR
@Test public void testQueryBuilder2() { QueryBuilder queryBuilder = QueryBuilders.boolQuery() .must(QueryBuilders.termQuery("user", "kimchy")) .mustNot(QueryBuilders.termQuery("message", "nihao")) .should(QueryBuilders.termQuery("gender", "male")); searchFunction(queryBuilder); }