博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java操作elasticsearch实现前缀查询、wildcard、fuzzy模糊查询、ids查询
阅读量:6717 次
发布时间:2019-06-25

本文共 5739 字,大约阅读时间需要 19 分钟。

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
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
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
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
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
map = hit.getSourceAsMap(); for(String key:map.keySet()) { System.out.println(key +" key对应的值为:" +map.get(key)); } } }

下一篇博客本人将书写java操作elasticsearch实现聚合查询。对后期博客感兴趣的朋友可以关注交流,转发请说明出处,本人的博客地址为:https://www.cnblogs.com/chenyuanbo/

技术在于交流!

 

转载于:https://www.cnblogs.com/chenyuanbo/p/10296845.html

你可能感兴趣的文章
ORCALE 分页查询order by 的字段数据重复问题
查看>>
C++文件读写操作
查看>>
mybatis service层操作,这个返回什么?
查看>>
将Mule ESB Http项目转换为Tomcat项目(2) ESB项目创建
查看>>
wxGraphicsContext,wxGraphicsPath怎么使用
查看>>
点击率模型
查看>>
02,需求 ,jfinal,项目要求和数据库设计
查看>>
百度api开发小试牛刀
查看>>
webservice接口资料
查看>>
Linux FTP的安装与配置
查看>>
ajax---
查看>>
失控读书笔记(1)规则
查看>>
关于为什么类的静态成员变量不能立即初始化
查看>>
Tomcat设置URI编码
查看>>
大数据量下高并发同步的讲解(不看,保证你后悔)
查看>>
Perl - Statistics::R
查看>>
库的层次设计
查看>>
Java正则表达式工具类
查看>>
开启智慧家庭娱乐新纪元-郭振平
查看>>
专家评说SQL Server 2014:技术资料精选
查看>>