【问题标题】:Making filter query with elastic search使用弹性搜索进行过滤查询
【发布时间】: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


    【解决方案1】:

    与您的 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": "test@yopmail.com"
              }
            },
            {
              "bool": {
                "minimum_should_match": 1,
                "should": [
                  {
                    "terms": {
                      "event": ["checkIn", "checkOut"]
                    }
                  },
                  {
                    "bool": {
                      "filter": [
                        {
                          "term": {
                            "event": "temperature"
                          }
                        },
                        {
                          "range": {
                            "temperature": {
                              "gte": 28.7,
                              "lte": 37.8
                            }
                          }
                        }
                      ]
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2014-09-29
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-28
      相关资源
      最近更新 更多