使用Elasticsearch DSL检索calendarItems.minNights第一个值大于2的文档。

huangapple go评论123阅读模式
英文:

Fetching documents with calendarItems.minNights first value greater than 2 using Elasticsearch DSL

问题

我想学习如何使用Elasticsearch DSL获取calendarItems.minNights字段的第一个值大于2的文档。数据结构如下:

在上述数据结构中,我想要检索第一个calendarItems.minNights值大于2的文档。这应该是Elasticsearch DSL的查询?

谢谢。

数据:

  1. {
  2. "_index": "list",
  3. "_id": "5150",
  4. "_version": 1,
  5. "_seq_no": 0,
  6. "_primary_term": 1,
  7. "found": true,
  8. "_source": {
  9. "id": 5150,
  10. "title": "test title",
  11. "calendarItems": [
  12. {
  13. "actualDate": "2023-07-10T00:00:00+03:00",
  14. "price": 458,
  15. "minNights": 4,
  16. "status": "booked",
  17. "reason": null,
  18. "isBlock": null
  19. },
  20. {
  21. "actualDate": "2023-07-11T00:00:00+03:00",
  22. "price": 458,
  23. "minNights": 2,
  24. "status": "available",
  25. "reason": null,
  26. "isBlock": null
  27. },
  28. {
  29. "actualDate": "2023-07-12T00:00:00+03:00",
  30. "price": 458,
  31. "minNights": 2,
  32. "status": "available",
  33. "reason": null,
  34. "isBlock": null
  35. },
  36. {
  37. "actualDate": "2023-07-12T00:00:00+03:00",
  38. "price": 458,
  39. "minNights": 2,
  40. "status": "booked",
  41. "reason": null,
  42. "isBlock": null
  43. }
  44. ]
  45. }
  46. }

查询:

  1. "query": {
  2. "bool": {
  3. "must": [
  4. {
  5. "nested": {
  6. "path": "calendarItems",
  7. "query": {
  8. "bool": {
  9. "must": [
  10. {
  11. "range": {
  12. "calendarItems.actualDate": {
  13. "gte": "2023-07-07T00:00:00+03:00",
  14. "lte": "2023-07-12T23:59:59+03:00"
  15. }
  16. }
  17. },
  18. {
  19. "term": {
  20. "calendarItems.status": "available"
  21. }
  22. },
  23. {
  24. "script": {
  25. "script": {
  26. "source": "2 >= doc['calendarItems.minNights'].value",
  27. "lang": "painless"
  28. }
  29. }
  30. }
  31. ]
  32. }
  33. }
  34. }
  35. }
  36. ],
  37. "must_not": [
  38. {
  39. "nested": {
  40. "path": "calendarItems",
  41. "query": {
  42. "bool": {
  43. "must": [
  44. {
  45. "term": {
  46. "calendarItems.status": "booked"
  47. }
  48. },
  49. {
  50. "range": {
  51. "calendarItems.actualDate": {
  52. "gte": "2023-07-07T00:00:00+03:00",
  53. "lte": "2023-07-12T23:59:59+03:00"
  54. }
  55. }
  56. }
  57. ]
  58. }
  59. }
  60. }
  61. }
  62. ]
  63. }
  64. }
英文:

I would like to learn how to fetch documents with the first value of the calendarItems.minNights field greater than 2 using Elasticsearch DSL. The data structure looks like this:

In the above data structure, I want to retrieve documents where the first value of calendarItems.minNights is greater than 2. What should be the Elasticsearch DSL query for this?

Thank you.

Data

  1. {
  2. "_index": "list",
  3. "_id": "5150",
  4. "_version": 1,
  5. "_seq_no": 0,
  6. "_primary_term": 1,
  7. "found": true,
  8. "_source": {
  9. "id": 5150,
  10. "title": "test title",
  11. "calendarItems": [
  12. {
  13. "actualDate": "2023-07-10T00:00:00+03:00",
  14. "price": 458,
  15. "minNights": 4,
  16. "status": "booked",
  17. "reason": null,
  18. "isBlock": null
  19. },
  20. {
  21. "actualDate": "2023-07-11T00:00:00+03:00",
  22. "price": 458,
  23. "minNights": 2,
  24. "status": "available",
  25. "reason": null,
  26. "isBlock": null
  27. },
  28. {
  29. "actualDate": "2023-07-12T00:00:00+03:00",
  30. "price": 458,
  31. "minNights": 2,
  32. "status": "available",
  33. "reason": null,
  34. "isBlock": null
  35. },
  36. {
  37. "actualDate": "2023-07-12T00:00:00+03:00",
  38. "price": 458,
  39. "minNights": 2,
  40. "status": "booked",
  41. "reason": null,
  42. "isBlock": null
  43. }
  44. ]
  45. }
  46. }

Query

  1. "query": {
  2. "bool": {
  3. "must": [
  4. {
  5. "nested": {
  6. "path": "calendarItems",
  7. "query": {
  8. "bool": {
  9. "must": [
  10. {
  11. "range": {
  12. "calendarItems.actualDate": {
  13. "gte": "2023-07-07T00:00:00+03:00",
  14. "lte": "2023-07-12T23:59:59+03:00"
  15. }
  16. }
  17. },
  18. {
  19. "term": {
  20. "calendarItems.status": "available"
  21. }
  22. },
  23. {
  24. "script": {
  25. "script": {
  26. "source": "2 >= doc['calendarItems.minNights'].value",
  27. "lang": "painless"
  28. }
  29. }
  30. }
  31. ]
  32. }
  33. }
  34. }
  35. }
  36. ],
  37. "must_not": [
  38. {
  39. "nested": {
  40. "path": "calendarItems",
  41. "query": {
  42. "bool": {
  43. "must": [
  44. {
  45. "term": {
  46. "calendarItems.status": "booked"
  47. }
  48. },
  49. {
  50. "range": {
  51. "calendarItems.actualDate": {
  52. "gte": "2023-07-07T00:00:00+03:00",
  53. "lte": "2023-07-12T23:59:59+03:00"
  54. }
  55. }
  56. }
  57. ]
  58. }
  59. }
  60. }
  61. }
  62. ]
  63. }
  64. }

答案1

得分: 0

我解决了这个问题。

英文:

I solved this

  1. {
  2. "query": {
  3. "bool": {
  4. "must": [
  5. {
  6. "nested": {
  7. "path": "calendarItems",
  8. "query": {
  9. "bool": {
  10. "must": [
  11. {
  12. "range": {
  13. "calendarItems.actualDate": {
  14. "gte": "2023-07-07T00:00:00+03:00",
  15. "lte": "2023-07-15T23:59:59+03:00"
  16. }
  17. }
  18. },
  19. {
  20. "term": {
  21. "calendarItems.status": "available"
  22. }
  23. }
  24. ]
  25. }
  26. }
  27. }
  28. },
  29. {
  30. "nested": {
  31. "path": "calendarItems",
  32. "query": {
  33. "bool": {
  34. "must": [
  35. {
  36. "range": {
  37. "calendarItems.actualDate": {
  38. "gte": "2023-07-07T00:00:00+03:00",
  39. "lte": "2023-07-07T23:59:59+03:00"
  40. }
  41. }
  42. },
  43. {
  44. "term": {
  45. "calendarItems.status": "available"
  46. }
  47. },
  48. {
  49. "script": {
  50. "script": {
  51. "source": "8 >= doc['calendarItems.minNights'].value",
  52. "lang": "painless"
  53. }
  54. }
  55. }
  56. ]
  57. }
  58. }
  59. }
  60. }
  61. ],
  62. "must_not": [
  63. {
  64. "nested": {
  65. "path": "calendarItems",
  66. "query": {
  67. "bool": {
  68. "must": [
  69. {
  70. "term": {
  71. "calendarItems.status": "booked"
  72. }
  73. },
  74. {
  75. "range": {
  76. "calendarItems.actualDate": {
  77. "gte": "2023-07-07T00:00:00+03:00",
  78. "lte": "2023-07-15T23:59:59+03:00"
  79. }
  80. }
  81. }
  82. ]
  83. }
  84. }
  85. }
  86. }
  87. ]
  88. }
  89. }

}

huangapple
  • 本文由 发表于 2023年6月19日 15:10:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76504349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定