我是ElasticSearch的新手。我正在尝试在ES中进行一个简单的家谱项目,并希望对名字和姓氏使用同义词。我具有以下ElasticSearch索引设置,希望将两个同义词分析器添加到我的设置中,然后使用这两个同义词分析器在不同字段上进行搜索时同义词处理。我不知道如何将正确的条目添加到我的“设置” JSON文件中
我的两个同义词文件分别称为given_synonyms.txt和surname_synonyms.txt。我非常感谢您提供一些帮助,以获取正确的json文件语法。
我为要执行的操作添加(大写)非常通用的伪代码,希望这样对读者更有意义。
{
"settings": {
"index": {
"number_of_shards": "128",
"number_of_replicas": "0",
"analysis": {
"filter": {
"dbl_metaphone": {
"type": "phonetic",
"encoder": "double_metaphone",
"max_code_len" : 5
}
},
"analyzer": {
"dbl_metaphone": {
"tokenizer": "standard",
"filter": "dbl_metaphone"
}
}
I ASSUME THE SYNONYM ENTRIES GO HERE FOR THE TWO SYNONYM FILES - BUT I DON'T KNOW HOW THAT SHOULD BE DONE
}
}
},
"mappings": {
"test": {
"_all": {
"enabled": false
},
"_source": {
"enabled": true
},
"properties": {
"GivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
I WANT TO USE THE GIVEN_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
}
},
"Surnames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
I WANT TO USE THE SURNAME_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
}
},
"FatherGivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
}
I WANT TO USE THE GIVEN_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
},
"FatherSurnames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
}
I WANT TO USE THE SURNAME_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
},
"MotherGivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
}
I WANT TO USE THE GIVEN_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
},
"MotherSurnames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
}
I WANT TO USE THE SURNAME_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
},
"SpouseGivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
}
I WANT TO USE THE GIVEN_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
},
"SpouseSurnames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
}
I WANT TO USE THE SURNAME_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
},
"ChildrenGivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone"
}
}
I WANT TO USE THE GIVEN_SYNONYMS SEARCH ANALYZER HERE IN ADDITION TO THE DBL_METAPHONE
},
"BirthYears": {
"type": "short"
},
"BirthLocations": {
"type": "integer"
},
"DeathYears": {
"type": "short"
},
"DeathLocations": {
"type": "integer"
},
"MarriageLocations": {
"type": "integer"
},
"MarriageYears": {
"type": "integer"
},
"ResidenceLocations": {
"type": "integer"
}
}
}
}
}
首先,让我们看一下同义词分析器以及如何包含文件。从这里https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-synonym-tokenfilter.html,您可以了解如何进行配置。您提到了两个文件。最好定义一个包含或同义词的文件。
{
"settings": {
"index": {
"number_of_shards": "128",
"number_of_replicas": "0",
"analysis": {
"filter": {
"dbl_metaphone": {
"type": "phonetic",
"encoder": "double_metaphone",
"max_code_len": 5
},
"given_synonyms": {
"type": "synonym",
"synonyms_path": "analysis/given_synonyms.txt"
},
"surname_synonyms": {
"type": "synonym",
"synonyms_path": "analysis/surname_synonyms.txt"
}
},
"analyzer": {
"dbl_metaphone": {
"tokenizer": "standard",
"filter": "dbl_metaphone"
},
"given_synonyms": {
"tokenizer": "whitespace",
"filter": "given_synonyms"
},
"surname_synonyms": {
"tokenizer": "whitespace",
"filter": "surname_synonyms"
}
}
}
}
}
}
请记住,当使用一个文件(或多个文件)作为同义词时,您需要确保每个elasticsearch节点都可以访问该文件。另一种方法是在设置部分中指定同义词。如果同义词的数量不是很大,或者在使文件可以被Elasticsearch节点访问时存在问题,这可能会更好。您可以在文档中查看更多定义同义词的方法
{
"settings": {
"index": {
"number_of_shards": "128",
"number_of_replicas": "0",
"analysis": {
"filter": {
"dbl_metaphone": {
"type": "phonetic",
"encoder": "double_metaphone",
"max_code_len": 5
},
"given_synonyms": {
"type": "synonym",
"synonyms": [ ... YOUR SYNONYMS HERE ...]
},
"surname_synonyms": {
"type": "synonym",
"synonyms": [ ... YOUR SYNONYMS HERE ...]
}
},
"analyzer": {
"dbl_metaphone": {
"tokenizer": "standard",
"filter": "dbl_metaphone"
},
"given_synonyms": {
"tokenizer": "standard",
"filter": "given_synonyms"
},
"surname_synonyms": {
"tokenizer": "standard",
"filter": "surname_synonyms"
}
}
}
}
}
}
为了达成最终解决方案,您可以有类似这样的东西
{
"settings": {
"index": {
"number_of_shards": "128",
"number_of_replicas": "0",
"analysis": {
"filter": {
"dbl_metaphone": {
"type": "phonetic",
"encoder": "double_metaphone",
"max_code_len": 5
},
"given_synonyms": {
"type": "synonym",
"synonyms_path": "analysis/given_synonyms.txt"
},
"surname_synonyms": {
"type": "synonym",
"synonyms_path": "analysis/surname_synonyms.txt"
}
},
"analyzer": {
"dbl_metaphone": {
"tokenizer": "standard",
"filter": "dbl_metaphone"
},
"dbl_metaphone_given_synonym": {
"tokenizer": "standard",
"filter": [
"given_synonyms",
"dbl_metaphone"
]
},
"dbl_metaphone_surname_synonym": {
"tokenizer": "standard",
"filter": [
"surname_synonyms",
"dbl_metaphone"
]
}
}
}
}
}
}
在此,您总共有三个分析仪。其中两个正在组合两个滤波器(第一个的输出是第二个的输入,因此顺序很重要)。在elasticsearch中,您可以指定在索引时间内使用一个分析器分析一个字段,当某个输入被搜索时,该输入将被另一个分析器分析。因此,您可以拥有这样的内容(从此处https://www.elastic.co/guide/en/elasticsearch/reference/current/search-analyzer.html)
{
"mappings": {
"properties": {
"text": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
}
}
}
所以您的设置/映射将变成这样
{
"settings": {
"index": {
"number_of_shards": "128",
"number_of_replicas": "0",
"analysis": {
"filter": {
"dbl_metaphone": {
"type": "phonetic",
"encoder": "double_metaphone",
"max_code_len": 5
},
"given_synonyms": {
"type": "synonym",
"synonyms_path": "analysis/given_synonyms.txt"
},
"surname_synonyms": {
"type": "synonym",
"synonyms_path": "analysis/surname_synonyms.txt"
}
},
"analyzer": {
"dbl_metaphone": {
"tokenizer": "standard",
"filter": "dbl_metaphone"
},
"dbl_metaphone_given_synonym": {
"tokenizer": "standard",
"filter": [
"given_synonyms",
"dbl_metaphone"
]
},
"dbl_metaphone_surname_synonym": {
"tokenizer": "standard",
"filter": [
"surname_synonyms",
"dbl_metaphone"
]
}
}
}
}
},
"mappings": {
"test": {
"_all": {
"enabled": false
},
"_source": {
"enabled": true
},
"properties": {
"GivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_given_synonym"
}
}
},
"Surnames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_surname_synonym"
}
}
},
"FatherGivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_given_synonym"
}
}
},
"FatherSurnames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_surname_synonym"
}
}
},
"MotherGivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_given_synonym"
}
}
},
"MotherSurnames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_given_synonym"
}
}
},
"SpouseGivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_given_synonym"
}
}
},
"SpouseSurnames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_surname_synonym"
}
}
},
"ChildrenGivenNames": {
"type": "keyword",
"index_options": "freqs",
"store": "false",
"similarity": "boolean",
"norms": "false",
"fields": {
"phonetic": {
"type": "text",
"analyzer": "dbl_metaphone",
"search_analyzer": "dbl_metaphone_given_synonym"
}
}
},
"BirthYears": {
"type": "short"
},
"BirthLocations": {
"type": "integer"
},
"DeathYears": {
"type": "short"
},
"DeathLocations": {
"type": "integer"
},
"MarriageLocations": {
"type": "integer"
},
"MarriageYears": {
"type": "integer"
},
"ResidenceLocations": {
"type": "integer"
}
}
}
}
}