30_Not_quite_not.asciidoc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. [[not-quite-not]]
  2. === Not Quite Not
  3. 在互联网上搜索 “Apple”,返回的结果很可能是一个公司、水果和各种食谱。((("relevance", "controlling", "must_not clause in bool query")))((("bool query", "must_not clause")))我们可以在 `bool` 查询中用 `must_not` 语句来排除像 `pie` 、 `tart` 、 `crumble` 和 `tree` 这样的词,从而将查询结果的范围缩小至只返回与 “Apple” (苹果)公司相关的结果:
  4. [source,json]
  5. -------------------------------
  6. GET /_search
  7. {
  8. "query": {
  9. "bool": {
  10. "must": {
  11. "match": {
  12. "text": "apple"
  13. }
  14. },
  15. "must_not": {
  16. "match": {
  17. "text": "pie tart fruit crumble tree"
  18. }
  19. }
  20. }
  21. }
  22. }
  23. -------------------------------
  24. 但谁又敢保证在排除 `tree` 或 `crumble` 这种词后,不会错失一个与苹果公司特别相关的文档呢?有时, `must_not` 条件会过于严格。
  25. [[boosting-query]]
  26. ==== 权重提升查询
  27. {ref}/query-dsl-boosting-query.html[`boosting` 查询]
  28. 恰恰能解决这个问题。((("boosting query")))((("relevance", "controlling", "boosting query")))它仍然允许我们将关于水果或甜点的结果包括到结果中,但是使它们降级——即降低它们原来可能应有的排名:
  29. [source,json]
  30. -------------------------------
  31. GET /_search
  32. {
  33. "query": {
  34. "boosting": {
  35. "positive": {
  36. "match": {
  37. "text": "apple"
  38. }
  39. },
  40. "negative": {
  41. "match": {
  42. "text": "pie tart fruit crumble tree"
  43. }
  44. },
  45. "negative_boost": 0.5
  46. }
  47. }
  48. }
  49. -------------------------------
  50. 它接受 `positive` 和 `negative` 查询。((("positive query and negative query (in boosting query)")))只有那些匹配 `positive` 查询的文档罗列出来,对于那些同时还匹配 `negative` 查询的文档将通过文档的原始 `_score` ((("negative_boost")))与 `negative_boost` 相乘的方式降级后的结果。
  51. 为了达到效果, `negative_boost` 的值必须小于 `1.0` 。在这个示例中,所有包含负向词的文档评分 `_score` 都会减半。