【问题标题】:Elasticsearch filtering for the same range brings back different amount of results相同范围的 Elasticsearch 过滤会返回不同数量的结果
【发布时间】:2016-09-29 16:57:32
【问题描述】:

我要执行以下查询

SELECT * 
FROM logs
WHERE dst != "-" 
AND @timestamp > "a date before" AND @timestamp < "now"

我使用python elasticsearch sdk,形成了两个查询进行测试

from elasticsearch import Elasticsearch
from datetime import datetime, timedelta

now = datetime.now()
four_hours_before = now - timedelta(hours=4)

es = Elasticsearch("http://es.domain.com:9200")

query_bool_filter = {
    'query': {
        'bool': {'
             filter': {
                 'bool': {
                     'must_not': {
                         'term': {
                             'dst': '-'
                          }
                      }, 
                      'must': {
                          'range': {
                              '@timestamp': {
                                  'gte': four_hours_before, 
                                  'lte': now
                                  }
                              }
                          }
                      }
                  }
              }
          }
      }

第二个查询使用 must_not 与过滤器分开

query_bool_and_filter = {
    'query': {
        'bool': {
            'filter': {
                'range': {
                    '@timestamp': {
                        'gte': four_hours_before, 
                        'lte': now
                    }
                }
            },
            'must_not': {
                'term': {
                    'dst': '-'
                }
            }
        }
    }
}

当我使用 python sdk 中的搜索执行查询并比较返回结果中的总字段时,它的不同之处如下:

res1 = es.search(index="myindex", body=query_bool_filter)
res2 = es.search(index="myindex", body=query_bool_and_filter)

res1.get('hits').get('total') #prints 43197
res2.get('hits').get('total') #prints 43215

既然范围相同,为什么我会得到不同的数字?

【问题讨论】:

    标签: python elasticsearch


    【解决方案1】:

    您可以尝试logging,看看您的弹性搜索查询到底发生了什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-19
      • 1970-01-01
      • 2013-09-03
      • 1970-01-01
      相关资源
      最近更新 更多