首页 文章

Elasticsearch DSL查询 - 获取所有匹配结果

提问于
浏览
1

我正在尝试使用DSL查询搜索索引 . 我有许多文件符合日志标准和时间戳范围 .
我正在通过日期并将其转换为纪元毫秒 .
但是我在DSL查询中指定了size参数 .
我看到的是,如果我指定5000,它会在时间范围内提取5000条记录 . 但是在指定的时间范围内有更多的记录 .
如何检索匹配时间范围的所有数据,以便我不需要指定大小?

我的DSL查询如下 .

GET localhost:9200/_search    
{
    "query": {
      "bool": {
        "must": [
          {"match_phrase": {
              "log":  "SOME_VALUE"
              }
            },
             {"range": {
                "@timestamp": {
                  "gte": "'"${fromDate}"'", 
                  "lte": "'"${toDate}"'", 
                  "format": "epoch_millis"
                }
              }
            }
                ]
              }
            },    
        "size":5000
}

fromDate = 1519842600000
toDate = 1520533800000

1 回答

  • 1

    我无法使扫描API或滚动模式工作,因为它也没有显示预期的结果 .

    我终于想出了一种捕获命中数的方法,然后将其作为参数传递以提取数据 .

    GET localhost:9200/_count    
    {
    "query": {
      "bool": {
        "must": [
          {"match_phrase": {
              "log":  "SOME_VALUE"
              }
            },
             {"range": {
                "@timestamp": {
                  "gte": "'"${fromDate}"'", 
                  "lte": "'"${toDate}"'", 
                  "format": "epoch_millis"
                }
              }
            }
                ]
              }
            }
    }' > count_size.txt
    size_count=`cat count_size.txt  | cut -d "," -f1 | cut -d ":" -f2`
    echo "Total hits matching this criteria is ${size_count}"
    

    从这里我得到size_count值 . 如果此值小于10000,则提取该值,否则减少提取的时间范围 .

    GET localhost:9200/_search    
    {
    "query": {
      "bool": {
        "must": [
          {"match_phrase": {
              "log":  "SOME_VALUE"
              }
            },
             {"range": {
                "@timestamp": {
                  "gte": "'"${fromDate}"'", 
                  "lte": "'"${toDate}"'", 
                  "format": "epoch_millis"
                }
              }
            }
                ]
              }
            },    
        "size":'"${size_count}"'
    }
    

    如果在很长一段时间内需要大量数据,我需要使用不同的日期集来运行它,并将它们组合在一起以获得总体所需的报告 .

    这段完整的代码是shell脚本,因此我可以更简单地使用它 .

相关问题