track_total_hits: true

获取超过1w条数据 需要加上 "track_total_hits":true ,不然只能显示出9999条

Boosting加权查询

参考:官方文档

  1. GET indexName/_search?pretty
  2. {
  3. "query": {
  4. "boosting": {
  5. "positive": {
  6. "term": {
  7. "text": "apple"
  8. }
  9. },
  10. "negative": {
  11. "term": {
  12. "text": "pie tart fruit crumble tree"
  13. }
  14. },
  15. "negative_boost": 0.5
  16. }
  17. }
  18. }

Constant 固定分值

  1. GET /_search
  2. {
  3. "query": {
  4. "constant_score": {
  5. "filter": {
  6. "term": {
  7. "user.id": "kimchy"
  8. }
  9. },
  10. "boost": 1.2
  11. }
  12. }
  13. }

disjuction最大分值查询

  1. GET /_search
  2. {
  3. "query": {
  4. "dis_max": {
  5. "queries": [
  6. {
  7. "term": {
  8. "title": "Quick pets"
  9. }
  10. },
  11. {
  12. "term": {
  13. "body": "Quick pets"
  14. }
  15. }
  16. ],
  17. "tie_breaker": 0.7 #相关性打分
  18. }
  19. }
  20. }

function_score 函数式打分查询

  1. GET /_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": { "match_all": {} },
  6. "boost": "5",
  7. "random_score": {},
  8. "boost_mode": "multiply" # 乘法运算
  9. }
  10. }
  11. }
  1. GET /_search?pretty
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "match_all": {}
  7. },
  8. "boost": "5", # 整个查询的权重
  9. "functions": [
  10. {
  11. "filter": {
  12. "match": {
  13. "test": "bar"
  14. }
  15. },
  16. "random_score": {},
  17. "weight": 23
  18. },
  19. {
  20. "filter": {
  21. "match": {
  22. "test": "cat"
  23. }
  24. },
  25. "weight": 42
  26. }
  27. ],
  28. "max_boost": 42,
  29. "score_mode": "max", # multiplysumavgfirstmaxmin
  30. "boost_mode": "multiply", # multiplyreplacesumavgmaxmin
  31. "min_score": 42
  32. }
  33. }
  34. }

要使 min_score 起作用,需要对查询返回的所有文档进行评分,然后一一过滤掉。

The function_score query provides several types of score functions.

script score

  1. GET /_search?pretty
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "match": {
  7. "message": "elasticsearch"
  8. }
  9. },
  10. "script_score": {
  11. "script": {
  12. "source": "Math.log(2 + doc['my-int'].value)"
  13. }
  14. }
  15. }
  16. }
  17. }

script_score 函数允许包装另一个查询并自定义它的评分,可以选择使用脚本表达式从文档中的其他数字字段值派生的计算。