Elasticsearch-使用IK分词器实现搜索

使用IK分词器实现搜索

安装IK分词器

    下载 选择的ik分词器版本一定要与你的版本一致,否则就无法启动es,比如我用的是6.1.4的es,那么ik分词器也一定要6.1.4 下载到elasticsearch/plugin目录下
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.1.4/elasticsearch-analysis-ik-6.1.4.zip

 unzip elasticsearch-analysis-ik-6.1.4.zip

然后重启es就可以了

配置实体类

@Data
@Document(indexName = "lixue",type = "problem")
public class Problem implements Serializable {
          
   
   //题目id
   @Id
   private String problemId;
   private String knowledgePoint;
   //题目标题
   @Field(type = FieldType.Text, searchAnalyzer = "ik_smart", analyzer = "ik_max_word")
   private String title;
   //出版社
   private String publish;
   //题目图片名字
   private String problemPictureName;
   //答案图片名字
   private String answerPictureName;
   //热度
   private int hotPoint;
}

这里ik_max_word是按最细粒度分词,ik_smart是按最粗粒度分词。 在搜索的时候用粗粒度,在写入的时候用细粒度。

搜索

@Override
public List<Problem> searchProblemByString(String title) {
          
   
  List list=new ArrayList();
  problemRepository.search(new MatchQueryBuilder("title",title)).forEach(problem -> list.add(problem));
  return list;
}

测试

经验分享 程序员 微信小程序 职场和发展