通过多个字段进行匹配查询 [英] Query with match by multiple fields
问题描述
我对弹性搜索还很陌生,想编写一个涉及两个字段的查询.我的意思是字段的内容包含指定的子字符串.我有一个包含字段的文档,如下所示:
I'm pretty new to elastic search and would like to write a query that is concerned about two fields. I mean the content of the fields contains the specified substring. I have a document containing fields, like this:
name: n
tag: t
我尝试过:
/_search -d '
{
"query": {
"match": {
"name": "n",
"tag": "t"
}
}
}
但是查询会导致以下错误:
But the query results in the following error:
[match]查询以简化形式解析,具有直接字段名称,但是不仅包含字段名称,还包含更多选项,可能使用其带有查询"元素的选项"形式?
[match] query parsed in simplified form, with direct field name, but included more options than just the field name, possibly use its 'options' form, with 'query' element?
elasticsearch中有没有办法做到这一点?
Is there a way to do this in elasticsearch?
推荐答案
您需要在 bool/must
查询中包含两个 match
查询,如下所示:
You need two match
queries enclosed in a bool/must
query, like this:
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "n"
}
},
{
"match": {
"tag": "t"
}
}
]
}
}
}
这篇关于通过多个字段进行匹配查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!