FS全栈计划

Elasticsearch计算Array中元素的个数

2018-10-29  本文已影响3人  MaxZing

N久没动手写东西了,原来的博客估计被我玩废了。这次讲一个开发中遇到的问题。
现在有一个document如下:

{
  "_index": "test_index_8088",
  "_type": "_doc",
  "_id": "fa358749ec18e",
  "_version": 1,
  "_score": 8.956308,
  "_source": {
    "collectGroupId": "126",
    "createTime": "2018-10-28 21:05:45",
    "products": [
      "6Gq9wI-cAAgAATtm48QX17zNzBinKTwIkMfZClrLxnGxx01Tcui",
      "7Mrm-iPgAAgAASFBdrR7bYVCPK6acU-_9fam7K5ZuzLUysdNuii",
      "9b_L759yAAgAASNTxVpC3L9CV_cKoRgFeuzH6LWbgPrndqO539t",
      "A9jPFpaeAAgAAQby28p89RsysknPafv3NUOspVoORTTUsvCOowj",
      "AFZJwTu0AAgAAR3wO_EfpkYEIrG_cS0hOdLmIRgOoIZH05xqRcE",
      "AcPVcTbmAAgAAQldtAxISNo3eGe7sXNa66x7S6BKYit9uNjtTQE",
      "CG8Exa1AAAgAAS-UnPEWxT_yHLNs_2IYVpJwcdm1wQLlEF99Pu_",
      "ClARKM7eAAgAAQgWWJJtsAYDuY5CVWi9ibuZeOMqHzWZBedrtZT",
      "DCcGF8cSAAgAAQb4RZko-mE6EsBtRrCS4qawDc4Nf9YShXzogiG",
      "G7r4dRkYAAgAAREwvmZvI-FXFBxKpEaFTDQfbCIaK8qp2NVYM1b",
      "HiXaAldyAAgAAS6bVaEFohXAb_jJmP0zhUewaUFeIeERd3N5Krg",
      "KhZNddRIAAgAARzY4EA8UHf9R0vRuwKE9Nq6Fbm8Q-RV55RRnwL",
      "LQgqBgrsAAgAARPY2i52soLNgeg46mRfLoxBptoGsl2VVGMABrL",
      "My9WHr7KAAgAASyQu5ku6ltNl7Hz3yNUHOxQxRKZLuOiVmqWGHQ",
      "NS2uRagaAAgAAQ37ddZ0IxihFxyZPXRbUggd3f6h14iMo8uH7P8",
      "Oi8FzUv-AAgAAS8gPhAFyteFxb3W3n0O2Y5P-ZGkk7egm81gA6v",
      "PsxpwxQaAAgAATsSsAkc3IuyqAcI2hM2XXWayrN-ejcD8WXYtC4",
      "PxAmpUOcAAgAARwLvIEseJ-JmIOA45HcrLJtZX3UR2jnkU-5j6n",
      "Q5g-uCnMAAgAASNziwp8ovaMcUpBUYHPKrKqZkj5pTFmOD6Nyyz",
      "RE9oJOugAAgAARRWsGEt5TDTRJbR1jpGCB6OAZp0c7AYUrZogRb",
      "TkdtqYWcAAgAARDqZ_J9QuV_CR88qMCpe7BJWL-oe05yYPejl7g",
      "UC0y4BBoAAgAATsjzkQE1CAXxwPAESvbXcmo_9ygqwpHHvvAG6P",
      "VAQ2teJwAAgAAQWq1pZHGY0PmTtsN9FvHFeynhxs9WIk9o9kTDz",
      "YEXHe2jYAAgAASu3QqB2dcVB6OKO7lsMCOSVxuTtJbHO7pnUnwA",
      "ZjpMJ0-QAAgAAQ1vAXErFv-XfzkrkuWX2lhT14nDgRcwPuc26_M",
      "_D-xqoEcAAgAAT5dxKQT7fvNuRQEG7h7HDroOF5rdCKMgJpqIcp",
      "_T8K0LzIAAgAATY4kPRXDfM9094X4_PCXFKjwdlMhONRb8ZcmEH",
      "_tyqjLHMAAgAAQBb_kA7FOsaketh7sOauAmtTxGpobm26qIW07C",
      "cRo_BhFWAAgAAT43m5AvxO9N18ZEQ_JfAlZd7N-GAkn5JaxEWAD",
      "cr31zA9GAAgAAQ-6sEA4O2pgQplW_kq7gHoGn3mkMSi2Kjz9bhx",
      "dKAAV6G2AAgAATfskVpYQA4uPPEo3PfxkUxqD5Pssjl1ZmxfSOb",
      "e91zvaNQAAgAAQvRwoQgPrahjvK8g4lKr6fkLRh1DVPdWk_cRXS",
      "eP-es5liAAgAATs3NuQ-77zfUq3mBJyD6Zg-nFMZan9InCLfxHX",
      "eaw9J4oCAAgAASQ1r8QN9Ca9TJFGUsYnYoSKZD3QpqjqYSwIaaE",
      "foyca7GqAAgAARC0mQQSh9W3xdz9zJBw98KBn00SXmSxKadEFRp",
      "gcYfQvCEAAgAASg4xo5OfJ6gDnYTcIEveKlN7TU3jykaFGdyM8h",
      "hJsqPPP0AAgAAQsAi8Zs0az6cc3yAZxm-sBKOu_ZzDNtZ2iSxn3",
      "jKeNbhNKAAgAAT6L7pEXhY8pISiTvn4NBfP2kSqK1O8-LvsPZvV",
      "kScrbEaUAAgAAROihYBn4DWiF1k-pin2LAi3l6QehnBgObYuzB2",
      "l_t5JKsaAAgAAQQUNwEjxYD1CLACmoCe0lgm2QFLzsFTjxmJEeL",
      "lbY0AYoKAAgAARcV18EkMEPRLdWQBYLYEe3Jymf3Qenkdnf2Kii",
      "mEluiu10AAgAAStluSQiBEH4MJwp1Opert0LX0SqQVjn5NoCzuo",
      "nGzMA6bSAAgAATsPrMQS3hO-Kp3JcxMptBxvCwil4ixZSfU0z3j",
      "n_Iho9pSAAgAATbCVJEQGFS9FZS6J2mAww1sedHSMiuALETUDBK",
      "oXbd1Qy-AAgAAQrWDHRMaygEdrZCc0fzYMBgcJ2LHTRbvD3lNij",
      "q1L8AY10AAgAASRzsQhpscmj7RrPMcQhV6ns_WulrUh0QcocJf0",
      "sQJwR3saAAgAAQ6OEbEXy9Wncv292_6kGKsvNGlgZBUspFW2y9A",
      "t6YDLucMAAgAATDezxhu5rgyxkk0gGRivjVIzxHm9g5RIp655cX",
      "uEMMe8M4AAgAAQlWmjk4hvweEQ6nKJGPSS0LpVHKV9yd4PnVgru",
      "vlIqggKWAAgAAQuco_5dCQ8CKiF7Mt0qLP9aTeTfuQYB3CM_RiA"
    ],
    "allowAdd": 0,
    "dataType": 0,
    "defaultValue": "2018-10-28 13:05:45",
    "propertyId": 145,
    "propertyName": "被爬虫扒窃的开始时间",
    "sortCode": 1,
    "stageId": 187,
    "thickness": 378
  }
}

ps: 参数为测试使用的参数,部分是针对博文修改

现在有一个头疼的事情是,boss想知道这个index下的的产品数量,因为products项是个数组,查官方聚合搜索文档,貌似没啥答案,只能求助万能的Stack Overflow。

https://stackoverflow.com/questions/49250267/how-to-get-array-count-of-nested-object-in-elastic-search

抱歉我又搬砖了

那么用这种方式的queryDSL的写法现在已经换了,用上述的document举个栗子

# kibana DEV Tools脚本
GET test_index_8088/_doc/_search
{
  "size":0,
  "aggs": {
    "productNum": {
      "sum": {
        "script": {
          "source": "params._source.products.size()"
        }
      }
    }
  }
}

对的,"inline" 换成了"source",
这统计的是test_index_8088 index下的所有product数量,针对特定的要求,可以添加query环节。后面我也会看看聚合,还有script脚本查询。有机会分享给各位。

转载请注明出处:https://www.jianshu.com/p/3bc797dcfa62
如有错误,请务必指正。谢谢
我的博客:https://micorochio.github.io/

上一篇下一篇

猜你喜欢

热点阅读