我使用的是ElasticSearch的PHP客户端,当我想搜索字符串中“隐藏”的单词时,发现很难返回分数的结果。
这是一个例子:
我想获取所有文件,其中"file"字段的单词为“anses”,文件的命名如下:
axx14anses19122015.zip
我对它的了解
我知道我应该将这些单词标记化,无法实现。
我也读过有关聚合的文章,但我对ES真的很陌生,我必须尽快交付工作文件。
我到目前为止已经尝试过的内容
提前致谢。
最佳答案
我建议您使用nGram tokenizer参见示例
我将为文档类型创建一个分析器和一个映射
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"tokenizer": {
"ngram_tokenizer": {
"type": "nGram",
"min_gram": 4,
"max_gram": 4,
"token_chars": [ "letter", "digit" ]
}
},
"analyzer": {
"ngram_tokenizer_analyzer": {
"type": "custom",
"tokenizer": "ngram_tokenizer",
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"text_field": {
"type": "string",
"term_vector": "yes",
"analyzer": "ngram_tokenizer_analyzer"
}
}
}
}
}
之后,我将使用您的文件名插入文档PUT /test_index/doc/1
{
"text_field": "axx14anses19122015"
}
现在我将使用查询匹配POST /test_index/_search
{
"query": {
"match": {
"text_field": "anses"
}
}
}
并会收到这样的回复{
"took": 8,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.10848885,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.10848885,
"_source": {
"text_field": "axx14anses19122015"
}
}
]
}
}
我做了什么?我刚刚创建了一个nGram标记生成器,它将用4个字符的术语爆炸我们的字符串,并将这些术语分开索引,当我搜索字符串的一部分时将对它们进行搜索。
要了解更多信息,请阅读这篇文章https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch
希望对您有所帮助!
关于php - Elasticsearch:在字符串中搜索单词并获得分数的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35582628/