elasticsearch通过Java class类的@Setting和@Mapping来定义索引index

elasticsearch通过Java class类的@Setting和@Mapping来定义索引index

​ 今天就来和大家讲讲如何将es索引中的mapping和setting在索引index和class联系起来,其实在这个问题也困扰我好久了,一直没有解决,在elasticsearch7.x版本的时候貌似好像可以用request在程序中来建立索引,像String index = “{ “mapping”:…}”之类的操作,干起来比较复杂,在elasticsearch8.x的版本中可以建立起一个实体类,使用@Field注解来说明每个字段的定义,但是这个注解无法处理suggest之类的自动补全查询,还好elasticsearch官方提供了class链接json文件的处理方式,可以解决注解的无法处理的问题。

​ 下面是定义PostIndex相当于mybatisplus中的entity实体类,post-setting.json文件中可以定义分词器,例如简繁体、拼音分词器、同义词转换等等之类的分词器。

​ post-mapping.json文件中定义class对应elasticsearch的数据类型,可以定义fields用来做自动补全查询。

elasticsearch通过Java class类的@Setting和@Mapping来定义索引index_第1张图片

PostIndex实体类

@Data
@Document(indexName = "post")
@Setting(settingPath = "json/post-setting.json")
@Mapping(mappingPath = "json/post-mapping.json")
public class PostIndex {
    private Long id;
    private Long postId;
    private String username;
    private String avatar;
    private String content;
    private Integer source;
    private Integer isShow;
    private Integer order;
    private String igUrl;
    private Date createTime;
    private Date updateTime;
    private Long userid;
    private List<String> tagsList;
    private List<String> postResourcesList;
    private String creator;
    private String updater;
    private Integer status;
    private Integer likes;

}

setting.json文件

{
  "analysis": {
    "analyzer": {
      "text_analyzer": {
        "filter": ["py"],
        "tokenizer": "ik_max_word",
        "char_filter": [
          "tsConvert"
        ]
      },
      "completion_analyzer": {
        "filter": ["tsConvert", "py"],
        "tokenizer": "keyword"
      },
      "tsConvert": {
        "tokenizer": "tsConvert"
      },
      "ikSearchAnalyzer": {
        "type": "custom",
        "tokenizer": "ik_max_word",
        "char_filter": [
          "tsConvert"
        ]
      }
    },
    "filter": {
      "py": {
        "type": "pinyin",
        "keep_full_pinyin": false,
        "keep_joined_full_pinyin": true,
        "keep_original": true,
        "limit_first_letter_length": 16,
        "remove_duplicated_term": true,
        "none_chinese_pinyin_tokenize": false
      },
      "tsConvert": {
        "convert_type": "t2s",
        "type": "stconvert",
        "keep_both": "false",
        "delimiter": "#"
      }
    },
    "char_filter": {
      "tsConvert": {
        "convert_type": "t2s",
        "type": "stconvert"
      }
    },
    "tokenizer": {
      "tsConvert": {
        "convert_type": "t2s",
        "type": "stconvert",
        "keep_both": "false",
        "delimiter": "#"
      }
    }
  }
}

mapping.json文件

{
  "properties": {
    "id": {
      "type": "long"
    },
    "avatar": {
      "type": "keyword"
    },
    "content": {
      "type": "text",
      "store": true,
      "analyzer": "text_analyzer",
      "search_analyzer": "ikSearchAnalyzer",
      "fields": {
        "suggest": {
          "type": "completion",
          "analyzer": "ik_max_word",
          "preserve_separators": true,
          "preserve_position_increments": true,
          "max_input_length": 50
        }
      }
    },
    "createTime": {
      "type": "date",
      "index": false,
      "store": true,
      "format": "date_optional_time",
      "ignore_malformed": true
    },
    "creator": {
      "type": "keyword",
      "store": true
    },
    "igUrl": {
      "type": "keyword"
    },
    "isShow": {
      "type": "integer",
      "index": false,
      "store": true
    },
    "postResourcesList": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "tagsList": {
      "type": "text",
      "fields": {
        "keyword": {
          "type": "keyword",
          "ignore_above": 256
        }
      }
    },
    "likes": {
      "type": "keyword",
      "store": true
    },
    "order": {
      "type": "keyword"
    },
    "postId": {
      "type": "long"
    },
    "source": {
      "type": "integer",
      "index": false,
      "store": true
    },
    "status": {
      "type": "keyword",
      "store": true
    },
    "updateTime": {
      "type": "date",
      "index": false,
      "store": true,
      "format": "date_optional_time",
      "ignore_malformed": true
    },
    "updater": {
      "type": "keyword",
      "store": true
    },
    "userid": {
      "type": "keyword"
    },
    "username": {
      "type": "keyword",
      "store": true
    },
    "all": {
      "type": "text",
      "analyzer": "text_analyzer",
      "search_analyzer": "ikSearchAnalyzer"
    }
  }
}

你可能感兴趣的:(elasticsearch,Java,springboot,elasticsearch,java,大数据)