【问题标题】:ElasticSearch get last n distinct recordsElasticSearch 获取最后 n 条不同的记录
【发布时间】:2019-02-27 07:51:25
【问题描述】:

我正在尝试对存储在 elasticsearch 中的记录执行搜索查询。 记录结构看起来像这样。

   {
    "_index" : "box_info_store",
    "_type" : "boxes",
    "_id" : "pWjQLWkBIJk0ORjd0X2P",
    "_score" : null,    
    "_source" : {
      "transactionID" : "60ab66cf24c9924f562bf1a2b5d92305d0a6",
      "boxNumber" : "Box3",
      "createDate" : "2013-09-17T00:00:00",
      "itemNumber" : "Item1",
      "address" : "Sample Address"
    }
  }

一个盒子可以包含多个项目。例如 Box3 可以有 Item1、Item2 和 Item3。所以在elasticsearch中我将有3个不同的文档。同时,同样的盒子和同样的物品也可以存在,但地址不同。这些文档的 transactionID 可能相同也可能不同。

我的要求是获取最近 n 个最近且不同的 transactionID 及其记录。

我尝试使用以下查询来获取最后 7 个不同的 transactionID

GET /box_info_store/boxes/_search?size=7
{
  "query": {
    "bool": {
       "must": [
         {"match":{"boxNumber":"Box3"}},
         {"match":{"itemNumber":"Item1"}}
         ]
    }
  },
  "sort": [
    {
      "createDate": {
        "order": "desc"
      }
    }
  ],
  "aggs": {
    "distinct_transactions": {
      "terms": { "field": "transactionID"}
    }
  }
}

这为我获取了最后 7 个文档,其中 boxNumber 是 Box3,itemNumber 是 Item1,但不是 7 个不同的 transactionID,这七个文档中有两个具有相同的 transactionID(尽管两者都有不同的地址)。 但我的要求是获得 7 个不同的 transactionId,无论它返回多少个文档。

希望我能够解释自己。 在这里感谢任何形式的帮助

谢谢

-----编辑@gaurav9620,我运行第一个查询并得到计数为32,然后我运行第二个查询,不同计数为3,我得到以下结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 32,
    "max_score" : null,
    "hits" : [
      {
        "_index" : "box_info_store",
        "_type" : "boxes",
        "_id" : "RWjRLWkBIJk0ORjdEX-L",
        "_score" : null,
        "_source" : {
          "transactionID" : "3087e106244f6247a5290fb21ce64254529c",
          "boxNumber" : "Box3",
          "createDate" : "2017-11-15T00:00:00",
          "itemNumber" : "Item1",
          "address" : "sampleAddress12",
        },
        "sort" : [
          1510704000000
        ]
      },
      {
        "_index" : "box_info_store",
        "_type" : "boxes",
        "_id" : "MGjQLWkBIJk0ORjdwX0M",
        "_score" : null,
        "_source" : {
          "transactionID" : "60ab66cf24c9924f562bf1a2b5d92305d0a6",
          "boxNumber" : "Box3",
          "createDate" : "2016-04-03T00:00:00",
          "itemNumber" : "Item1",
          "address" : "sampleAddress321",
        },
        "sort" : [
          1459641600000
        ]
      },
      ..........
      ..........
      ..........
      {
        "_index" : "box_info_store",
        "_type" : "boxes",
        "_id" : "AGjRLWkBIJk0ORjdK4CJ",
        "_score" : null,
        "_source" : {
          "transactionID" : "3087e106244f6247a5290fb21ce64254529c",
          "boxNumber" : "Box3",
          "createDate" : "1996-02-16T00:00:00",
          "itemNumber" : "Item1",
          "address" : "sampleAddress4324",
        },
        "sort" : [
          824428800000
        ]
      }
    ]
  },
  "aggregations" : {
    "unique_transactions" : {
      "doc_count_error_upper_bound" : 0,
      "sum_other_doc_count" : 16,
      "buckets" : [
        {
          "key" : "3087e106244f6247a5290fb21ce64254529c",
          "doc_count" : 6
        },
        {
          "key" : "27c5f3422f4482495d29e7b2c15c0e311743",
          "doc_count" : 5
        },
        {
          "key" : "c40e53212e74e24bf02a5bd2b134cf92bffb",
          "doc_count" : 5
        }
      ]
    }
  }
}

【问题讨论】:

    标签: elasticsearch kibana


    【解决方案1】:

    您使用的大小:表示检索到的原始文档的数量。

    如果你的情况你需要做的是:

    1. 提及大小为 0 -> 这将不会返回原始文档
    2. 在聚合中包含一个 size 参数,它将返回唯一的 7 个 id。

      GET /box_info_store/boxes/_search?size=7 { “询问”: { “布尔”:{ “必须”: [ { “匹配”: { “boxNumber”:“Box3” } }, { “匹配”: { “项目编号”:“项目 1” } } ] } }, “种类”: [ { “创建日期”:{ “顺序”:“降序” } } ], “聚合”:{ “distinct_transactions”:{ “条款”:{ “字段”:“交易ID”, “尺寸”:7 } } } }

    编辑--------------------------------------

    首先触发这个查询

    GET /box_info_store/boxes/_search?size=0
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "boxNumber": "Box3"
              }
            },
            {
              "match": {
                "itemNumber": "Item1"
              }
            }
          ]
        }
      }
    }
    

    在这里您会找到与您的查询匹配的文档总数,您可以将其设置为 n 在此触发您的查询后,如下所示

    GET /box_info_store/boxes/_search?size=**n**
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "boxNumber": "Box3"
              }
            },
            {
              "match": {
                "itemNumber": "Item1"
              }
            }
          ]
        }
      },
      "sort": [
        {
          "createDate": {
            "order": "desc"
          }
        }
      ],
      "aggs": {
        "distinct_transactions": {
          "terms": {
            "field": "transactionID",
            "size": NUMBER_OF_UNIQUE_TRANSACTION_IDS_TO_BE_FETCHED
          }
        }
      }
    }
    

    【讨论】:

    • 感谢@gaurav9620,使用它我可以获得不同的事务ID及其doc_count。但不是与该 ID 相关的文档。我怎样才能得到这些文件呢。我需要再次调用并获取这些 ID 的文档吗?
    • 只需将顶部提到的大小从 0 增加到您要获取的文档数
    • 如果我将聚合大小设为 3,则返回 3 个 transactionID,这 3 个 transactionID 可以属于 n 个具有相同框和项目但地址不同的文档,我需要获取所有这些。因此我不会知道外部尺寸值。另外,如果我不给出外部大小值,那么它会返回所有带有 box3 和 item1 的文档,而不管这 3 个不同的事务 ID
    • 非常感谢您的更新,我已经更新了我的问题,请您进一步协助
    • Also I noticed, that when i do order by create date , the latest 3 transactionIDs, that i get are 3087e106244f6247a5290fb21ce64254529c 60ab66cf24c9924f562bf1a2b5d92305d0a6 f249e7bb2b35f24857288b9228cdc71ac520, but the aggregation query returns 3087e106244f6247a5290fb21ce64254529c 27c5f3422f4482495d29e7b2c15c0e311743 c40e53212e74e24bf02a5bd2b134cf92bffb, shouldn't the aggregation query return第一套?
    猜你喜欢
    • 2018-03-19
    • 2013-12-31
    • 1970-01-01
    • 1970-01-01
    • 2015-04-29
    • 2011-05-24
    • 2021-12-24
    • 2010-09-30
    相关资源
    最近更新 更多