elasticsearch 查询语句中must与should一起使用,should失效

矫情吗;* 2022-12-06 05:24 554阅读 0赞

这是写过最短的一个博客了。

当查询语句中包含must与should的时候,发现should失效。那我们就把must改成must_not,逻辑再取反就可以了(无意中发现)。

希望能帮到大家,真TM有趣~

贴俩真实语句给大家看看把。

失败语句:

  1. {
  2. "from" : 0,
  3. "size" : 100,
  4. "query" : {
  5. "bool" : {
  6. "must" : [
  7. {
  8. "range" : {
  9. "report_time" : {
  10. "from" : "2020-04-10 17:38:15",
  11. "to" : null,
  12. "include_lower" : true,
  13. "include_upper" : true,
  14. "boost" : 1.0
  15. }
  16. }
  17. }
  18. ],
  19. "should" : [
  20. {
  21. "term" : {
  22. "event_type.keyword" : {
  23. "value" : "xx",
  24. "boost" : 1.0
  25. }
  26. }
  27. },
  28. {
  29. "term" : {
  30. "event_type.keyword" : {
  31. "value" : "yy",
  32. "boost" : 1.0
  33. }
  34. }
  35. }
  36. ],
  37. "adjust_pure_negative" : true,
  38. "boost" : 1.0
  39. }
  40. },
  41. "sort" : [
  42. {
  43. "report_time" : {
  44. "order" : "desc"
  45. }
  46. }
  47. ]
  48. }

成功语句:

  1. {
  2. "from" : 0,
  3. "size" : 100,
  4. "query" : {
  5. "bool" : {
  6. "must_not" : [
  7. {
  8. "range" : {
  9. "report_time" : {
  10. "from" : null,
  11. "to" : 2020-04-10 17:38:15,
  12. "include_lower" : true,
  13. "include_upper" : true,
  14. "boost" : 1.0
  15. }
  16. }
  17. }
  18. ],
  19. "should" : [
  20. {
  21. "term" : {
  22. "event_type.keyword" : {
  23. "value" : "xx",
  24. "boost" : 1.0
  25. }
  26. }
  27. },
  28. {
  29. "term" : {
  30. "event_type.keyword" : {
  31. "value" : "yy",
  32. "boost" : 1.0
  33. }
  34. }
  35. }
  36. ],
  37. "adjust_pure_negative" : true,
  38. "boost" : 1.0
  39. }
  40. },
  41. "sort" : [
  42. {
  43. "report_time" : {
  44. "order" : "desc"
  45. }
  46. }
  47. ]
  48. }

发表评论

表情:
评论列表 (有 0 条评论,554人围观)

还没有评论,来说两句吧...

相关阅读