Elasticsearch使用IK分词器的坑

Elasticsearch使用IK分词器

定义实体指定index,type,分片数量

IK分词器分类:ik_smart(粗粒度),ik_max_word(细粒度)
注意: es版本为7.0.0以下的不支持ik_max_word7.0.0以下的仅支持ik_smart分词器

具体使用方式如下:

@Data
@Document(indexName = "entertainment_idx_sole", type = "culture_data", shards = 3)
public class SearchEntity {
    @Field(type = FieldType.Keyword)
    private String id;
    /**
     * 类型,精确查询
     */
    @Field(type = FieldType.Keyword)
    private String type;
    @Field(type = FieldType.Keyword)
    private String obj_id;//private Integer objId;
    /**
     * 标题
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
    private String title;
    /**
     * 豆瓣评分
     */
    @Field(type = FieldType.Text)
    private String db_score; //private Float dbScore;
    /**
     * 原名
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
    private String original_name;
    /**
     * 人名
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
    private String people_name;
    /**
     * 简介
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
    private String intro;
    /**
     * 品牌类型
     */
    @Field(type = FieldType.Text)
    private String brand_type;
    /**
     * 主演作品数
     */
    @Field(type = FieldType.Text)
    private String starring_works_num;//private Integer starringWorksNum;
    /**
     * 代表作
     */
    @Field(type = FieldType.Text, analyzer = "ik_smart", searchAnalyzer = "ik_smart")
    private String masterpiece;
}

es组合条件查询,并指定查询权重:

private BoolQueryBuilder getQueryBuilder(String keyword, String type) {
        MultiMatchQueryBuilder multiMatchQuery =
                QueryBuilders.multiMatchQuery(keyword,
                        "intro",
                        "title",
                        "original_name",
                        "people_name",
                        "db_score",
                        "publishing_house_name",
                        "masterpiece").field("title", 2.0f).field("intro", 1.5f);
        if (!ALL.equals(type)) {
            return QueryBuilders.boolQuery()
                    .must(QueryBuilders.termsQuery("type", type))
                    .must(multiMatchQuery);
        } else {
            return QueryBuilders.boolQuery().must(multiMatchQuery);
        }
    }

/**
     * QueryBuilders.matchQuery("originalName", keyword).boost(2.0f)设置权重
     *
     * @param type    查询类别
     * @param keyword 查询关键字
     * @return 结果
     */
    public List<SearchResponseVo> searchEntities(String type, String keyword) {
        try {
            BoolQueryBuilder queryBuilder = getQueryBuilder(keyword, type);
            Pageable pageable = PageRequest.of(0, 100);
            Iterable<SearchEntity> searchEntities = entertainmentSearchDao.search(queryBuilder, pageable);
            return searchResponseVos(Lists.newArrayList(searchEntities));
        } catch (Exception e) {
            log.error("searchEntities error:", e);
            throw new RuntimeException(e);
        }
    }

你可能感兴趣的:(Elasticsearch使用IK分词器的坑)