代码之家  ›  专栏  ›  技术社区  ›  martin-g

Elasticsearch:仅在值更改时获取文档

  •  0
  • martin-g  · 技术社区  · 6 年前

    from_1,to_1,timestamp_1
    from_1,to_1,timestamp_2
    from_1,to_2,timestamp_3
    from_2,to_3,timestamp_4
    from_1,to_2,timestamp_5
    from_2,to_3,timestamp_6
    from_1,to_1,timestamp_7
    from_2,to_4,timestamp_8
    

    我需要一个查询,只有当它的 from to 值不同于以前看到的具有相同 价值观。

    1. 文档 timestamp_1 from_1 to_1 结合
    2. 文档 timestamp_2 必须跳过,因为 + 来自\u 1
    3. timestamp_3 应该在结果中,因为 字段( to_2 )与上次看到的值不同的是同一个 ( 至\u 1 在文档中 时间戳1
    4. 文档 timestamp_4 应该在结果中
    5. 文档 timestamp_5 不能出现在结果中,因为它的from+to组合与最后看到的with相同 (文档) 时间戳3 )
    6. timestamp_6 from_2 时间戳4 )
    7. 文档 timestamp_7 应该在结果中,因为它具有从+到最后一次使用的不同组合 来自\u 1 时间戳3 )
    8. 文档 timestamp_8

    我需要从索引中获取所有这些“半唯一”文档,因此如果可以使用 scroll 请求或 after_key 如果使用聚合。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Benjamin Trent    6 年前

    我能想到的最接近的东西是以下内容(如果它不适用于您的数据,请告诉我)。

    {
      "size": 0,
      "aggs": {
        "from_and_to": {
          "composite" : {
            "size": 5,
            "sources": [
              {
                "from_to_collected":{
                  "terms": {
                    "script": {
                      "lang": "painless",
                      "source": "doc['from'].value + '_' + doc['to'].value"
                    }
                  }
                }
              }]
          },
          "aggs": {
            "top_from_and_to_hits": {
              "top_hits": {
                "size": 1,
                "sort": [{"timestamp":{"order":"asc"}}],
                "_source": {"includes": ["_id"]}
              }
            }
          }
        }
      }
    }
    

    请记住 terms aggregations is probabilistic .

    from_to_collected 钥匙。