如何将2个匹配查询加入到弹性搜索的查询中? [英] How to join 2 match queries into a query for elasticsearch?
本文介绍了如何将2个匹配查询加入到弹性搜索的查询中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想查询 user_id
的数据是'1'
和名称
是'John'
。很容易编写一个常用的SQL:
I'd like to query for data that user_id
is '1'
and name
is 'John'
. It's easy to write a commonly-used SQL:
select * from t where user_id = '1' and name = 'John';
但是,对弹性搜索进行查询并不容易。
But it's not easy for me to make a query for elasticsearch.
首先,我查询了 user_id
:
{
"query" : {
"match" : {
"user_id" : "1"
}
}
}
结果是我预期的。
然后,我查询了 name
:
{
"query" : {
"match" : {
"name" : "John"
}
}
}
它也很好。
但是我无法查询加入2条件与操作。如何将这2个匹配查询加入到一个使用和操作中?
But I couldn't make a query joining 2 conditions with and operation. How can I join those 2 match queries into one using and operation?
推荐答案
您需要的是一个 bool查询,您可以在其中放入所有单个查询:
What you need is a bool query in which you put all your single queries:
(我无法测试查询,可能是错误的,但布尔查询是您的问题的答案)
{
"bool" : {
"must" : [{
"match" : {
"user_id" : "1"
},
"match" : {
"name" : "John"
}
}]
}
}
这篇关于如何将2个匹配查询加入到弹性搜索的查询中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文