elasticsearch 使用太多字词元素优化ES查询

oknwwptz  于 2022-12-11  发布在  ElasticSearch
关注(0)|答案(1)|浏览(114)

我们正在处理一个包含数十亿条记录的数据集,目前所有数据都保存在ElasticSearch中,所有查询和聚合都使用ElasticSearch执行。
简化的查询主体如下所示,我们将设备id放在terms中,然后将它们与should进行合并,以避免terms的1024限制,terms元素的总数高达100,000,现在变得非常慢。

  1. {
  2. "_source": {
  3. "excludes": [
  4. "raw_msg"
  5. ]
  6. },
  7. "query": {
  8. "filter": {
  9. "bool": {
  10. "must": [
  11. {
  12. "range": {
  13. "create_ms": {
  14. "gte": 1664985600000,
  15. "lte": 1665071999999
  16. }
  17. }
  18. }
  19. ],
  20. "should": [
  21. {
  22. "terms": {
  23. "device_id": [
  24. "1328871",
  25. "1328899",
  26. "1328898",
  27. "1328934",
  28. "1328919",
  29. "1328976",
  30. "1328977",
  31. "1328879",
  32. "1328910",
  33. "1328902",
  34. ... # more values, since terms not support values more than 1024, wen concate all of them with should
  35. ]
  36. }
  37. },
  38. {
  39. "terms": {
  40. "device_id": [
  41. "1428871",
  42. "1428899",
  43. "1428898",
  44. "1428934",
  45. "1428919",
  46. "1428976",
  47. "1428977",
  48. "1428879",
  49. "1428910",
  50. "1428902",
  51. ...
  52. ]
  53. }
  54. },
  55. ... # concate more terms until all of the 100,000 values are included
  56. ],
  57. "minimum_should_match": 1
  58. }
  59. }
  60. },
  61. "aggs": {
  62. "create_ms": {
  63. "date_histogram": {
  64. "field": "create_ms",
  65. "interval": "hour",
  66. }
  67. }
  68. },
  69. "size": 0}

我的问题是,有没有办法优化这个案例?或者有没有更好的选择来做这种搜索?
实时或接近实时是必须的,其它发动机也可以接受。
数据的简化模式:

  1. "id" : {
  2. "type" : "long"
  3. },
  4. "content" : {
  5. "type" : "text"
  6. },
  7. "device_id" : {
  8. "type" : "keyword"
  9. },
  10. "create_ms" : {
  11. "type" : "date"
  12. },
  13. ... # more field
rdrgkggo

rdrgkggo1#

您可以将术语查询与术语查找结合使用,以指定更大的值列表,如下所示
将您的ID存储在ID为“device_ids”的特定文档中

  1. "should": [
  2. {
  3. "terms": {
  4. "device_id": {
  5. "index": "your-index-name",
  6. "id": "device_ids",
  7. "path": "field-name"
  8. }
  9. }
  10. }
  11. ]

相关问题