我已直接在elastcisearch中保存了用户输入。名称字段具有针对同一学生的各种拼写组合。
PrabhuNath Prasad
PrabhuNathPrasad
Prabhu NathPrasad
Prabhu Nath Prashad
PrabhuNath Prashad
PrabhuNathPrashad
Prabhu NathPrashad
学生的真实姓名是&#34; Prabhu Nath Prasad &#34;当我用这个名字搜索时,我应该得到以上所有结果。弹性搜索中是否有任何分析器可以处理它?</ p>
答案 0 :(得分:2)
你可以这样做custom_analyzer
,这是我的设置
POST name_index
{
"settings": {
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"char_filter": [
"space_removal"
],
"tokenizer": "keyword",
"filter": [
"lowercase",
"asciifolding"
]
}
},
"char_filter": {
"space_removal": {
"type": "pattern_replace",
"pattern": "\\s+",
"replacement": ""
}
}
}
},
"mappings": {
"your_type": {
"properties": {
"name": {
"type": "string",
"fields": {
"variation": {
"type": "string",
"analyzer": "my_custom_analyzer"
}
}
}
}
}
}
}
我已将name
映射到standard analyzer
和custom_analyzer
,其中使用keyword tokenizer
和lowercase filter
以及char_filter
删除空格并加入串。这个char_filter
将有助于我们有效地查询不同的变体。
我插入了你在索引中给出的所有7种组合。这是我的查询
GET name_index/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "Prabhu Nath Prasad"
}
},
{
"match": {
"name.variation": {
"query": "Prabhu Nath Prasad",
"fuzziness": "AUTO"
}
}
}
]
}
}
}
这可以处理所有可能性,它还会返回 prabhu , prasad 等。
希望这会有所帮助!!
答案 1 :(得分:1)
然而,没有分析器,你可以看到的是&#34;模糊&#34; ..
在您的查询中指定可以帮助您获取上述记录的模糊性。
我建议你浏览下面的链接
https://www.elastic.co/blog/found-fuzzy-search
https://www.elastic.co/guide/en/elasticsearch/guide/current/fuzzy-match-query.html
https://www.elastic.co/guide/en/elasticsearch/guide/current/fuzziness.html
这将帮助您实现您想要的目标。
如果用户键入&#34; PrabhuNath&#34;也不会有任何直接的方法来获取记录,因为弹性会将其视为单个令牌,但是你可以使用&#34; phrase_prefix&#34;查询,帮助您在用户输入时获取记录..
您的查询将如下所示以获得基本的拼写错误
{
"query": {
"match": {
"name": {
"query":"PrabhuNath Prasad",
"fuzziness": 2
}
}
}
}