gpt4 book ai didi

spring - 在Spring Data Elasticsearch中使用asiifolding过滤器创建自定义分析器

转载 作者:行者123 更新时间:2023-12-02 23:07:46 26 4
gpt4 key购买 nike

记录名称为cozum后使用çözümçözüm搜索时,我想检索同一对象。我已经搜索过,建议使用asciifolding filter。如何使用Spring Data Elasticsearch实现此功能?

    @Document(indexName = "erp")
public class Company {

@Id
private String id;

private String name;

private String description;

@Field(type = FieldType.Nested, includeInParent = true)
private List<Employee> employees;

// getters, setter
}

最佳答案

您将需要创建一个asciifolding分析器,为此请参见the Elasticsearch docs并将其添加到索引的索引设置中。
然后,您可以在name属性的@Field批注中引用此分析器。
编辑:完整示例
首先是用于索引设置的文件,我将其命名为erp-company.json并将其保存在src / main / resources下:

{
"analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"char_filter": [
"html_strip"
],
"filter": [
"lowercase",
"asciifolding"
]
}
}
}
}
然后,您需要在您的实体类(此处为 Company)中引用此文件和分析器:
@Document(indexName = "erp")
@Setting(settingPath = "/erp-company.json")
public class Company {

@Id
private String id;

@Field(type = FieldType.Text, analyzer = "custom_analyzer")
private String name;

@Field(type = FieldType.Text, analyzer = "custom_analyzer")
private String description;

// getters, setter
}
使用此的 CompanyController:
@RestController
@RequestMapping("/company")
public class CompanyController {

private final CompanyRepository repository;

public CompanyController(CompanyRepository repository) {
this.repository = repository;
}


@PostMapping
public Company put(@RequestBody Company company) {
return repository.save(company);
}

@GetMapping("/{name}")
public SearchHits<Company> get(@PathVariable String name) {
return repository.searchByName(name);
}
}
保存一些包含变音符号的数据(使用 httpie):
http POST :8080/company id=1 name="Renée et François"
不带变音符号的搜索:
http  GET :8080/company/francois

HTTP/1.1 200
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Connection: keep-alive
Content-Type: application/json
Date: Wed, 09 Sep 2020 17:56:16 GMT
Expires: 0
Keep-Alive: timeout=60
Pragma: no-cache
Transfer-Encoding: chunked
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

{
"aggregations": null,
"empty": false,
"maxScore": 0.2876821,
"scrollId": null,
"searchHits": [
{
"content": {
"description": null,
"id": "1",
"name": "Renée et François"
},
"highlightFields": {},
"id": "1",
"index": "erp",
"innerHits": {},
"nestedMetaData": null,
"score": 0.2876821,
"sortValues": []
}
],
"totalHits": 1,
"totalHitsRelation": "EQUAL_TO"
}
Elasticsearch为索引返回的索引信息:
{
"erp": {
"aliases": {},
"mappings": {
"properties": {
"_class": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"description": {
"analyzer": "custom_analyzer",
"type": "text"
},
"id": {
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
},
"type": "text"
},
"name": {
"analyzer": "custom_analyzer",
"type": "text"
}
}
},
"settings": {
"index": {
"analysis": {
"analyzer": {
"custom_analyzer": {
"char_filter": [
"html_strip"
],
"filter": [
"lowercase",
"asciifolding"
],
"tokenizer": "standard",
"type": "custom"
}
}
},
"creation_date": "1599673911503",
"number_of_replicas": "1",
"number_of_shards": "1",
"provided_name": "erp",
"uuid": "lRwcKcPUQxKKGuNJ6G30uA",
"version": {
"created": "7090099"
}
}
}
}
}

关于spring - 在Spring Data Elasticsearch中使用asiifolding过滤器创建自定义分析器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63810021/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com