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);
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM