欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ElasticSearch RestClient索引管理

程序员文章站 2022-07-05 14:23:07
...
	@SpringBootTest
    @RunWith(SpringRunner.class)
    public class Searchtest {
    
        @Autowired
        RestHighLevelClient client;
    
        @Autowired
        RestClient restClient;
    
        @Test
        public void testCreatIndex() throws IOException {
            //创建索引请求对象,并设置索引名称
            CreateIndexRequest createIndexRequest = new CreateIndexRequest("xc_course5");
            //设置索引参数
            createIndexRequest.settings(Settings.builder().put("number_of_shards", 1)
                    .put("number_of_replicas", 0));
            //设置映射
            createIndexRequest.mapping("doc", " {\n" +
                    " \t\"properties\": {\n" +
                    " \"name\": {\n" +
                    " \"type\": \"text\",\n" +
                    " \"analyzer\":\"ik_max_word\",\n" +
                    " \"search_analyzer\":\"ik_smart\"\n" +
                    " },\n" +
                    " \"description\": {\n" +
                    " \"type\": \"text\",\n" +
                    " \"analyzer\":\"ik_max_word\",\n" +
                    " \"search_analyzer\":\"ik_smart\"\n" +
                    " },\n" +
                    " \"studymodel\": {\n" +
                    " \"type\": \"keyword\"\n" +
                    " },\n" +
                    " \"price\": {\n" +
                    " \"type\": \"float\"\n" +
                    " }\n" +
                    " }\n" +
                    "}", XContentType.JSON);
    
            //创建索引操作客户端
            IndicesClient indices = client.indices();
            //创建响应对象
            CreateIndexResponse createIndexResponse = indices.create(createIndexRequest);
            //得到响应结果
            boolean acknowledged = createIndexResponse.isAcknowledged();
            System.out.println(acknowledged);
    
        }
    
        //删除索引库
        @Test
        public void deleteIndex() throws IOException {
            //删除索引请求对象
            DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("xc_course5");
            //删除索引
            DeleteIndexResponse deleteIndexResponse = client.indices().delete(deleteIndexRequest);
            //删除索引响应结果
            boolean acknowledged = deleteIndexResponse.isAcknowledged();
            System.out.println(acknowledged);
        }
    
        //添加文档
        @Test
        public void testAddDoc() throws IOException {
            //准备json数据
            Map<String, Object> jsonMap = new HashMap<>();
            jsonMap.put("name", "spring cloud实战");
            jsonMap.put("description", "本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud基础入门 3.实战Spring Boot 4.注册中心eureka。");
            jsonMap.put("studymodel", "201001");
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy‐MM‐dd HH:mm:ss");
            jsonMap.put("timestamp", dateFormat.format(new Date()));
            jsonMap.put("price", 5.6f);
            //索引请求对象
            IndexRequest indexRequest = new IndexRequest("index_doc1", "doc");
            //指定索引文档内容
            indexRequest.source(jsonMap);
            //索引响应对象
            IndexResponse indexResponse = client.index(indexRequest);
            //获取响应结果
            DocWriteResponse.Result result = indexResponse.getResult();
            System.out.println(result);
        }
    
        //查询文档
        @Test
        public void getDoc() throws IOException {
            GetRequest getRequest = new GetRequest(
                    "index_doc1",
                    "doc",
                    "vS8sQm8BP3ucAP7jZX-r");
            GetResponse getResponse = client.get(getRequest);
            boolean exists = getResponse.isExists();
            Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
            System.out.println(sourceAsMap);
        }
    
        //更新文档
        @Test
        public void updateDoc() throws IOException {
            UpdateRequest updateRequest = new UpdateRequest("index_doc1", "doc",
                    "vS8sQm8BP3ucAP7jZX-r");
            Map<String, String> map = new HashMap<>();
            map.put("name", "spring cloud吃饭");
            updateRequest.doc(map);
            UpdateResponse update = client.update(updateRequest);
            RestStatus status = update.status();
            System.out.println(status);
        }
    
        //根据id删除文档
        @Test
        public void testDelDoc() throws IOException {
            //删除文档id
            String id = "vS8sQm8BP3ucAP7jZX-r";
            //删除索引请求对象
            DeleteRequest deleteRequest = new DeleteRequest("index_doc1", "doc", id);
            //响应对象
            DeleteResponse deleteResponse = client.delete(deleteRequest);
            //获取响应结果
            DocWriteResponse.Result result = deleteResponse.getResult();
            System.out.println(result);
        }
    
        //搜索type下的全部记录
        @Test
        public void testSearchAll() throws IOException {
            SearchRequest searchRequest = new SearchRequest("xc_course");
            searchRequest.types("doc");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            //source源字段过虑
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel"}, new String[]{});
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                String studymodel = (String) sourceAsMap.get("studymodel");
                String description = (String) sourceAsMap.get("description");
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
        }
    
        //分页查询
        @Test
        public void pageSearch() throws IOException {
            SearchRequest searchRequest = new SearchRequest("xc_course");
            searchRequest.types("doc");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.matchAllQuery());
            //分页查询,设置起始下标,从0开始
            searchSourceBuilder.from(0);
            //每页显示个数
            searchSourceBuilder.size(3);
            //source源字段过虑
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel"}, new String[]{});
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            for (SearchHit hit : hits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                String studymodel = (String) sourceAsMap.get("studymodel");
                String description = (String) sourceAsMap.get("description");
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
        }
    
        //精确查询
        @Test
        public void termSearch() throws IOException {
            SearchRequest searchRequest = new SearchRequest("xc_course");
            searchRequest.types("xc_course");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.query(QueryBuilders.termQuery("name", "spring"));
            //source源字段过虑
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel"}, new String[]{});
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            for (SearchHit hit : hits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                String studymodel = (String) sourceAsMap.get("studymodel");
                String description = (String) sourceAsMap.get("description");
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
        }
    
        //根据id精确查询
        @Test
        public void FindById() throws IOException {
            SearchRequest searchRequest = new SearchRequest("xc_course");
            searchRequest.types("xc_course");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            String[] split = new String[]{"1", "2"};
            List<String> idList = Arrays.asList(split);
            searchSourceBuilder.query(QueryBuilders.termsQuery("_id", idList));
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            for (SearchHit hit : hits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                String studymodel = (String) sourceAsMap.get("studymodel");
                String description = (String) sourceAsMap.get("description");
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
        }
    
        //根据关键字搜索
        @Test
        public void testMatchQuery() throws IOException {
            SearchRequest searchRequest = new SearchRequest("xc_course");
            searchRequest.types("xc_course");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //source源字段过虑
            searchSourceBuilder.fetchSource(new String[]{"name", "studymodel"}, new String[]{});
            //匹配关键字
            searchSourceBuilder.query(QueryBuilders.matchQuery("description", "spring开发").operator(Operator.OR));
            searchRequest.source(searchSourceBuilder);
            SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                String studymodel = (String) sourceAsMap.get("studymodel");
                String description = (String) sourceAsMap.get("description");
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
        }
    
        /*2、minimum_should_match
                上边使用的operator = or表示只要有一个词匹配上就得分,如果实现三个词至少有两个词匹配如何实现?
        使用minimum_should_match可以指定文档匹配词的占比
        “spring开发框架”会被分为三个词:spring、开发、框架
            设置"minimum_should_match": "80%"表示,三个词在文档的匹配占比为80%,即3*0.8=2.4,向上取整得2,表
            示至少有两个词在文档中要匹配成功
    
            Client代码
            //匹配关键字
            MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("description", "前台页面开发框架 架
            构")
            .minimumShouldMatch("80%");//设置匹配占比
            searchSourceBuilder.query(matchQueryBuilder);
    
        */
        //BoolQuery,将搜索关键字分词,拿分词去索引库搜索
            @Test
        public void testBoolQuery() throws IOException {
            //创建搜索请求对象
            SearchRequest searchRequest = new SearchRequest("xc_course");
            searchRequest.types("doc");
            //创建搜索源配置对象
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            searchSourceBuilder.fetchSource(new String[]{"name", "pic", "studymodel"}, new String[]{});
            //multiQuery
            String keyword = "spring开发框架";
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("spring框架",
                    "name", "description")
                    .minimumShouldMatch("50%");
            multiMatchQueryBuilder.field("name", 10);
            //TermQuery
            TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("studymodel", "201001");
            //布尔查询
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(multiMatchQueryBuilder);
            boolQueryBuilder.must(termQueryBuilder);
            //设置布尔查询对象
            searchSourceBuilder.query(boolQueryBuilder);
            searchRequest.source(searchSourceBuilder);//设置搜索源配置
            SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                System.out.println(sourceAsMap);
            }
        }
        //排序查询
        @Test
        public void testSort() throws IOException {
            SearchRequest searchRequest = new SearchRequest("xc_course");
            searchRequest.types("doc");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //source源字段过虑
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","description"},
                    new String[]{});
            searchRequest.source(searchSourceBuilder);
        //布尔查询
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        //过虑
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
            //排序
           /* searchSourceBuilder.sort(new FieldSortBuilder("studymodel").order(SortOrder.DESC));*/
           searchSourceBuilder.sort(new FieldSortBuilder("price").order(SortOrder.ASC));
            SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
                String name = (String) sourceAsMap.get("name");
                String studymodel = (String) sourceAsMap.get("studymodel");
                String description = (String) sourceAsMap.get("description");
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
        }
        //高亮显示
        @Test
        public void testHighlight() throws IOException {
            SearchRequest searchRequest = new SearchRequest("xc_course");
            searchRequest.types("doc");
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //source源字段过虑
            searchSourceBuilder.fetchSource(new String[]{"name","studymodel","price","description"},
                    new String[]{});
            searchRequest.source(searchSourceBuilder);
            //匹配关键字
            MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery("开发","name", "description");
            searchSourceBuilder.query(multiMatchQueryBuilder);
            //布尔查询
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
            boolQueryBuilder.must(searchSourceBuilder.query());
            //过虑
            boolQueryBuilder.filter(QueryBuilders.rangeQuery("price").gte(0).lte(100));
            //排序
           /* searchSourceBuilder.sort(new FieldSortBuilder("studymodel").order(SortOrder.DESC));*/
            searchSourceBuilder.sort(new FieldSortBuilder("price").order(SortOrder.ASC));
            //高亮设置
            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);
            SearchResponse searchResponse = client.search(searchRequest);
            SearchHits hits = searchResponse.getHits();
            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            //名称
                String name = (String) sourceAsMap.get("name");
            //取出高亮字段内容
                Map<String, HighlightField> highlightFields = hit.getHighlightFields();
                if(highlightFields!=null){
                    HighlightField nameField = highlightFields.get("name");
                    if(nameField!=null){
                        Text[] fragments = nameField.getFragments();
                        StringBuffer stringBuffer = new StringBuffer();
                        for (Text str : fragments) {
                            stringBuffer.append(str.string());
                        }
                        name = stringBuffer.toString();
                    }
                }
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                String studymodel = (String) sourceAsMap.get("studymodel");
                String description = (String) sourceAsMap.get("description");
                System.out.println(name);
                System.out.println(studymodel);
                System.out.println(description);
            }
        }
    
    
    }