1、前缀查询(prefix)
//prefix前缀查询 @Test public void test15() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(settings) //获取es主机中节点的ip地址及端口号(以下是单个节点案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //prefixQuery 第一个参数为字段名,后面是以zhao开头的条件进行查询 QueryBuilder builder = QueryBuilders.prefixQuery("name", "zhao"); SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //将获取的值转换成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key对应的值为:" +map.get(key)); } } }
2、wildcard模糊查询
//wildcard模糊查询 @Test public void test16() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(settings) //获取es主机中节点的ip地址及端口号(以下是单个节点案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //wildcardQuery模糊查询 QueryBuilder builder = QueryBuilders.wildcardQuery("name", "zhao*"); SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //将获取的值转换成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key对应的值为:" +map.get(key)); } } }
3、fuzzy模糊查询
说明:fuzzy才是实现真正的模糊查询,我们输入的字符可以是个大概,他可以根据我们输入的文字大概进行匹配查询,具体可看文章中的解释和代码,注意与wildcard模糊查询的区别
//fuzzy模糊查询(输入的值输入个大概也可以查询出来) @Test public void test17() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(settings) //获取es主机中节点的ip地址及端口号(以下是单个节点案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //fuzzyQuery模糊查询 QueryBuilder builder = QueryBuilders.fuzzyQuery("name", "chagge"); SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //将获取的值转换成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key对应的值为:" +map.get(key)); } } } //typeQuery类型查询 @Test public void test18() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(settings) //获取es主机中节点的ip地址及端口号(以下是单个节点案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //typeQuery类型查询 QueryBuilder builder = QueryBuilders.typeQuery("blog"); SearchResponse response = client.prepareSearch("index1").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //将获取的值转换成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key对应的值为:" +map.get(key)); } } }
4、ids查询
说明:根据多个id值进行查询
//idsQuery id查询(可以同时根据多个id进行查询) @Test public void test19() throws UnknownHostException { //1、指定es集群 cluster.name 是固定的key值,my-application是ES集群的名称 Settings settings = Settings.builder().put("cluster.name", "my-application").build(); //2.创建访问ES服务器的客户端 TransportClient client = new PreBuiltTransportClient(settings) //获取es主机中节点的ip地址及端口号(以下是单个节点案例) .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.1.94"), 9300)); //idsQuery id查询 QueryBuilder builder = QueryBuilders.idsQuery("1","2"); SearchResponse response = client.prepareSearch("lib3").setQuery(builder).get(); SearchHits hits = response.getHits(); for(SearchHit hit:hits) { System.out.println(hit.getSourceAsString()); //将获取的值转换成map的形式 Map<String, Object> map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key对应的值为:" +map.get(key)); } } }
下一篇博客本人将书写java操作elasticsearch实现聚合查询。对后期博客感兴趣的朋友可以关注交流,转发请说明出处,本人的博客地址为:https://www.cnblogs.com/chenyuanbo/
技术在于交流!