如何获取正确的数据?

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

How to get the right data?

问题

我不理解为什么返回了ID = 498。

我的目标是过滤掉ID = 498,我不知道如何做到这一点。

英文:

I have this ElasticSearch Query for ES ,version:2.2:

  1. {
  2. "query": {
  3. "bool": {
  4. "must": [
  5. {
  6. "term": {
  7. "companyId": 3211002
  8. }
  9. },
  10. {
  11. "bool": {
  12. "should": [
  13. {
  14. "term": {
  15. "hrId": 1005031
  16. }
  17. },
  18. {
  19. "terms": {
  20. "manager": [
  21. 1005031
  22. ]
  23. }
  24. },
  25. {
  26. "bool": {
  27. "must": [
  28. {
  29. "exists": {
  30. "field": "manager"
  31. }
  32. },
  33. {
  34. "terms": {
  35. "manager": []
  36. }
  37. }
  38. ]
  39. }
  40. },
  41. {
  42. "bool": {
  43. "must_not": {
  44. "exists": {
  45. "field": "manager"
  46. }
  47. }
  48. }
  49. }
  50. ],
  51. "minimum_should_match": "1"
  52. }
  53. },
  54. {
  55. "terms": {
  56. "status": [
  57. "0"
  58. ]
  59. }
  60. },
  61. {
  62. "bool": {
  63. "should": [
  64. {
  65. "bool": {
  66. "must_not": {
  67. "exists": {
  68. "field": "approvals"
  69. }
  70. }
  71. }
  72. },
  73. {
  74. "nested": {
  75. "query": {
  76. "terms": {
  77. "approvals.approvalStatus": [
  78. 1
  79. ]
  80. }
  81. },
  82. "path": "approvals"
  83. }
  84. }
  85. ],
  86. "minimum_should_match": "1"
  87. }
  88. }
  89. ]
  90. }
  91. },
  92. "sort": [
  93. {
  94. "createTime": {
  95. "order": "desc"
  96. }
  97. }
  98. ]
  99. }

But,ES gives me back this:

  1. {
  2. "hits": {
  3. "total": 2,
  4. "max_score": null,
  5. "hits": [
  6. {
  7. "_index": "recruitment",
  8. "_type": "requirement",
  9. "_id": "501",
  10. "_score": null,
  11. "_source": {
  12. "id": 501,
  13. "companyId": 3211002,
  14. "hrId": 1005031,
  15. "formId": 501,
  16. "requirementId": "Test0004",
  17. "positionTitle": "招聘需求004",
  18. "positionProperties": 1,
  19. "requirementCount": 3,
  20. "requirementType": 1,
  21. "reportTo": 1004651,
  22. "requirementTeam": 188384773,
  23. "requirementStatus": 0,
  24. "manager": [
  25. 1005031
  26. ],
  27. "status": 0,
  28. "createTime": 1685350126881,
  29. "hasAttachment": false
  30. },
  31. "sort": [
  32. 1685350126881
  33. ]
  34. },
  35. {
  36. "_index": "recruitment",
  37. "_type": "requirement",
  38. "_id": "498",
  39. "_score": null,
  40. "_source": {
  41. "id": 498,
  42. "companyId": 3211002,
  43. "hrId": 1004483,
  44. "formId": 498,
  45. "requirementId": "Test0002",
  46. "positionTitle": "审批测试1",
  47. "positionProperties": 2,
  48. "requirementCount": 2,
  49. "requirementStatus": 0,
  50. "status": 0,
  51. "createTime": 1685346243403,
  52. "hasAttachment": false,
  53. "approvals": [
  54. {
  55. "approvalStatus": 1,
  56. "nodes": [
  57. {
  58. "approvers": [
  59. {
  60. "approverStatus": 3,
  61. "id": 1005031
  62. }
  63. ],
  64. "nodeStatus": 3
  65. },
  66. {
  67. "approvers": [
  68. {
  69. "approverStatus": 1,
  70. "id": 1004789
  71. },
  72. {
  73. "approverStatus": 1,
  74. "id": 1004483
  75. }
  76. ],
  77. "nodeStatus": 1
  78. }
  79. ],
  80. "createTime": "2023-05-29T15:44:07+08:00",
  81. "hrId": 1004483,
  82. "id": 597
  83. }
  84. ]
  85. },
  86. "sort": [
  87. 1685346243403
  88. ]
  89. }
  90. ]
  91. }
  92. }

I don't understand why ID = 498 is returned.

My goal is to filter out id = 498, I don't know how to do that.

答案1

得分: 1

你应该将嵌套查询放在must_not内部,这样应该可以工作:

  1. GET test_hr/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "term": {
  8. "companyId": 3211002
  9. }
  10. },
  11. {
  12. "bool": {
  13. "should": [
  14. {
  15. "term": {
  16. "hrId": 1005031
  17. }
  18. },
  19. {
  20. "terms": {
  21. "manager": [
  22. 1005031
  23. ]
  24. }
  25. },
  26. {
  27. "bool": {
  28. "must": [
  29. {
  30. "exists": {
  31. "field": "manager"
  32. }
  33. },
  34. {
  35. "terms": {
  36. "manager": []
  37. }
  38. }
  39. ]
  40. }
  41. },
  42. {
  43. "bool": {
  44. "must_not": {
  45. "exists": {
  46. "field": "manager"
  47. }
  48. }
  49. }
  50. }
  51. ],
  52. "minimum_should_match": "1"
  53. }
  54. },
  55. {
  56. "terms": {
  57. "status": [
  58. "0"
  59. ]
  60. }
  61. },
  62. {
  63. "bool": {
  64. "should": [
  65. {
  66. "bool": {
  67. "must_not": {
  68. "nested": {
  69. "path": "approvals",
  70. "query": {
  71. "exists": {
  72. "field": "approvals"
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. ],
  80. "minimum_should_match": "1"
  81. }
  82. }
  83. ]
  84. }
  85. },
  86. "sort": [
  87. {
  88. "createTime": {
  89. "order": "desc"
  90. }
  91. }
  92. ]
  93. }

测试部分:

  1. PUT test_hr
  2. {
  3. "mappings": {
  4. "properties": {
  5. "approvals":{
  6. "type": "nested"
  7. }
  8. }
  9. }
  10. }
  11. PUT test_hr/_doc/501
  12. {
  13. "id": 501,
  14. "companyId": 3211002,
  15. "hrId": 1005031,
  16. "formId": 501,
  17. "requirementId": "Test0004",
  18. "positionTitle": "招聘需求004",
  19. "positionProperties": 1,
  20. "requirementCount": 3,
  21. "requirementType": 1,
  22. "reportTo": 1004651,
  23. "requirementTeam": 188384773,
  24. "requirementStatus": 0,
  25. "manager": [
  26. 1005031
  27. ],
  28. "status": 0,
  29. "createTime": 1685350126881,
  30. "hasAttachment": false
  31. }
  32. PUT test_hr/_doc/498
  33. {
  34. "id": 498,
  35. "companyId": 3211002,
  36. "hrId": 1004483,
  37. "formId": 498,
  38. "requirementId": "Test0002",
  39. "positionTitle": "审批测试1",
  40. "positionProperties": 2,
  41. "requirementCount": 2,
  42. "requirementStatus": 0,
  43. "status": 0,
  44. "createTime": 1685346243403,
  45. "hasAttachment": false,
  46. "approvals": [
  47. {
  48. "approvalStatus": 1,
  49. "nodes": [
  50. {
  51. "approvers": [
  52. {
  53. "approverStatus": 3,
  54. "id": 1005031
  55. }
  56. ],
  57. "nodeStatus": 3
  58. },
  59. {
  60. "approvers": [
  61. {
  62. "approverStatus": 1,
  63. "id": 1004789
  64. },
  65. {
  66. "approverStatus": 1,
  67. "id": 1004483
  68. }
  69. ],
  70. "nodeStatus": 1
  71. }
  72. ],
  73. "createTime": "2023-05-29T15:44:07+08:00",
  74. "hrId": 1004483,
  75. "id": 597
  76. }
  77. ]
  78. }

以下是两个不同查询的示例:

不工作的查询:

  1. # not working
  2. GET test_hr/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must_not": [
  7. {
  8. "exists": {
  9. "field": "approvals"
  10. }
  11. }
  12. ]
  13. }
  14. }
  15. }

工作的查询:

  1. # working
  2. GET test_hr/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must_not": [
  7. {
  8. "nested": {
  9. "path": "approvals",
  10. "query": {
  11. "exists": {
  12. "field": "approvals"
  13. }
  14. }
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }
英文:

You should add the nested query inside of a must_not, it should work:

  1. GET test_hr/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "term": {
  8. "companyId": 3211002
  9. }
  10. },
  11. {
  12. "bool": {
  13. "should": [
  14. {
  15. "term": {
  16. "hrId": 1005031
  17. }
  18. },
  19. {
  20. "terms": {
  21. "manager": [
  22. 1005031
  23. ]
  24. }
  25. },
  26. {
  27. "bool": {
  28. "must": [
  29. {
  30. "exists": {
  31. "field": "manager"
  32. }
  33. },
  34. {
  35. "terms": {
  36. "manager": []
  37. }
  38. }
  39. ]
  40. }
  41. },
  42. {
  43. "bool": {
  44. "must_not": {
  45. "exists": {
  46. "field": "manager"
  47. }
  48. }
  49. }
  50. }
  51. ],
  52. "minimum_should_match": "1"
  53. }
  54. },
  55. {
  56. "terms": {
  57. "status": [
  58. "0"
  59. ]
  60. }
  61. },
  62. {
  63. "bool": {
  64. "should": [
  65. {
  66. "bool": {
  67. "must_not": {
  68. "nested": {
  69. "path": "approvals",
  70. "query": {
  71. "exists": {
  72. "field": "approvals"
  73. }
  74. }
  75. }
  76. }
  77. }
  78. }
  79. ],
  80. "minimum_should_match": "1"
  81. }
  82. }
  83. ]
  84. }
  85. },
  86. "sort": [
  87. {
  88. "createTime": {
  89. "order": "desc"
  90. }
  91. }
  92. ]
  93. }

test

  1. PUT test_hr
  2. {
  3. "mappings": {
  4. "properties": {
  5. "approvals":{
  6. "type": "nested"
  7. }
  8. }
  9. }
  10. }
  11. PUT test_hr/_doc/501
  12. {
  13. "id": 501,
  14. "companyId": 3211002,
  15. "hrId": 1005031,
  16. "formId": 501,
  17. "requirementId": "Test0004",
  18. "positionTitle": "招聘需求004",
  19. "positionProperties": 1,
  20. "requirementCount": 3,
  21. "requirementType": 1,
  22. "reportTo": 1004651,
  23. "requirementTeam": 188384773,
  24. "requirementStatus": 0,
  25. "manager": [
  26. 1005031
  27. ],
  28. "status": 0,
  29. "createTime": 1685350126881,
  30. "hasAttachment": false
  31. }
  32. PUT test_hr/_doc/498
  33. {
  34. "id": 498,
  35. "companyId": 3211002,
  36. "hrId": 1004483,
  37. "formId": 498,
  38. "requirementId": "Test0002",
  39. "positionTitle": "审批测试1",
  40. "positionProperties": 2,
  41. "requirementCount": 2,
  42. "requirementStatus": 0,
  43. "status": 0,
  44. "createTime": 1685346243403,
  45. "hasAttachment": false,
  46. "approvals": [
  47. {
  48. "approvalStatus": 1,
  49. "nodes": [
  50. {
  51. "approvers": [
  52. {
  53. "approverStatus": 3,
  54. "id": 1005031
  55. }
  56. ],
  57. "nodeStatus": 3
  58. },
  59. {
  60. "approvers": [
  61. {
  62. "approverStatus": 1,
  63. "id": 1004789
  64. },
  65. {
  66. "approverStatus": 1,
  67. "id": 1004483
  68. }
  69. ],
  70. "nodeStatus": 1
  71. }
  72. ],
  73. "createTime": "2023-05-29T15:44:07+08:00",
  74. "hrId": 1004483,
  75. "id": 597
  76. }
  77. ]
  78. }

  1. #not working
  2. GET test_hr/_search
  3. {
  4. "query": {
  5. "bool": {
  6. "must_not": [
  7. {
  8. "exists": {
  9. "field": "approvals"
  10. }
  11. }
  12. ]
  13. }
  14. }
  15. }
  16. #working
  17. GET test_hr/_search
  18. {
  19. "query": {
  20. "bool": {
  21. "must_not": [
  22. {
  23. "nested": {
  24. "path": "approvals",
  25. "query": {
  26. "exists": {
  27. "field": "approvals"
  28. }
  29. }
  30. }
  31. }
  32. ]
  33. }
  34. }
  35. }

如何获取正确的数据?

huangapple
  • 本文由 发表于 2023年5月29日 20:21:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357334.html
匿名

发表评论

匿名网友

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

确定