SpringBoot配置Elasticsearch以逗号分词查询(自定义逗号分词器)
①在resources目录下创建commaAnalyzer_setting.json配置文件。
ps:注意pom.xml配置扫描json文件
{
"analysis": {
"analyzer": {
"comma": {
"type": "pattern",
"pattern": ","
}
}
}
}
②为文档类添加注解。
@Setting(settingPath = "commaAnalyzer_setting.json")
③为要分词的字典添加注解属性
@Field(type = FieldType.Text, analyzer = "comma", searchAnalyzer = "comma")
④删除索引(如果已经存在索引,需要先删除索引才能重新生成带分词器的索引)。
⑤重启项目(重新生成索引),如图就是成功生成。
⑥测试。
{
"query": {
"bool": {
"must": [
{
"match": {
"worksTitle": {
"query": "动漫",
"operator": "OR",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"boost": 1.0
}
}
},
{
"term": {
"isPublish": {
"value": 1,
"boost": 1.0
}
}
},
{
"match": {
"worksTag": {
"query": "0,1",
"operator": "OR",
"analyzer": "comma",
"prefix_length": 0,
"max_expansions": 50,
"fuzzy_transpositions": true,
"lenient": false,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"boost": 1.0
}
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
},
"highlight": {
"fields": {
"worksTitle": {}
}
}
}
成功查询到数据
⑦SpringData代码实现。文章来源:https://www.toymoban.com/news/detail-581652.html
boolQueryBuilder.must(QueryBuilders.matchQuery("worksTag", worksInfoESDTO.getWorksTag()).analyzer("comma"));
参考文章,感谢大佬
spring data elasticsearch 使用 自定义分词/逗号分词 遇到的坑
SpringBoot整合ElasticSearch自定义分词器Analyzer文章来源地址https://www.toymoban.com/news/detail-581652.html
到了这里,关于SpringBoot配置Elasticsearch以逗号分词查询(自定义逗号分词器)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!