栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

SpringBoot项目集成全文搜索引擎Elasticsearch

SpringBoot项目集成全文搜索引擎Elasticsearch

 

首先安装 Elasticsearch和Kibana

Download Elasticsearch | Elastic       Download Kibana Free | Get Started Now | Elastic

导入相关maven依赖

	org.springframework.boot
	spring-boot-starter-data-elasticsearch
yml中配置相关参数
spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch
      cluster-nodes: 127.0.0.1:9300 #9200是图形界面端,9300代码端
创建文档对象

该文档对象可用来做如下几个事情

  • 索引库的创建
  • 文档的映射
  • 存储到ES的数据封装
    @document(indexName = "stu" , type = "student")
    public class StudentDoc {
    
        //对应文档的id  PUT  /index/type/id
        @Id
        private Long id;
    
        //指定为 不分词
        @Field(type = FieldType.Keyword)    
        private String userName;
    
        private int age;
    
        //指定为 分词
        @Field(type =FieldType.Text,analyzer = "ik_max_word",searchAnalyzer = "ik_max_word")
        private String intro;
       
    初始化索引库和文档映射
    @RunWith(SpringRunner.class)
    @SpringBootTest(classes = EsServiceApplication.class)
    public class ElasticsearchTest {
    
        @Autowired
        private ElasticsearchTemplate elasticsearchTemplate;
    
        @Test
        public void testCreateIndex() {
            //创建索引
            elasticsearchTemplate.createIndex(StudentDoc.class);
            //做文档映射
            elasticsearchTemplate.putMapping(StudentDoc.class);
        }
    }

    到此为止基础的集成就已经完成,可以通过Kibana测试索引是否创建成功。

  • 转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/728606.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号