Elasticsearch Java入门

本文详细介绍了如何安装Elasticsearch,包括选择适合SpringBoot版本的Elasticsearch、创建Windows服务、安装Kibana并验证服务状态。接着讨论了Elasticsearch的默认分词器问题,展示了标准分词器对中文的处理,并介绍了IK分词器的安装与使用。最后,文章展示了如何在SpringBoot2.2.6版本中集成Elasticsearch6.8.6,包括配置、实体类、Repository和测试类的创建。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Elasticsearch安装

在这里插入图片描述

官网下载
下载的时候注意版本,spring boot如果用的是2.2+版本就下载6.8+的版本就行

下载完成之后解压,运行bin->elasticsearch.bat就可以启动服务了

做出win服务

elasticsearch-service.bat install

在这里插入图片描述

浏览器输入localhost:9200
在这里插入图片描述

Kibana安装

官网下载
下载完成之后解压,运行kibana.bat就可以启动服务了

在这里插入图片描述
浏览器输入http://localhost:5601就可以访问了
在这里插入图片描述
输入GET /_cluster/health可以查看es服务信息

分词器安装

es默认是用的standard分词器,对英文很友好,但是对中文并不友好,我们来看下

GET /_analyze
{
  "analyzer": "standard", 
  "text": "i love you"
}

在这里插入图片描述
英文是可以分词的,我们再来看下中文

GET /_analyze
{
  "analyzer": "standard", 
  "text": "我 爱 中华 人民 共和国"
}

在这里插入图片描述
可以看到中国这个词并没有分出来,所以我们需要安装中文分词器,现在用的最多的就是ik

下载地址
github下载很慢慢的,可以去我分享的百度网盘里面直接安装
在es的plugins文件下新建ik文件,把刚刚下载好的解压缩到当前文件下就可以了

在这里插入图片描述
然后重启es服务就可以了

ik有2中分词器模式

  • ik_smart:按最粗粒度分词
  • ik_max_word:按最细粒度分词

我们先看下ik_smart

GET /_analyze
{
  "analyzer": "ik_smart", 
  "text": "我爱中华人民共和国"
}

在这里插入图片描述
他把中华人民共和国当成一个粗粒度的词,并没有再往下细分,一共只分出了3个词

再来看下ik_max_word

在这里插入图片描述
这里可以看到一共分了10个词,按最细粒度分的

spring boot 集成

注意spring boot 的版本是2.2.6,es的版本是6.8.6

pom

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

新建配置类

@Configuration
public class ElasticSearchClientConfig extends AbstractElasticsearchConfiguration {
    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
                .connectedTo("127.0.0.1:9200")
                .build();
        return RestClients.create(clientConfiguration).rest();
    }
}

新建实体类

@Data
@Document(indexName = "products", createIndex = true)
public class Product {

    /**
     * id
     */
    @Id
    private Integer id;
    /**
     * 名称
     */
    @Field(type = FieldType.Keyword)
    private String title;

    /**
     * 价格
     */
    @Field(type = FieldType.Float)
    private Double price;
    
    /**
     * 描述
     */
    @Field(type = FieldType.Text)
    private String description;
}

新建Repository

@Repository
public interface ProductRepository extends ElasticsearchRepository<Product,Integer> {

}

新建测试类

里面有添加,修改,删除,根据id查询,查询所有方法

@SpringBootTest
class DemoApplicationTests {

    @Autowired
    private ProductRepository productRepository;

    @Test
    public void testCreate() throws IOException {
        Product product = new Product();
        product.setId(3);
        product.setTitle("恒大冰泉");
        product.setPrice(33.11);
        product.setDescription("恒大冰泉很好喝的水");
        productRepository.save(product);
    }

    @Test
    public void testDelete(){
        Product product = new Product();
        product.setId(2);
        productRepository.delete(product);
    }

    @Test
    public void testUpdate(){
        Product product = new Product();
        product.setId(3);
        product.setTitle("恒大冰泉");
        product.setPrice(33.33);
        product.setDescription("恒大冰泉是许家印的水");
        productRepository.save(product);
    }

    @Test
    public void testGetById(){
        Optional<Product> product = productRepository.findById(3);
        System.out.println(product.toString());
    }

    @Test
    public void testGetAll(){
        Iterable<Product> products = productRepository.findAll();
        products.forEach(p->{
            System.out.println(p.toString());
        });
    }
}

查看测试结果
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

珍妮玛•黛金

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值