我有这些数据:
name:
first: 'John'
last: 'Smith'
Run Code Online (Sandbox Code Playgroud)
当我将它存储在ES中时,AFAICT最好将其设置为一个字段.但是,这个字段应该是:
name: 'John Smith'
Run Code Online (Sandbox Code Playgroud)
要么
name: 'JohnSmith'
Run Code Online (Sandbox Code Playgroud)
?
我认为查询应该是:
query:
match:
name:
query: searchTerm
fuzziness: 'AUTO'
operator: 'and'
Run Code Online (Sandbox Code Playgroud)
示例搜索词是人们可能在搜索框中输入的内容,例如
John
Jhon Smi
J Smith
Smith
Run Code Online (Sandbox Code Playgroud)
等等
Slo*_*ens 13
您可能需要ngrams和模糊匹配查询的组合.如果你需要一本入门书,我写了一篇关于Qbox的ngrams的博客文章:http://blog.qbox.io/an-introduction-to-ngrams-in-elasticsearch .我会在帖子的末尾刷一下入门代码来说明我的意思.
另外,我认为你是使用两个字段name还是只使用一个字段并不重要.如果您有其他原因需要两个字段,则可能需要在查询中使用该_all字段.为简单起见,我在这里只使用一个字段.
这是一个映射,它将为您提供所需的部分单词匹配,假设您只关心从单词开头开始的标记(否则使用ngrams而不是edge ngrams).使用ngrams有很多细微差别,所以如果你想了解更多信息,我会向你介绍文档和我的入门知识.
PUT /test_index
{
"settings": {
"number_of_shards": 1,
"analysis": {
"filter": {
"edge_ngram_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 10
}
},
"analyzer": {
"edge_ngram_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"edge_ngram_filter"
]
}
}
}
},
"mappings": {
"doc": {
"properties": {
"name": {
"type": "string",
"index_analyzer": "edge_ngram_analyzer",
"search_analyzer": "standard"
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
有一点需要注意,特别是:"min_gram": 1.这意味着将从索引值生成单字符标记.当您查询时(例如,许多单词以"j"开头),这将投出相当广泛的网络,因此您可能会得到一些意想不到的结果,尤其是当与模糊结合时.但这需要让"J Smith"查询正常工作.因此需要考虑一些权衡因素.
为了说明,我索引了四个文件:
PUT /test_index/doc/_bulk
{"index":{"_id":1}}
{"name":"John Hancock"}
{"index":{"_id":2}}
{"name":"John Smith"}
{"index":{"_id":3}}
{"name":"Bob Smith"}
{"index":{"_id":4}}
{"name":"Bob Jones"}
Run Code Online (Sandbox Code Playgroud)
您的查询大多数都有效,但需要注意几点.
POST /test_index/_search
{
"query": {
"match": {
"name": {
"query": "John",
"fuzziness": "AUTO",
"operator": "and"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
此查询返回三个文档,因为ngrams加上模糊:
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.90169895,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "1",
"_score": 0.90169895,
"_source": {
"name": "John Hancock"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 0.90169895,
"_source": {
"name": "John Smith"
}
},
{
"_index": "test_index",
"_type": "doc",
"_id": "4",
"_score": 0.6235822,
"_source": {
"name": "Bob Jones"
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
这可能不是你想要的.此外,"AUTO"不适用于"Jhon Smi"查询,因为"Jhon"与"John"的编辑距离为2,而"AUTO"使用编辑距离1为3-5个字符的字符串(请参阅文档了解更多信息).所以我必须使用此查询:
POST /test_index/_search
{
"query": {
"match": {
"name": {
"query": "Jhon Smi",
"fuzziness": 2,
"operator": "and"
}
}
}
}
...
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.4219328,
"hits": [
{
"_index": "test_index",
"_type": "doc",
"_id": "2",
"_score": 1.4219328,
"_source": {
"name": "John Smith"
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
其他查询按预期工作.所以这个解决方案并不完美,但它会让你接近.
这是我使用的所有代码:
http://sense.qbox.io/gist/ba5a6741090fd40c1bb20f5d36f3513b4b55ac77
| 归档时间: |
|
| 查看次数: |
2623 次 |
| 最近记录: |