我应该在模糊查询字段中包含空格吗?
Should I include spaces in fuzzy query fields?
我有这个数据:
name:
first: 'John'
last: 'Smith'
当我将它存储在 ES 中时,AFAICT 最好将其设为一个字段。但是,这一字段应该是:
name: 'John Smith'
或
name: 'JohnSmith'
?
我认为查询应该是:
query:
match:
name:
query: searchTerm
fuzziness: 'AUTO'
operator: 'and'
示例搜索词是人们可能会在搜索框中键入的内容,例如
John
Jhon Smi
J Smith
Smith
等等
您可能需要 ngrams and a fuzzy match query. I wrote a blog post about ngrams for Qbox if you need a primer: https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch 的组合。我将在 post 末尾滑动启动代码来说明我的意思。
另外,我认为 name
使用两个字段还是一个字段并不重要。如果您出于其他原因需要两个字段,则可能需要在查询中使用 _all
field。为简单起见,我在这里只使用一个字段。
假设您只关心从单词开头开始的标记(否则使用 ngrams instead of 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"
}
}
}
}
}
这里要注意一件事,特别是:"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"}
您的查询基本有效,但有几点需要注意。
POST /test_index/_search
{
"query": {
"match": {
"name": {
"query": "John",
"fuzziness": "AUTO",
"operator": "and"
}
}
}
}
这个查询returns三个文档,因为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"
}
}
]
}
}
这可能不是您想要的。此外,"AUTO"
不适用于 "Jhon Smi" 查询,因为 "Jhon" 与 "John" 的编辑距离为 2,而 "AUTO" 使用的编辑距离为1 表示 3-5 个字符的字符串(有关详细信息,请参阅 docs)。所以我必须改用这个查询:
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"
}
}
]
}
}
其他查询按预期工作。所以这个解决方案并不完美,但它会让你接近。
这是我使用的所有代码:
http://sense.qbox.io/gist/ba5a6741090fd40c1bb20f5d36f3513b4b55ac77
我有这个数据:
name:
first: 'John'
last: 'Smith'
当我将它存储在 ES 中时,AFAICT 最好将其设为一个字段。但是,这一字段应该是:
name: 'John Smith'
或
name: 'JohnSmith'
?
我认为查询应该是:
query:
match:
name:
query: searchTerm
fuzziness: 'AUTO'
operator: 'and'
示例搜索词是人们可能会在搜索框中键入的内容,例如
John
Jhon Smi
J Smith
Smith
等等
您可能需要 ngrams and a fuzzy match query. I wrote a blog post about ngrams for Qbox if you need a primer: https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch 的组合。我将在 post 末尾滑动启动代码来说明我的意思。
另外,我认为 name
使用两个字段还是一个字段并不重要。如果您出于其他原因需要两个字段,则可能需要在查询中使用 _all
field。为简单起见,我在这里只使用一个字段。
假设您只关心从单词开头开始的标记(否则使用 ngrams instead of 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"
}
}
}
}
}
这里要注意一件事,特别是:"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"}
您的查询基本有效,但有几点需要注意。
POST /test_index/_search
{
"query": {
"match": {
"name": {
"query": "John",
"fuzziness": "AUTO",
"operator": "and"
}
}
}
}
这个查询returns三个文档,因为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"
}
}
]
}
}
这可能不是您想要的。此外,"AUTO"
不适用于 "Jhon Smi" 查询,因为 "Jhon" 与 "John" 的编辑距离为 2,而 "AUTO" 使用的编辑距离为1 表示 3-5 个字符的字符串(有关详细信息,请参阅 docs)。所以我必须改用这个查询:
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"
}
}
]
}
}
其他查询按预期工作。所以这个解决方案并不完美,但它会让你接近。
这是我使用的所有代码:
http://sense.qbox.io/gist/ba5a6741090fd40c1bb20f5d36f3513b4b55ac77