Elasticsearch spring boot 指定拼音分词器

1下载ik中文/拼音分词器

ik分词器:https://github.com/medcl/elasticsearch-analysis-ik

拼音分词器:https://github.com/medcl/elasticsearch-analysis-pinyin

注意:elasticsearch版本要求严格必须相同

2 安装

1)通过releases找到和es对应版本的zip文件,或者source文件

2)进入elasticsearch安装目录plugins,新建pinyin文件夹

3)将拼音分词器zip文件解压到pinyin目录

4)重启es

3 kibana中配置

1)配置setting

PUT  my_index
 {
        "number_of_shards" : "5",//主分片
        "number_of_replicas" : "1",//副本
        "analysis" : {
          "analyzer" : {
            "default" : {
              "tokenizer" : "ik_max_word"//默认多词分词
            },
            "pinyin_analyzer" : {
              "tokenizer" : "my_pinyin"//拼音分词
            }
          },
          "tokenizer" : {
            //设置拼音分词
            "my_pinyin" : {
              "keep_separate_first_letter" : "false",
              "lowercase" : "true",
              "type" : "pinyin",
              "limit_first_letter_length" : "16",
              "keep_original" : "false",
              "keep_full_pinyin" : "true"
            }
          }
        }
      }

2)配置mapping

PUT my_index/index/_mapping
{    
        "properties" : {
            "name" : {
                "type" : "keyword",
                "analyzer" : "ik_max_word",
                "include_in_all" : true,
                "fields" : {
                    "pinyin" : {
                        "type" : "text",
                        "analyzer" : "pinyin_analyzer"
                      }
                 }
            }
      }
}

4 测试

通过_analyze测试下分词器是否能正常运行:

GET my_index/_analyze
{
"text":"刘德华",
"analyzer":"pinyin_analyzer"
}

5 spring boot 中自动创建setting mapping

1)在resources路径下创建usersearch_mapping.json和usersearch_setting.json文件

usersearch_mapping.json
{ "index" : { "analysis" : { "analyzer" : { "pinyin_analyzer" : { "tokenizer" : "my_pinyin" } }, "tokenizer" : { "my_pinyin" : { "type" : "pinyin", "keep_separate_first_letter" : false, "keep_full_pinyin" : true, "keep_original" : true, "limit_first_letter_length" : 16, "lowercase" : true, "remove_duplicated_term" : true } } } } }
usersearch_setting.json

{
  "user": {
    "properties": {
      "title": {
        "type": "keyword",
        "fields": {
          "pinyin": {
            "type": "text",
            "store": "no",
            "term_vector": "with_offsets",
            "analyzer": "pinyin_analyzer"
          }
        }
      }
    }
  }
}

2)新建测试demo 使用@Mapping和@Setting注解

@Mapping(mappingPath = "usersearch_setting.json")
@Setting(settingPath = "usersearch_mapping.json")
@Document(indexName = "user",type = "user",shards = 5,replicas = 1)
public class UserIndex  {
    @Id
    private  String user;
//get set省略
}

3)使用save方法添加数据

使用ElasticsearchTemplate 中的putMapping将setting 和mapping文件执行

public class UserContrller {
    @Autowired
    private UserRepository userRepository;
    @Autowired
    private ElasticsearchTemplate elasticsearchTemplate;
    @RequestMapping("/add")
    public void add(){
        //添加配置
        elasticsearchTemplate.putMapping(User.class);
        User user =new User();
        userIndex.setUser("陈奕迅");
        userRepository.save(user);
    }
}
原文地址:https://www.cnblogs.com/xiaoxiaoliu/p/9715014.html