Sringboot操作ElasticSearch全文搜索引擎简单实现

一、在建好项目之后,导入相应的基础依赖 - pom.xml文件

 <!--SpringBoot-->
    <parent>
        <groupId> org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.5.RELEASE</version>
    </parent>
 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

二、去配置application.yml(端口号默认9200,不做更改的话可以不配置)

spring:
  elasticsearch:
    rest:
      uris: http://localhost:9200

三、配置配置类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class ESApplication {
    public static void main(String[] args) {
        SpringApplication.run(ESApplication.class, args);
    }
}

四丶编写一个document对象

/**
 * @Document标记该对象是ES的文档对象
 * indexName 索引库
 * type 类型
 */
@Document(indexName = "order",type = "_doc")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class OrderDoc {
    /**
     * 标记为文档ID,该ID的值会作为document的id值
     */
    @Id
    private Long id;
    /**
     * Text需要分词
     * 按ik_max_word分词建立倒排索引
     * 查询按ik_max_word查询
     */
    @Field(type = FieldType.Text, analyzer = "ik_max_word", searchAnalyzer = "ik_max_word")
    private String goods;
 
    @Field(type = FieldType.Integer)
    private Integer count;
 
    @Field(type = FieldType.Date)
    @DateTimeFormat
    private Date createTime;
    /**
     * ES没有BigDecimal数据类型用Doubl
     */
    @Field(type = FieldType.Double)
    private BigDecimal price;
}

五丶实现ElasticsearchRepository接口(ElasticsearchRepository<文档对象,文档对象中id的类型>)

@Repository
public interface OrderRepository extends ElasticsearchRepository<OrderDoc,Long> {
}

六丶测试类

    @Autowired
    private ElasticsearchRestTemplate template;
    
    @Autowired
    private OrderRepository orderRepository;
 
 
  @Test
    public void test(){
           //创建索引
        template.createIndex(OrderDoc.class);
        //创建映射
        template.putMapping(OrderDoc.class);
    }
  @Test
    public void testAdd() {
        orderRepository.save(new OrderDoc(1L,"真爱坤",
        10,new Date(),new BigDecimal("100.00")));
    }
    @Test
    public void testGet() {
        System.out.println(orderRepository.findById(1L));
    }
 
    /**
     * 修改和添加是同一个方法,Id存在就修改
     */
    @Test
    public void testUpdate() {
        orderRepository.save(new OrderDoc(1L,"小黑子",10,
        new Date(),new BigDecimal("250.00")));
    }
    @Test
    public void testDelete() {
        orderRepository.deleteById(1L);
    }

如果对您有所帮助,鄙人不胜荣幸!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值