SpringBoot3.0集成Elasticsearch,@Field没有为字段创建分词器的解决方法

  1. 创建实体类
@Document(indexName = "moon_blog", createIndex = true) // 默认createIndex为true,这个值要为true
public class ESArticleDto {
    @Id
    private String id;

    @Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_max_word") // 配置你的分词器,字段类型必须为Text,不配置searchAnalyzer 默认使用analyzer 
    private String articleTitle;

    @Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_max_word")
    private String articleContent;

    @Field(type = FieldType.Boolean)
    private Boolean deleted;

    @Field(type = FieldType.Long)
    private Long articleId;

    @Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_max_word")
    private String articleSummary;


    public ESArticleDto() {}

    @PersistenceCreator
    public ESArticleDto(
            String id, String articleTitle,
            String articleContent,
            Boolean deleted, Long articleId,
            String articleSummary) {
        this.id = id;
        this.articleTitle = articleTitle;
        this.articleContent = articleContent;
        this.deleted = deleted;
        this.articleId = articleId;
        this.articleSummary = articleSummary;
    }

    ...getter
    ...setter
}

  1. 创建Repository(重要)
@Repository
public interface ES extends ElasticsearchRepository<ESArticleDto, String> {
}
  1. 启动应用,可以查询到已经配置上了分词器
{
  "moon_blog": {
    "aliases": {},
    "mappings": {
      "properties": {
        "_class": {
          "type": "keyword",
          "index": false,
          "doc_values": false
        },
        "articleContent": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart"
        },
        "articleId": {
          "type": "long"
        },
        "articleSummary": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart"
        },
        "articleTitle": {
          "type": "text",
          "analyzer": "ik_max_word",
          "search_analyzer": "ik_smart"
        },
        "deleted": {
          "type": "boolean"
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "refresh_interval": "1s",
        "number_of_shards": "1",
        "provided_name": "moon_blog",
        "creation_date": "1676942756292",
        "number_of_replicas": "1",
        "uuid": "xh4Y5X5qRyidGrH8TE6o9w",
        "version": {
          "created": "8060199"
        }
      }
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值