我正在使用弹性搜索,我正面临着一个问题。如果有人给我一个提示,我会非常感谢的。
我想分析由不同条目组成的字段"name“或"description”。例如有人想要搜身莎拉。如果他进入SARA,sara或SAra。他应该能抓到莎拉。elastic search使用分析器,使所有内容都是小写的。
我想实现它不区分大小写,不管用户输入的是大写还是小写的名字,他/她都应该得到结果。我使用ngram过滤器来搜索名字和小写,这使得它不区分大小写。但我想确保一个人即使输入大写或小写也能得到结果。
有没有办法在elastic search中做到这一点?
{"settings": {
"analysis": {
"filter": {
"ngram_filter": {
"type": "ngram",
"min_gram": 1,
"max_gram": 80
}
},
"analyzer": {
"index_ngram": {
"type": "custom",
"tokenizer": "keyword",
"filter": [ "ngram_filter", "lowercase" ]
},我附上了包含json示例和search.txt文件的example.js文件来解释我的问题。我希望我的问题现在会更清楚。这是我保存这两个文件的onedrive的链接。https://1drv.ms/f/s!AsW4Pb3Y55Qjb34OtQI7qQotLzc
发布于 2016-10-13 08:35:50
你使用ngram有什么特别的原因吗?Elasticsearch在“查询”和你索引的文本上使用相同的分析器-除非明确指定了search_analyzer,就像@Adam在他的回答中提到的那样。在您的示例中,使用带有小写过滤器的standard tokenizer可能就足够了
我创建了一个具有以下设置和映射的索引:
{
"settings": {
"analysis": {
"analyzer": {
"custom_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"typehere": {
"properties": {
"name": {
"type": "string",
"analyzer": "custom_analyzer"
},
"description": {
"type": "string",
"analyzer": "custom_analyzer"
}
}
}
}
}已为两个文档编制索引文档1
PUT /test_index/test_mapping/1
{
"name" : "Sara Connor",
"Description" : "My real name is Sarah Connor."
}文档2
PUT /test_index/test_mapping/2
{
"name" : "John Connor",
"Description" : "I might save humanity someday."
}做一个简单的搜索
POST /test_index/_search?query=sara
{
"query" : {
"match" : {
"name" : "SARA"
}
}
}并且只取回第一个文档。我试着用"sara“和"Sara”也是一样的结果。
{
"took": 12,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.19178301,
"hits": [
{
"_index": "test_index",
"_type": "test_mapping",
"_id": "1",
"_score": 0.19178301,
"_source": {
"name": "Sara Connor",
"Description": "My real name is Sarah Connor."
}
}
]
}
}发布于 2016-10-13 06:55:38
对全文搜索字段(已分析)执行分析过程两次:第一次是在存储数据时,第二次是在搜索时。值得一提的是,输入JSON将以与搜索查询的输出相同的形式返回。分析过程仅用于为倒排索引创建标记。解决方案的关键是以下步骤:
你可以在这里阅读更多信息:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-analyzer.html
你的代码应该是这样的:
PUT /my_index
{
"settings": {
"analysis": {
"filter": {
"ngram_filter": {
"type": "ngram",
"min_gram": 1,
"max_gram": 5
}
},
"analyzer": {
"index_store_ngram": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"ngram_filter",
"lowercase"
]
}
}
}
},
"mappings": {
"my_type": {
"properties": {
"name": {
"type": "string",
"analyzer": "index_store_ngram",
"search_analyzer": "standard"
}
}
}
}
}
post /my_index/my_type/1
{
"name": "Sara_11_01"
}
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": "sara"
}
}
}
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": "SARA"
}
}
}
GET /my_index/my_type/_search
{
"query": {
"match": {
"name": "SaRa"
}
}
}编辑1:更新了问题中提供的新示例的代码
发布于 2021-12-30 11:54:30
此答案在ElasticSearch 7.14的上下文中。所以,让我用另一种方式重新格式化这个问题:
Irrespective of the actual case type provided in the match query, you would like to be able to get those documents that have been analysed with :
"tokenizer": "keyword",
"filter": [ "ngram_filter", "lowercase" ]现在,到了答案部分:
不可能让match查询返回已经使用filter lowercase分析过的文档,并且match查询包含大写字母。您在settings中应用的analysis在更新和搜索数据时都适用。虽然,也可以应用不同的分析器来更新和搜索,但我不认为这对您的情况有帮助。在进行查询之前,必须将匹配查询值转换为小写。所以,如果你的过滤器是lowercase,你不能通过Sara或SARA或sAra等来match。match参数应该全是小写的,就像它在你的分析器中一样。
https://stackoverflow.com/questions/40007971
复制相似问题