• springboot整合es客户端操作elasticsearch(四)


    对文档查询,在实际开发中,对文档的查询也是偏多的,记得之前在mou快递公司,做了一套事实的揽件数据操作,就是通过这个来存储数据的,由于一天的数据最少拥有3500万数据

    所以是比较多的,而且还要求查询速度特别快,所以就选择了这个作为数据库

    首先向es库中插入数据

    首先新建索引:

     当然也可以使用api一次操作:

    向es中创建映射:

    我采用的postman进行的,所以

    json:

    {
        "properties": {
            "description": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            },
            "name": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_smart"
            },
            "pic": {
                "type": "text",
                "index": false
            },
            "price": {
                "type": "float"
            },
            "studymodel": {
                "type": "keyword"
            },
            "timestamp": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
            }
        }
    }

    这个json数据我是从网上找的一个课程相关的内容。

    插入数据:

    使用postman

    http://localhost:9200/chenxuyou/doc/9
    
    {
    "name": "es开发基础",
    "description": "在es领域非常流行,es程序员都在用。",
    "studymodel": "2012001",
    "price":813.6,
    "timestamp":"2019-11-14 19:11:35",
    "pic":"group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
    }

    参数后面的那个9,这个是id的意思,如果哦我们不传输,那么es会自动给我生成这个,

    使用postman

    http://localhost:9200/_search

    结果: 返回所有的数据,所有的索引下面的数据:

    {
        "took": 2,
        "timed_out": false,
        "_shards": {
            "total": 2,
            "successful": 2,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 14,
            "max_score": 1,
            "hits": [
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "1",
                    "_score": 1,
                    "_source": {
                        "name": "Bootstrap开发",
                        "description": "Bootstrap是由Twitter推出的一个前台页面开发框架,是一个非常流行的开发框架,此框架集成了多种页面效果。此开发框架包含了大量的CSS、JS程序代码,可以帮助开发者(尤其是不擅长页面开发的程序人员)轻松的实现一个不受浏览器限制的精美界面效果。",
                        "studymodel": "201002",
                        "price": 38.6,
                        "timestamp": "2018-04-25 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "2",
                    "_score": 1,
                    "_source": {
                        "name": "java编程基础",
                        "description": "java语言是世界第一编程语言,在软件开发领域使用人数最多。",
                        "studymodel": "201001",
                        "price": 68.6,
                        "timestamp": "2018-03-25 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "3",
                    "_score": 1,
                    "_source": {
                        "name": "spring开发基础",
                        "description": "spring 在java领域非常流行,java程序员都在用。",
                        "studymodel": "201001",
                        "price": 88.6,
                        "timestamp": "2018-02-24 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "4",
                    "_score": 1,
                    "_source": {
                        "name": "开发基础",
                        "description": "在java领域非常流行,java程序员都在用。",
                        "studymodel": "201001",
                        "price": 8.6,
                        "timestamp": "2019-02-24 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "5",
                    "_score": 1,
                    "_source": {
                        "name": "js开发基础",
                        "description": "在js领域非常流行,js程序员都在用。",
                        "studymodel": "201001",
                        "price": 83.6,
                        "timestamp": "2019-12-24 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "6",
                    "_score": 1,
                    "_source": {
                        "name": "go开发基础",
                        "description": "在go领域非常流行,go程序员都在用。",
                        "studymodel": "201001",
                        "price": 813.6,
                        "timestamp": "2019-12-14 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "7",
                    "_score": 1,
                    "_source": {
                        "name": "python开发基础",
                        "description": "在python领域非常流行,python程序员都在用。",
                        "studymodel": "201001",
                        "price": 8134.6,
                        "timestamp": "2019-12-14 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "8",
                    "_score": 1,
                    "_source": {
                        "name": "php开发基础",
                        "description": "在php领域非常流行,php程序员都在用。",
                        "studymodel": "201001",
                        "price": 813.6,
                        "timestamp": "2019-12-14 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou",
                    "_type": "doc",
                    "_id": "9",
                    "_score": 1,
                    "_source": {
                        "name": "es开发基础",
                        "description": "在es领域非常流行,es程序员都在用。",
                        "studymodel": "2012001",
                        "price": 813.6,
                        "timestamp": "2019-11-14 19:11:35",
                        "pic": "group1/M00/00/00/wKhlQFs6RCeAY0pHAAJx5ZjNDEM428.jpg"
                    }
                },
                {
                    "_index": "chenxuyou3",
                    "_type": "doc",
                    "_id": "fECECG8BAmmLmqjt7xyH",
                    "_score": 1,
                    "_source": {
                        "price": 5.6,
                        "studymodel": "201001",
                        "name": "spring cloud实战",
                        "description": "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。",
                        "timestamp": "2019-12-15 15:45:27"
                    }
                }
            ]
        }
    }

    结果说明:
    took:本次操作花费的时间,单位为毫秒。
    timed_out:请求是否超时
    _shards:说明本次操作共搜索了哪些分片
    hits:搜索命中的记录
    hits.total : 符合条件的文档总数 hits.hits :匹配度较高的前N个文档
    hits.max_score:文档匹配得分,这里为最高分
    _score:每个文档都有一个匹配度得分,按照降序排列。
    _source:显示了文档的原始内容。

    当然也可以对指定索引库进行查询

    http://localhost:9200/chenxuyou3/doc/_search
    
    post
    
    {
        "took": 2,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": 5,
            "max_score": 1,
            "hits": [
                {
                    "_index": "chenxuyou3",
                    "_type": "doc",
                    "_id": "fECECG8BAmmLmqjt7xyH",
                    "_score": 1,
                    "_source": {
                        "price": 5.6,
                        "studymodel": "201001",
                        "name": "spring cloud实战",
                        "description": "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。",
                        "timestamp": "2019-12-15 15:45:27"
                    }
                },
                {
                    "_index": "chenxuyou3",
                    "_type": "doc",
                    "_id": "fUCECG8BAmmLmqjt8xw3",
                    "_score": 1,
                    "_source": {
                        "price": 5.6,
                        "studymodel": "201001",
                        "name": "spring cloud实战",
                        "description": "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。",
                        "timestamp": "2019-12-15 15:45:28"
                    }
                },
                {
                    "_index": "chenxuyou3",
                    "_type": "doc",
                    "_id": "fkCECG8BAmmLmqjt-Bwr",
                    "_score": 1,
                    "_source": {
                        "price": 5.6,
                        "studymodel": "201001",
                        "name": "spring cloud实战",
                        "description": "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。",
                        "timestamp": "2019-12-15 15:45:29"
                    }
                },
                {
                    "_index": "chenxuyou3",
                    "_type": "doc",
                    "_id": "f0CECG8BAmmLmqjt-xxw",
                    "_score": 1,
                    "_source": {
                        "price": 5.6,
                        "studymodel": "201001",
                        "name": "spring cloud实战",
                        "description": "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。",
                        "timestamp": "2019-12-15 15:45:30"
                    }
                },
                {
                    "_index": "chenxuyou3",
                    "_type": "doc",
                    "_id": "ekBpCG8BAmmLmqjtgRwU",
                    "_score": 1,
                    "_source": {
                        "price": 5.6,
                        "studymodel": "201001",
                        "name": "Ealsticseach学习实战",
                        "description": "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。",
                        "timestamp": "2019-12-15 15:15:29"
                    }
                }
            ]
        }
    }

    现在采用客户端的aip进行操作,所以只需要在原来的基础上增阿加一个controller就好

    查询全部和分页查询:

     @RequestMapping(value = "/searchAlldoc", method = RequestMethod.GET)
        public String searchAlldoc() {
            //搜索请求对象
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //设置搜索方式
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            //设置包含哪些字段,不包括哪些字段,第一个参数,是包含的,第二个是不包含的
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"},new String[]{});
    
            //设置搜索源
            searchRequest.source(searchSourceBuilder);
            SearchResponse search =null;
            try {
                search = client.search(searchRequest, RequestOptions.DEFAULT);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            SearchHits hits = search.getHits();
            long totalHits = hits.totalHits;
            System.out.println(totalHits);
            SearchHit[] hits1 = hits.getHits();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit searchHit:hits1){
                String id = searchHit.getId();
                Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
               String s = (String) sourceAsMap.get("timestamp");
                Date timestamp =null;
                try {
                    timestamp = dateFormat.parse(s);
    
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(s);
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
    
            }
    
            return "ok";
        }
    
        @RequestMapping(value = "/searchAlldocPage", method = RequestMethod.GET)
        public String searchAlldocPage() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //设置分页参数
            //页码
            int page = 1;
            //每页记录数
            int size = 1;
            //计算出记录起始下标
            int from = (page - 1) * size;
            searchSourceBuilder.from(from);//起始记录下标,从0开始
            searchSourceBuilder.size(size);//每页显示的记录数
            //搜索方式
            //matchAllQuery搜索全部
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }

    精确查询

    Term Query为精确查询,在搜索时会整体匹配关键字,不再将关键字分词,本来es是进行分词插叙,这个词主要用作精确的,不进行分词的,

     @RequestMapping(value = "/termquerydocPage", method = RequestMethod.GET)
        public String termquerydocPage() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //设置分页参数
            //页码
            int page = 1;
            //每页记录数
            int size = 1;
            //计算出记录起始下标
            int from = (page - 1) * size;
            searchSourceBuilder.from(from);//起始记录下标,从0开始
            searchSourceBuilder.size(size);//每页显示的记录数
            //搜索方式
    
            //matchAllQuery搜索全部
            searchSourceBuilder.query(QueryBuilders.termQuery("name","spring"));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
        @RequestMapping(value = "/termqueryId", method = RequestMethod.GET)
        public String termqueryId() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            //定义id
            String[] ids = new String[]{"1","2"};
            searchSourceBuilder.query(QueryBuilders.termsQuery("_id",ids));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }

    控制台打印结果:

    38.6
    Bootstrap开发
    201002
    null
    Sun Mar 25 19:11:35 CST 2018
    68.6
    java编程基础
    201001
    null

    matchquery

      

    match Query即全文检索,它的搜索方式是先将搜索字符串分词,再使用各各词条从索引中搜索。

    match query与Term query区别是match query在搜索前先将搜索关键字分词,再拿各各词语去索引中搜索。

    postman 测试:

    http://localhost:9200/chenxuyou/doc/_search
    
    post
    第一种
    {
    "query": {    
        "match" : {
            "description" : {
                "query" : "spring开发",
                "operator" : "or"
            }
        }
      }
    }

    query:搜索的关键字,对于英文关键字如果有多个单词则中间要用半角逗号分隔,而对于中文关键字中间可以用 逗号分隔也可以不用。

    operator:or 表示 只要有一个词在文档中出现则就符合条件,and表示每个词都在文档中出现则才符合条件。 上边的搜索的执行过程是:

    1 、将“spring开发”分词,分为spring、开发两个词
    2、再使用spring和开发两个词去匹配索引中搜索。
    3、由于设置了operator为or,只要有一个词匹配成功则就返回该文档。


    第二中

    {
    "query": {    
        "match" : {
            "description" : {
                "query" : "spring开发框架",
                 "minimum_should_match": "80%"
            }
        }
      }
    }

    上边使用的operator = or表示只要有一个词匹配上就得分,如果实现三个词至少有两个词匹配如何实现?
    使用minimum_should_match可以指定文档匹配词的占比:
    比如搜索语句如下:

    “spring开发框架”会被分为三个词:spring、开发、框架
    设置"minimum_should_match": "80%"表示,三个词在文档的匹配占比为80%,即3*0.8=2.4,向上取整得2,表
    示至少有两个词在文档中要匹配成功。

     @RequestMapping(value = "/matchquery", method = RequestMethod.GET)
        public String matchquery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            //定义id
            searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开发框架")
                    .minimumShouldMatch("80%"));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
    
        @RequestMapping(value = "/matchqueryOr", method = RequestMethod.GET)
        public String matchqueryOr() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            
    //匹配关键字
    searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }

    multi Query

      

      上边的termQuery和matchQuery一次只能匹配一个Field,本节学习multiQuery,一次可以匹配多个字段。 1、基本使用 单项匹配是在一个field中去匹配,多项匹配是拿关键字去多个Field中匹配。

    http://localhost:9200/chenxuyou/doc/_search
    
    {
        "query": {
            "multi_match": {
                "query": "spring 开发框架",
                "minimum_should_match": "50%",
                "fields": [
                    "name",
                    "description"
                ]
            }
        }
    }
    结果:
    
    
    
    提高name的权重得分
    {
        "query": {
            "multi_match": {
                "query": "spring 开发框架",
                "minimum_should_match": "50%",
                "fields": [
                    "name^10",
                    "description"
                ]
            }
        }
    }

    这样会导致两次得分不一样,就会造成结果顺序不一样

    @RequestMapping(value = "/multiMatchQuery", method = RequestMethod.GET)
        public String multiMatchQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
    
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            searchSourceBuilder.query(QueryBuilders.multiMatchQuery("spring css","name","description")
                    .minimumShouldMatch("50%")
                    .field("name",10));
           // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }

    布尔查询:

       

      布尔查询对应于Lucene的BooleanQuery查询,实现将多个查询组合起来。 三个参数: must:文档必须匹配must所包括的查询条件,相当于 “AND” should:文档应该匹配should所包括的查询条件其 中的一个或多个,相当于 "OR" must_not:文档不能匹配must_not所包括的该查询条件,相当于“NOT”

      

    @RequestMapping(value = "/boolQuery", method = RequestMethod.GET)
        public String boolQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
    
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring css", "name", "description")
                    .minimumShouldMatch("50%")
                    .field("name", 10);
            //再定义一个termQuery
            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", "201001");
    
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(multiMatchQueryBuilder);
            boolQueryBuilder.must(termQueryBuilder);
    
            searchSourceBuilder.query(boolQueryBuilder);
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }

    过滤器:

      

    过虑是针对搜索的结果进行过虑,过虑器主要判断的是文档是否匹配,不去计算和判断文档的匹配度得分,所以过 虑器性能比查询要高,且方便缓存,推荐尽量使用过虑器去实现查询或者过虑器和查询共同使用。

    range:范围过虑,保留大于等于60 并且小于等于100的记录。 

    term :项匹配过虑,保留studymodel等于"201001"的记录。
    注意:range和term一次只能对一个Field设置范围过虑。

    使用:

    @RequestMapping(value = "/filterQuery", method = RequestMethod.GET)
        public String filterQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
    //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring css", "name", "description")
                    .minimumShouldMatch("50%")
                    .field("name", 10);
    
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(multiMatchQueryBuilder);
            //定义过虑器
            boolQueryBuilder.filter(QueryBuilders.termQuery("studymodel","201001"));
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(90).lte(100));
    
            searchSourceBuilder.query(boolQueryBuilder);
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }

    排序: 

      可以在字段上添加一个或多个排序,支持在keyword、date、float等类型上添加,text类型的字段上不允许添加排

    @RequestMapping(value = "/sortQuery", method = RequestMethod.GET)
        public String sortQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            //boolQuery搜索方式
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //定义过虑器
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
    
            searchSourceBuilder.query(boolQueryBuilder);
            //添加排序
            searchSourceBuilder.sort("studymodel", SortOrder.DESC);
            searchSourceBuilder.sort("price", SortOrder.ASC);
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }

    高亮:

      高亮显示可以将搜索结果一个或多个字突出显示,以便向用户展示匹配关键字的位置

    @RequestMapping(value = "/highlightQuery", method = RequestMethod.GET)
        public String highlightQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            //boolQuery搜索方式
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //定义过虑器
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
    
            searchSourceBuilder.query(boolQueryBuilder);
            //设置高亮
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.preTags("<tag>");
            highlightBuilder.postTags("</tag>");
            highlightBuilder.fields().add(new HighlightBuilder.Field("name"));
    //        highlightBuilder.fields().add(new HighlightBuilder.Field("description"));
            searchSourceBuilder.highlighter(highlightBuilder);
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }

    最后讲整个controller进行拿出来:

    package com.cxy.elasticsearch.controller;
    
    import org.elasticsearch.action.search.SearchRequest;
    import org.elasticsearch.action.search.SearchResponse;
    import org.elasticsearch.action.update.UpdateRequest;
    import org.elasticsearch.action.update.UpdateResponse;
    import org.elasticsearch.client.RequestOptions;
    import org.elasticsearch.client.RestClient;
    import org.elasticsearch.client.RestHighLevelClient;
    import org.elasticsearch.index.query.*;
    import org.elasticsearch.rest.RestStatus;
    import org.elasticsearch.search.SearchHit;
    import org.elasticsearch.search.SearchHits;
    import org.elasticsearch.search.builder.SearchSourceBuilder;
    import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
    import org.elasticsearch.search.sort.SortOrder;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    import java.io.IOException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
    
    @RestController
    public class EsdocController {
        @Autowired
        RestHighLevelClient client;
    
        @Autowired
        RestClient restClient;
    
        @RequestMapping(value = "/searchAlldoc", method = RequestMethod.GET)
        public String searchAlldoc() {
            //搜索请求对象
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //设置搜索方式
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            //设置包含哪些字段,不包括哪些字段,第一个参数,是包含的,第二个是不包含的
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"},new String[]{});
    
            //设置搜索源
            searchRequest.source(searchSourceBuilder);
            SearchResponse search =null;
            try {
                search = client.search(searchRequest, RequestOptions.DEFAULT);
    
            } catch (IOException e) {
                e.printStackTrace();
            }
            SearchHits hits = search.getHits();
            long totalHits = hits.totalHits;
            System.out.println(totalHits);
            SearchHit[] hits1 = hits.getHits();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit searchHit:hits1){
                String id = searchHit.getId();
                Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
               String s = (String) sourceAsMap.get("timestamp");
                Date timestamp =null;
                try {
                    timestamp = dateFormat.parse(s);
    
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(s);
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
    
            }
    
            return "ok";
        }
    
        @RequestMapping(value = "/searchAlldocPage", method = RequestMethod.GET)
        public String searchAlldocPage() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //设置分页参数
            //页码
            int page = 1;
            //每页记录数
            int size = 1;
            //计算出记录起始下标
            int from = (page - 1) * size;
            searchSourceBuilder.from(from);//起始记录下标,从0开始
            searchSourceBuilder.size(size);//每页显示的记录数
            //搜索方式
            //matchAllQuery搜索全部
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
    
        @RequestMapping(value = "/termquerydocPage", method = RequestMethod.GET)
        public String termquerydocPage() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //设置分页参数
            //页码
            int page = 1;
            //每页记录数
            int size = 1;
            //计算出记录起始下标
            int from = (page - 1) * size;
            searchSourceBuilder.from(from);//起始记录下标,从0开始
            searchSourceBuilder.size(size);//每页显示的记录数
            //搜索方式
    
            //matchAllQuery搜索全部
            searchSourceBuilder.query(QueryBuilders.termQuery("name","spring"));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
        @RequestMapping(value = "/termqueryId", method = RequestMethod.GET)
        public String termqueryId() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            //定义id
            String[] ids = new String[]{"1","2"};
            searchSourceBuilder.query(QueryBuilders.termsQuery("_id",ids));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
        @RequestMapping(value = "/matchquery", method = RequestMethod.GET)
        public String matchquery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            //定义id
            searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开发框架")
                    .minimumShouldMatch("80%"));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
    
        @RequestMapping(value = "/matchqueryOr", method = RequestMethod.GET)
        public String matchqueryOr() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
    
    //匹配关键字
    searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
        @RequestMapping(value = "/multiMatchQuery", method = RequestMethod.GET)
        public String multiMatchQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
    
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            searchSourceBuilder.query(QueryBuilders.multiMatchQuery("spring css","name","description")
                    .minimumShouldMatch("50%")
                    .field("name",10));
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
    
        @RequestMapping(value = "/boolQuery", method = RequestMethod.GET)
        public String boolQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
    
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring css", "name", "description")
                    .minimumShouldMatch("50%")
                    .field("name", 10);
            //再定义一个termQuery
            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", "201001");
    
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(multiMatchQueryBuilder);
            boolQueryBuilder.must(termQueryBuilder);
    
            searchSourceBuilder.query(boolQueryBuilder);
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
    
        @RequestMapping(value = "/filterQuery", method = RequestMethod.GET)
        public String filterQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
    //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring css", "name", "description")
                    .minimumShouldMatch("50%")
                    .field("name", 10);
    
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(multiMatchQueryBuilder);
            //定义过虑器
            boolQueryBuilder.filter(QueryBuilders.termQuery("studymodel","201001"));
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(90).lte(100));
    
            searchSourceBuilder.query(boolQueryBuilder);
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
    
        @RequestMapping(value = "/sortQuery", method = RequestMethod.GET)
        public String sortQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            //boolQuery搜索方式
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //定义过虑器
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
    
            searchSourceBuilder.query(boolQueryBuilder);
            //添加排序
            searchSourceBuilder.sort("studymodel", SortOrder.DESC);
            searchSourceBuilder.sort("price", SortOrder.ASC);
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
        @RequestMapping(value = "/highlightQuery", method = RequestMethod.GET)
        public String highlightQuery() {
            SearchRequest searchRequest = new SearchRequest("chenxuyou");
            //指定类型
            searchRequest.types("doc");
            //搜索源构建对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //搜索方式
            //根据id查询
            //boolQuery搜索方式
            //定义一个boolQuery
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            //定义过虑器
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
    
            searchSourceBuilder.query(boolQueryBuilder);
            //设置高亮
            HighlightBuilder highlightBuilder = new HighlightBuilder();
            highlightBuilder.preTags("<tag>");
            highlightBuilder.postTags("</tag>");
            highlightBuilder.fields().add(new HighlightBuilder.Field("name"));
    //        highlightBuilder.fields().add(new HighlightBuilder.Field("description"));
            searchSourceBuilder.highlighter(highlightBuilder);
    //匹配关键字
            //搜索方式
            //MultiMatchQuery
            //boolQuery搜索方式
            //先定义一个MultiMatchQuery
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","timestamp"},new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            // searchSourceBuilder.query(QueryBuilders.matchQuery("description","spring开 发").operator(Operator.OR));
            //设置源字段过虑,第一个参数结果集包括哪些字段,第二个参数表示结果集不包括哪些字段
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel", "price", "timestamp"}, new String[]{});
            //向搜索请求对象中设置搜索源
            searchRequest.source(searchSourceBuilder);
            //执行搜索,向ES发起http请求
            SearchResponse searchResponse = null;
            try {
                searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
            } catch (IOException e) {
                e.printStackTrace();
            }
            //搜索结果
            SearchHits hits = searchResponse.getHits();
            //匹配到的总记录数
            long totalHits = hits.getTotalHits();
            //得到匹配度高的文档
            SearchHit[] searchHits = hits.getHits();
            //日期格式化对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            for (SearchHit hit : searchHits) {
                //文档的主键
                String id = hit.getId();
                //源文档内容
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                //由于前边设置了源文档字段过虑,这时description是取不到的
                String description = (String) sourceAsMap.get("description");
                //学习模式
                String studymodel = (String) sourceAsMap.get("studymodel");
                //价格
                Double price = (Double) sourceAsMap.get("price");
                //日期
                try {
                    Date timestamp = dateFormat.parse((String) sourceAsMap.get("timestamp"));
                    System.out.println(timestamp);
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                System.out.println(price);
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
            return "ok";
        }
    }

      

  • 相关阅读:
    奢想下财务自由吧--理想生活啊
    [翻译] AGG 之贝塞尔插值
    Beginning Silverlight 4 in C#Welcome to Silverlight 4[学习笔记]
    从属性赋值到MVVM模式详解
    Beginning Silverlight 4 in C#Silverlight的布局管理学习笔记
    Beginning Silverlight 4 in C#Silverlight工具包
    不用IDE写C#的Hello World
    NHibernate使用无状态Sessions
    Beginning Silverlight 4 in C#Silverlight控件
    NHibernate使用session.Merge[翻译]
  • 原文地址:https://www.cnblogs.com/cxyxiaobao/p/12045355.html
Copyright © 2020-2023  润新知