【发布时间】:2021-02-24 17:18:07
【问题描述】:
我需要创建弹性搜索查询,以从已编入弹性搜索索引的日志中过滤掉。
问题陈述是列出电子邮件为'test@yopmail.com'并且是company_id'123'的一部分的用户的日志。随着更多过滤器的添加,日志列表会发生变化。例如,带有事件签入或签出的日志或温度范围在 28.7 - 37.8 之间的用户。
等效的mysql查询是:
select * from logs
where
(
company_id = 123 or company_id is null // company_id may be missing
)
AND
(
email = 'test@yopmail.com'
)
AND
(
event = 'checkIn'
or event = 'checkOut'
or
(
event = 'temperature'
AND temperature >= 28.7
AND temperature <= 37.8
)
)
其中 logs 是索引的名称,company_id、email、event、temperature、create_date 是字段(列)名称。
我生成的查询是:
'query' => {
'bool' => {
'must' => [
{
'bool' => {
'must' => {
'match' => {
"email.keyword" => {
'query' => $email, 'fuzziness' => 'auto'
}
}
},
'should' => {
{
'match' => {
"event" => {
'query' => "checkIn"
}
}
},
{
'match' => {
"event" => {
'query' => "checkOut"
}
}
},
{
'range' => {
"temperature" => {
"gte" => 28.7,
"lte" => 37.8
}
}
}
}
}
{
],
'should' => [
{
'bool' => {
'should' => [
{
'match' => {
"company_id" => [
'query' => $company_id
]
}
}
],
'must_not' => [
{
'exists' => {
'field' => 'company_id'
}
}
]
}
}
]
}
}
但这并不能正常工作。
我们将不胜感激。 谢谢
【问题讨论】:
标签: elasticsearch