(21)ElasticSearch java项目中match、multimath、term、terms以及范围、前缀、通配符、模糊、类型、ids等查询示例

  1、查询index1中content字段包含工厂的文档

@Test
    public void testMatch() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象
        QueryBuilder qb = QueryBuilders.matchQuery("content","工厂");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .setSize(3)//查询出3个
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  2、查询index1中title字段或者content字段包含设计的文档

@Test
    public void testMultiMatch() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询title或者content包含设计的
        QueryBuilder qb = QueryBuilders.multiMatchQuery("设计","title","content");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .setSize(3)//查询出3个
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  3、查询index1中title字段包含设计的

@Test
    public void testTermQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询title包含设计的
        QueryBuilder qb = QueryBuilders.termQuery("title","设计");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .setSize(3)//查询出3个
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  4、在index1中查询title字段包含设计或者静态工厂的

@Test
    public void testTermsQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询title包含设计或者静态工厂的
        QueryBuilder qb = QueryBuilders.termsQuery("title","设计","静态工厂");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .setSize(3)//查询出3个
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  5、在index1中查询postdate的范围在2018-05-20到2018-05-21之间的

@Test
    public void testRangeQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询postdate的范围在2018-05-20到2018-05-21之间的
        QueryBuilder qb = QueryBuilders.rangeQuery("postdate").from("2018-05-20").to("2018-05-21").format("yyyy-MM-dd");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  6、在index1中查询url字段以csdn开头的文档

@Test
    public void testPrefixQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询url以csdn开头的文档
        QueryBuilder qb = QueryBuilders.prefixQuery("url","csdn");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  7、在index1中查询url字段以csdn开头的文档

@Test
    public void testWildcardQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询url以csdn开头的文档
        QueryBuilder qb = QueryBuilders.wildcardQuery("url","csdn*");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")
                                  .setQuery(qb)
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  8、在index1中模糊查询interests字段中包含changge的文档

@Test
    public void testFuzzyQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询interests字段中包含changge的文档,("interests":"xi huan tingyinyue,changge,tiaowu")
        QueryBuilder qb = QueryBuilders.fuzzyQuery("interests","chagge");//即使changge写成chagge也能模糊查询出来
        //执行查询
        SearchResponse sr = client.prepareSearch("lib3")
                                  .setQuery(qb)
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  9、在index1中查询类型是blog的文档

@Test
    public void testTypeQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询类型是blog的
        QueryBuilder qb = QueryBuilders.typeQuery("blog");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")//索引是index1的
                                  .setQuery(qb)
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  10、在索引lib3中查询name是zhangsan的文档(类似于GET lib3/user/_search?q=name:zhangsan)

@Test
    public void testCommonTermsQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询name是zhangsan的
        QueryBuilder qb = QueryBuilders.commonTermsQuery("name","zhangsan");
        //执行查询
        SearchResponse sr = client.prepareSearch("lib3")
                                  .setQuery(qb)
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  11、在索引lib3中查询name字段是zhangsan的文档(不计算分数查询) 

@Test
    public void testConstantScoreQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询name字段是zhangsan的文档,不计算分数查询
        QueryBuilder qb = QueryBuilders.constantScoreQuery(QueryBuilders.termQuery("name","zhangsan"));
        //执行查询
        SearchResponse sr = client.prepareSearch("lib3")
                                  .setQuery(qb)
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

  12、在index1中查询id是4、8的文档

@Test
    public void testIdsQuery() throws IOException, InterruptedException, ExecutionException {
        //指定集群
        Settings settings = Settings.builder().put("cluster.name","my-application").build();
        //创建客户端
        TransportClient client = new PreBuiltTransportClient(settings)
                                .addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.43.151"),9300));
        //创建查询对象,查询id是4、8的
        QueryBuilder qb = QueryBuilders.idsQuery().addIds("4","8");
        //执行查询
        SearchResponse sr = client.prepareSearch("index1")//索引是index1
                                  .setQuery(qb)
                                  .get();
        //获取结果
        SearchHits hits = sr.getHits();
        for(SearchHit hit:hits) {
            //输出json
            System.out.println(hit.getSourceAsString());
            //输出json的key与value
            Map<String, Object> map = hit.getSourceAsMap();
            for(String key:map.keySet()) {
                System.out.println(key+"="+map.get(key));
            }
        }
        client.close();
   }

原文地址:https://www.cnblogs.com/javasl/p/12070427.html

时间: 2024-10-07 05:31:44

(21)ElasticSearch java项目中match、multimath、term、terms以及范围、前缀、通配符、模糊、类型、ids等查询示例的相关文章

(20)ElasticSearch java项目中的根据指定条件删除文档和查询所有文档

1.删除index1中,title包含工厂的文档 @Test public void testQueryDelete() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application").build(); //创建客户端 TransportCl

(19)ElasticSearch java项目中的批量操作mget和bulk

1.查询索引是index1,类型是blog,id是8.10和索引是lib3,类型是user,id是1.2.3的文档 @Test public void testMultiGet() throws IOException, InterruptedException, ExecutionException { //指定集群 Settings settings = Settings.builder().put("cluster.name","my-application"

ckeditor编辑器在java项目中配置

一.基本使用: 1.所需文件架包 A. Ckeditor基本文件包,比如:ckeditor_3.6.2.zip 下载地址:http://ckeditor.com/download 2.配置使用 A.将下载下来的CKEditor压缩解压,将解压后的文件夹("ckeditor")拷贝进项目里面,比如我是放在"WebContent"的"commons"文件夹下: B.在需要使用CKEditor的页面引入CKEditor的支持javascript <

JAVA项目中的常用的异常处理情况1

Java项目中常用的异常处理情况 1.java.lang.NullPointerException(空指针异常) 调用了未经初始化的对象或者是不存在的对象.经常出现在创建图片,调用数组这些操作中,比如图片未经初始化,或者图片创建时的路径错误等等.对数组操作中出现空指针, 即把数组的初始化和数组元素的初始化混淆起来了.数组的初始化是对数组分配需要的空间,而初始化后的数组,其中的元素并没有实例化, 依然是空的,所以还需要对每个元素都进行初始化(如果要调用的话). 2. java.lang.Class

java项目中可能会使用到的jar包解释

一.Struts2 用的版本是struts2.3.1.1 一个简单的Struts项目所需的jar包有如下8个 1. struts2-core-2.3.1.1.jar: Struts2的核心类库. 2. xwork-core-2.3.1.1.jar: XWork核心类,XWork是一个标准的command模式实现,并且完全从web层剥离出来.WebWork被构建在Xwork上,而Struts2由Struts1和WebWork两个经典的MVC框架发展而来. 3. ognl-3.0.3.jar: 支持

JAVA项目中公布WebService服务——简单实例

1.在Java项目中公布一个WebService服务: 怎样公布? --JDK1.6中JAX-WS规范定义了怎样公布一个WebService服务. (1)用jdk1.6.0_21以后的版本号公布. (2)与Web服务相关的类,都位于Javax.jws.*包中 @WebService--此注解用在类上指定将此类公布成一个WebService: EndPoint--此类为端点服务类,当中publish()方法用于将一个已经加入了@WebService注解对象绑定到一个地址的port上,用于公布. 2

JAVA项目中发布WebService服务——简单实例

1,在Java项目中发布一个WebService服务: 如何发布? --JDK1.6中JAX-WS规范定义了如何发布一个WebService服务: (1)用jdk1.6.0_21以后的版本发布: (2)与Web服务相关的类,都位于Javax.jws.*包中 @WebService--此注解用在类上指定将此类发布成一个WebService: EndPoint--此类为端点服务类,其中publish()方法用于将一个已经添加了@WebService注解对象绑定到一个地址的端口上,用于发布. 2,例子

在MyEclipse的web项目/java项目中,使用Hibernate-tools中的hbm2java和hbm2ddl工具,根据hbm文件自动生成pojo和数据库脚本

     首先,我一定要吐槽下,这个Ant管理部署项目的工具,以及hibernate刚刚学习,导入我这一个简单的问题整了一天多,实在效率有点低下.在这两天中,①了解了Ant,知道了在Ant中很灵活的步骤项目的情况,知道了build.xml文件的一些基本写法.②还学习了在MyEclipse这样的集成工具中完成项目部署.③以及在这两种情况中,利用hibernateTools中的hbm2java和hbm2ddl工具,根据对象关系映射文件,自动生成POJO以及SQL文件(就是数据表). 一,在web项目

java项目中META-INF的作用中MANIFEST.MF的作用(转载)

发布Java应用程序时你会感到困难?好在Java提供了一系列打包和发布工具,可以显著的简化发布过程 该文章提供了打包Java code的几种方法,我们将会探讨Java manifest 文件,给出用于管理JAR文件所依赖文件.估计跨平台发布所需的CLasspath的合适方法.我也会解释如何使用manifest包版本特性来确认包的兼容性... 什么是JAR文件? 在开发过程中,我们可以直接使用Java class文件来运行程序,但这并不是一个好方式,好在Java 提供了 JAR(Java Arch