Elasticsearch 如何使用 multi_match 和通配符 [英] Elasticsearch how to use multi_match with wildcard
问题描述
我有一个具有名称和姓氏属性的用户对象.我想使用一个查询来搜索这些字段,我在文档中找到了 multi_match
,但我不知道如何正确使用通配符.是否可以?
I have a User object with properties Name and Surname. I want to search these fields using one query, and I found multi_match
in the documentation, but I don't know how to properly use that with a wildcard. Is it possible?
我尝试使用 multi_match
查询,但没有奏效:
I tried with a multi_match
query but it didn't work:
{
"query": {
"multi_match": {
"query": "*mar*",
"fields": [
"user.name",
"user.surname"
]
}
}
}
推荐答案
或者,您可以使用带有通配符的 query_string
查询.
Alternatively you could use a query_string
query with wildcards.
"query": {
"query_string": {
"query": "*mar*",
"fields": ["user.name", "user.surname"]
}
}
这将比在索引时使用 nGram 过滤器慢(请参阅我的其他答案),但是如果您正在寻找快速而肮脏的解决方案...
This will be slower than using an nGram filter at index-time (see my other answer), but if you are looking for a quick and dirty solution...
我也不确定你的映射,但如果你使用 user.name
而不是 name
你的映射需要看起来像这样:
Also I am not sure about your mapping, but if you are using user.name
instead of name
your mapping needs to look like this:
"your_type_name_here": {
"properties": {
"user": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"surname": {
"type": "string"
}
}
}
}
}
这篇关于Elasticsearch 如何使用 multi_match 和通配符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!