关于spring-data-elasticsearch使用出现的一些小问题

前言:

今天使用了spring-data-elasticsearch,使用之前看了一部分文档,感觉应该不会出现什么问题,但是就是出现了不少的小问题,花费我了我不少时间,写一篇博客记录下来,以访以后遇到,也希望对看到这篇博客的人有所小小的帮助!

问题一

failed to load elasticsearch nodes : org.elasticsearch.index.mapper.MapperParsingException: No type specified for field【属性名称]
出现这个问题,我就想到了用solr时需要再solr的配置文件中配置自己的字段,难道是因为我没有配置?最后看了许多文章,自己又测试了许久,并不是,但又有一点关系!
解决:

  • 1、查看是否在对应的pojo中写上了@Document(indexName = “test”,type = “article”)
    即indexName,与type不能缺少,如下
/**
 * @author GXM
 * @date 2019/1/24
 */
@Document(indexName = "test",type = "article")
public class Article implements Serializable {
  • 2、在pojo的实体类属性把@Field去掉,如下(关于为什么去掉,和我有需求必须要加上该属性,比如该字段我需要分词,必须配置,看第3种方法)
/* @Field(index = true,analyzer="ik_max_word",searchAnalyzer="ik_max_word")    */
private String content;
  • 3、必须需要@Field属性,因为某些字段需要分词,在@field属性中再加上一个字段(type ),表明自己定义的该字段对应elasticsearch已近定义好的字段类型(出现该错误主要是由于该问题),如下
@Field(index = true,type = FieldType.text,analyzer="ik_max_word",searchAnalyzer="ik_max_word")
private String content;

对于其它的java类型,使用枚举FieldType中的属性一一对应即可,源码如下:

public enum FieldType {
    text,
    Integer,
    Long,
    Date,
    Float,
    Double,
    Boolean,
    Object,
    Auto,
    Nested,
    Ip,
    Attachment,
    keyword;

    private FieldType() {
    }
}

问题二

Unsupported Media Type 415
吐槽
说实话这个问题我真是没想到(因为之前都用的好好的。。)
原因:因为Content-type格式不正确,我后台接收用的是json接收,我这里是用的Google插件种的 postman,发送的时候用的是json格式数据,但是这个插件发送json的content-type不是palnt/text,而不是application/json
解决
更改content-type的格式为application/json

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
使用 Spring Boot Starter Data Elasticsearch 可以轻松地在 Spring Boot 应用中集成 Elasticsearch。以下是使用步骤: 1. 添加依赖:在你的 pom.xml 文件中添加 Spring Boot Starter Data Elasticsearch 依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> ``` 2. 配置连接:在应用的配置文件中,配置 Elasticsearch 的连接信息。例如,在 application.properties 文件中添加以下配置: ```properties spring.data.elasticsearch.cluster-nodes=localhost:9200 ``` 3. 创建实体类:创建一个与 Elasticsearch 索引文档对应的实体类,并使用注解进行映射。例如: ```java import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; @Document(indexName = "myindex", type = "mytype") public class MyDocument { @Id private String id; private String name; // Getters and setters } ``` 在上面的示例中,`@Document` 注解用于指定索引名称和类型,`@Id` 注解表示文档的唯一标识。 4. 创建仓库接口:创建一个继承自 ElasticsearchRepository 的接口,用于定义与 Elasticsearch 进行交互的方法。例如: ```java import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; public interface MyDocumentRepository extends ElasticsearchRepository<MyDocument, String> { List<MyDocument> findByName(String name); } ``` 在上面的示例中,`ElasticsearchRepository` 提供了一些基本的 CRUD 操作,你还可以定义自己的查询方法。 5. 使用仓库接口:在需要使用 Elasticsearch 的地方,通过注入仓库接口来进行操作。例如,在一个服务类中注入 `MyDocumentRepository` 并使用它执行查询: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class MyService { @Autowired private MyDocumentRepository repository; public List<MyDocument> searchByName(String name) { return repository.findByName(name); } } ``` 以上就是使用 Spring Boot Starter Data Elasticsearch 的基本步骤。你可以根据自己的需求,进行更多的配置和操作,包括自定义查询、分页等。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值