ITPub博客

首页 > IT职业 > IT图书 > spring-boot-data-elasticsearch中,Fie

spring-boot-data-elasticsearch中,Fie

IT图书 作者:mmxnj 时间:2021-09-09 09:12:24 0 删除 编辑

最近个人网站重构,将SpringBoot版本升到了SpringBoot2.0。期间遇到了一些问题。其中一个就是在实体层中用@Field注解配置ik分词器无效。

具体配置如下:

@Field(index = false, 
searchAnalyzer = "ik_smart", 
analyzer = "ik_smart")

通过查询ES生成的Mapping发现,searchAnalyzer和analyzer均未生成ik_smart分词器。

于是各种Google与StackOverflow,一直没有找到好的解决方案。

最后在SpringBoot Data Elasticsearch的官方文档中找到了可以通过@Mapping注解来自定义生成Mapping。

修改后的实体部分代码如下:

@Mapping(mappingPath = "articlesearch_mapping.json")
public class ArticleSearch {
    @Id
    private Long id;
    // 其它属性...
}

在resources目录下,新建articlesearch_mapping.json。内容如下:

{
  "article": {
    "properties": {
      "id": {
        "type": "long"
      },
      "publishTime": {
        "type": "long"
      },
      "tags": {
        "type": "text",
        "analyzer": "ik_smart",
        "search_analyzer": "ik_smart",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "text": {
        "type": "text",
        "analyzer": "ik_smart",
        "search_analyzer": "ik_smart",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "title": {
        "type": "text",
        "analyzer": "ik_smart",
        "search_analyzer": "ik_smart",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

删除ES的Mapping后再次运行Java程序生成Mapping,发现Mapping里有了预期的分词器设置。

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/3316/viewspace-2808629/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-08-14

  • 博文量
    67
  • 访问量
    652