ElasticSearch - 搜索人名

Nat*_*han 35 lucene search search-engine elasticsearch

我有一个大型的名字数据库,主要来自苏格兰.我们目前正在制作一个原型来替换执行搜索的现有软件.这仍然在生产中,我们的目标是尽可能地将结果与同一搜索的当前结果结合起来.

我希望有人可以帮助我,我正在搜索Elastic Search,查询是"Michael Heaney",我得到了一些疯狂的结果.当前搜索返回两个主要姓氏,这些是 - "Heaney"和"Heavey"都有"迈克尔"的名字,我可以在弹性搜索中获得"Heaney"结果但是我无法获得"Heavey"和ES也返回没有姓"迈克尔"的人,但我很欣赏这是由于它是模糊查询的一部分.我知道这是一个狭窄的用例,因为它只是一次搜索,但得到这个结果并知道如何获得它将有所帮助.

谢谢.

制图

{
   "jr": {
    "_all": {
        "enabled": true,
        "index_analyzer": "index_analyzer",
        "search_analyzer": "search_analyzer"
    },
    "properties": {
        "pty_forename": {
            "type": "string",
            "index": "analyzed",
            "boost": 2,
            "index_analyzer": "index_analyzer",
            "search_analyzer": "search_analyzer",
            "store": "yes"
        },
        "pty_full_name": {
            "type": "string",
            "index": "analyzed",
            "boost": 4,
            "index_analyzer": "index_analyzer",
            "search_analyzer": "search_analyzer",
            "store": "yes"
        },
        "pty_surname": {
            "type": "string",
            "index": "analyzed",
            "boost": 4,
            "index_analyzer": "index_analyzer",
            "search_analyzer": "search_analyzer",
            "store": "yes"
        }
     }
   }
}'
Run Code Online (Sandbox Code Playgroud)

索引设置

{
  "settings": {
    "number_of_shards": 2,
    "number_of_replicas": 0,
    "analysis": {
        "analyzer": {
            "index_analyzer": {
                "tokenizer": "standard",
                "filter": [
                    "standard",
                    "my_delimiter",
                    "lowercase",
                    "stop",
                    "asciifolding",
                    "porter_stem",
                    "my_metaphone"
                ]
            },
            "search_analyzer": {
                "tokenizer": "standard",
                "filter": [
                    "standard",
                    "my_metaphone",
                    "synonym",
                    "lowercase",
                    "stop",
                    "asciifolding",
                    "porter_stem"
                ]
            }
        },
        "filter": {
            "synonym": {
                "type": "synonym",
                "synonyms_path": "synonyms/synonyms.txt"
            },
            "my_delimiter": {
                "type": "word_delimiter",
                "generate_word_parts": true,
                "catenate_words": false,
                "catenate_numbers": false,
                "catenate_all": false,
                "split_on_case_change": false,
                "preserve_original": false,
                "split_on_numerics": false,
                "stem_english_possessive": false
            },
            "my_metaphone": {
                "type": "phonetic",
                "encoder": "metaphone",
                "replace": false
            }
        }
     }
   }
}'
Run Code Online (Sandbox Code Playgroud)

模糊

{
"from":0, "size":100,
"query": {
    "bool": {
        "should": [
            {
                "fuzzy": {
                    "pty_surname": {
                        "min_similarity": 0.2,
                        "value": "Heaney",
                        "prefix_length": 0,
                        "boost": 5
                    }
                }
            },
            {
                "fuzzy": {
                    "pty_forename": {
                        "min_similarity": 1,
                        "value": "Michael",
                        "prefix_length": 0,
                        "boost": 1
                    }
                }
            }
        ]
     }
  }
}
Run Code Online (Sandbox Code Playgroud)

Ale*_*vik 34

首先,我在Play中重新创建了您当前的配置:https://www.found.no/play/gist/867785a709b4869c5543

如果你去那里,切换到"分析"选项卡以查看文本的转换方式:

请注意,例如,Heaney最终标记为[hn, heanei]与,search_analyzer以及[HN, heanei]index_analyzer.请注意metaphone术语的大小写差异.因此,那个不匹配.

fuzzy-query没有做查询时间文本分析.因此,你最终会Heavey与之比较heanei.这比Damerau-Levenshtein距离长于参数允许的距离.

你真正想要做的是使用模糊功能match.匹配确实进行查询时文本分析,并具有模糊参数.

至于fuzziness,这在Lucene 4中有所改变.之前,它通常被指定为浮点数.现在它应该被指定为允许的距离.有一个突出的拉动请求来澄清:https://github.com/elasticsearch/elasticsearch/pull/4332/files

你得到没有名字的人的原因Michael是你正在做一个bool.should.这有OR语义.一个人匹配就足够了,但得分越多,匹配越多越好.

最后,将所有过滤结合到同一个术语中并不一定是最好的方法.例如,你无法知道并提升确切的拼写.您应该考虑使用multi_field以多种方式处理字段.

以下是您可以使用的示例,使用curl命令在下面重新创建它.不过,我完全不会完全使用"搬运工"割线机.我保留它只是为了展示multi_field的工作原理.使用匹配的组合,匹配模糊和语音匹配应该会让你走得更远.(确保你在进行语音匹配时不允许模糊 - 或者你会得到无用的模糊匹配.:-)

#!/bin/bash

export ELASTICSEARCH_ENDPOINT="http://localhost:9200"

# Create indexes

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{
    "settings": {
        "analysis": {
            "text": [
                "Michael",
                "Heaney",
                "Heavey"
            ],
            "analyzer": {
                "metaphone": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "my_metaphone"
                    ]
                },
                "porter": {
                    "type": "custom",
                    "tokenizer": "standard",
                    "filter": [
                        "lowercase",
                        "porter_stem"
                    ]
                }
            },
            "filter": {
                "my_metaphone": {
                    "encoder": "metaphone",
                    "replace": false,
                    "type": "phonetic"
                }
            }
        }
    },
    "mappings": {
        "jr": {
            "properties": {
                "pty_surename": {
                    "type": "multi_field",
                    "fields": {
                        "pty_surename": {
                            "type": "string",
                            "analyzer": "simple"
                        },
                        "metaphone": {
                            "type": "string",
                            "analyzer": "metaphone"
                        },
                        "porter": {
                            "type": "string",
                            "analyzer": "porter"
                        }
                    }
                }
            }
        }
    }
}'


# Index documents
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d '
{"index":{"_index":"play","_type":"jr"}}
{"pty_surname":"Heaney"}
{"index":{"_index":"play","_type":"jr"}}
{"pty_surname":"Heavey"}
'

# Do searches

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d '
{
    "query": {
        "bool": {
            "should": [
                {
                    "bool": {
                        "should": [
                            {
                                "match": {
                                    "pty_surname": {
                                        "query": "heavey"
                                    }
                                }
                            },
                            {
                                "match": {
                                    "pty_surname": {
                                        "query": "heavey",
                                        "fuzziness": 1
                                    }
                                }
                            },
                            {
                                "match": {
                                    "pty_surename.metaphone": {
                                        "query": "heavey"
                                    }
                                }
                            },
                            {
                                "match": {
                                    "pty_surename.porter": {
                                        "query": "heavey"
                                    }
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
}
'
Run Code Online (Sandbox Code Playgroud)

  • 我不明白为什么你需要两层应该和bool? (4认同)