最近个人网站重构,将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里有了预期的分词器设置。
热门评论
- 
			
				码痴2018-06-24 1 
 
 
- 
			
				JunJeff2020-10-15 0 
  
 
 
查看全部评论@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Documented @Inheritedpublic @interface Field { FieldType type() default FieldType.Auto;#自动检测属性的类型 FieldIndex index() default FieldIndex.analyzed;#默认情况下分词 }你都指定index为false了 你还怎么用分词器?