文章目录
依赖
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.3.1</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.3.1</version> </dependency> <dependency> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport-netty4-client</artifactId> <version>6.3.1</version> </dependency>
测试数据
{ "took": 31, "timed_out": false, "_shards": { "total": 6, "successful": 6, "skipped": 0, "failed": 0 }, "hits": { "total": 7, "max_score": 1, "hits": [ { "_index": ".kibana", "_type": "doc", "_id": "config:6.3.1", "_score": 1, "_source": { "type": "config", "updated_at": "2020-06-23T13:37:22.129Z", "config": { "buildNum": 17276, "telemetry:optIn": false } } }, { "_index": "test_index", "_type": "doc", "_id": "8", "_score": 1, "_source": { "username": "opop", "age": 689, "birth": "2008-10-15", "year": 96, "tags": [ "9", "ll" ], "money": "112.1" } }, { "_index": "test_index", "_type": "doc", "_id": "2", "_score": 1, "_source": { "username": "das", "age": 12, "birth": "1995-12-15", "year": 12, "tags": [ "12", "fashion" ], "money": "1.1" } }, { "_index": "test_index", "_type": "doc", "_id": "4", "_score": 1, "_source": { "username": "opop", "age": 69, "birth": "1986-10-15", "year": 96, "tags": [ "9", "ll" ], "money": "121.1" } }, { "_index": "test_index", "_type": "doc", "_id": "1", "_score": 1, "_source": { "username": "samualz", "age": 14, "birth": "1991-12-15", "year": 18, "tags": [ "boy", "fashion" ], "money": "100.1" } }, { "_index": "test_index", "_type": "doc", "_id": "7", "_score": 1, "_source": { "username": "opop", "age": 69, "birth": "2006-10-15", "year": 96, "tags": [ "9", "ll" ], "money": "121.1" } }, { "_index": "test_index", "_type": "doc", "_id": "3", "_score": 1, "_source": { "username": "abc", "age": 8, "birth": "1995-10-15", "year": 12, "tags": [ "13", "ple" ], "money": "21.1" } } ] }}
获取客户端
package com.zyd.es.config;import org.elasticsearch.client.transport.TransportClient;import org.elasticsearch.common.settings.Settings;import org.elasticsearch.common.transport.TransportAddress;import org.elasticsearch.transport.client.PreBuiltTransportClient;import org.springframework.boot.context.properties.ConfigurationProperties;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import java.net.InetAddress;import java.net.UnknownHostException;@ConfigurationProperties(prefix = "es")@Configuration public class EsCommonConfig { private String clusterName; private String nodes; @Bean public TransportClient getClient() throws UnknownHostException { String[] node = nodes.split(","); Settings settings = Settings .builder() .put("cluster.name", clusterName) //自动发现我们其他的es的服务器 .put("client.transport.sniff", "true") .build(); TransportClient client = new PreBuiltTransportClient(settings) .addTransportAddress(new TransportAddress(InetAddress.getByName(node[0].split(":")[0]), Integer.valueOf(node[0].split(":")[1]))) .addTransportAddress(new TransportAddress(InetAddress.getByName(node[1].split(":")[0]), Integer.valueOf(node[1].split(":")[1]))) .addTransportAddress(new TransportAddress(InetAddress.getByName(node[2].split(":")[0]), Integer.valueOf(node[2].split(":")[1]))); return client; }}
模糊匹配查询(bug未解决: 无法匹配数字)
TransportClient client = getClient(); Map<String, String> map = new HashMap<>(); map.put("username", "?amualz"); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); for (Map.Entry<String, String> entry : map.entrySet()) { String value = entry.getValue(); String key = entry.getKey(); //使用通配符查询,*代表0个或多个字母,?代表0个或1个字母。 boolQueryBuilder.must(QueryBuilders.wildcardQuery(key, value)); } SearchResponse response = client.prepareSearch("test_index").setTypes("doc") .setQuery(boolQueryBuilder) .setExplain(true) .execute() .actionGet(); List list = responseToList(client, response); System.out.println(list.toString());
精确匹配 termQuery
常见的是Bool查询,包括must、should、filter、must_not,依次对应为and,or,过滤,非的关系
@Test public void query() throws UnknownHostException { TransportClient client = getClient(); Map<String, String> map = new HashMap<>(); map.put("username", "samualz"); BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); /* for (Map.Entry<String, String> entry : map.entrySet()) { String value = entry.getValue(); String key = entry.getKey(); //使用通配符查询,*代表0个或多个字母,?代表0个或1个字母。 boolQueryBuilder.must(QueryBuilders.wildcardQuery(key, value)); } */ for (Map.Entry<String, String> entry : map.entrySet()) { String value = entry.getValue(); String key = entry.getKey(); //使用通配符查询,*代表0个或多个字母,?代表0个或1个字母。 boolQueryBuilder.must(QueryBuilders.termQuery(key, value)); } SearchResponse response = client.prepareSearch("test_index").setTypes("doc") .setQuery(boolQueryBuilder) .setExplain(true) .execute() .actionGet(); List list = responseToList(client, response); System.out.println(list.toString()); }
执行全文检索 Multi Match Query 搜索所有的value值
@Test public void MultiMatchQueryBuilderQuery() throws UnknownHostException { TransportClient client = getClient(); MultiMatchQueryBuilder queryBuilder = new MultiMatchQueryBuilder("samualz"); SearchResponse response = client.prepareSearch("test_index").setTypes("doc") .setQuery(queryBuilder) .setExplain(true) .execute() .actionGet(); List list = responseToList(client, response); System.out.println(list.toString()); }
同时也支持多字段匹配
根据id查询数据
@Test public void queryById() throws UnknownHostException { TransportClient client = getClient(); GetResponse getResponse = client.prepareGet("test_index", "doc", "1").get(); String sourceAsString = getResponse.getSourceAsString(); System.out.println(sourceAsString); }