今天就来和大家讲讲如何将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用来做自动补全查询。
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 tagsList;
private List 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"
}
}
}
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net
shell命令概述 Shell作用:命令解释器 介于操作系统内核与用户之间,负责解释命令行 获得命令帮助 内部命令help 命令的“–help” 选项 使用man命令阅读手册页 命令行编辑的几个辅助操作 Tab键:自动补齐 反斜杠“”:强制换行 快捷键 Ct…