我需要创建弹性搜索查询,以从已经索引到弹性搜索索引的日志中过滤掉。
问题陈述是列出电子邮件为“ [email protected]”且属于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 = '[email protected]'
)
AND
(
event = 'checkIn'
or event = 'checkOut'
or
(
event = 'temperature'
AND temperature >= 28.7
AND temperature <= 37.8
)
)
其中日志是索引的名称,company_id,电子邮件,事件,温度,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'
}
}
]
}
}
]
}
}
但这并不能正常工作。
在这里的任何帮助,将不胜感激。谢谢
与您的SQL查询相对应的DSL查询如下。它与您所拥有的略有不同。
{
"query": {
"bool": {
"filter": [
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"term": {
"company_id": "123"
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "company_id"
}
}
}
}
]
}
},
{
"term": {
"email.keyword": "[email protected]"
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"terms": {
"event": ["checkIn", "checkOut"]
}
},
{
"bool": {
"filter": [
{
"term": {
"event": "temperature"
}
},
{
"range": {
"temperature": {
"gte": 28.7,
"lte": 37.8
}
}
}
]
}
}
]
}
}
]
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句