从elasticsearch中的排序数据中获取带有_score的resultSearch

时间:2016-04-20 08:11:18

标签: elasticsearch

我想获得过滤和排序数据的示例数据。 我用'random_score'尝试了这个,这就是我的做法。

{
   "query": {
      "function_score": {
      "query": {
        "function_score": {
          "functions": [
            {
              "random_score": {
                "seed": 1
              }
            }
          ]
        }
      },
      "functions": [
        {
          "script_score": {
            "script": "if (_score.doubleValue() > 0.2) {return 1;} else {return 0;}"
          }
        }
      ],
      "boost_mode": "replace"
    }
   }
}

它只会产生1/5数据的_score 1.但是当我添加sort()查询时,

{
   "query": {
      "function_score": {
      "query": {
        "function_score": {
          "functions": [
            {
              "random_score": {
                "seed": 1
              }
            }
          ]
        }
      },
      "functions": [
        {
          "script_score": {
            "script": "if (_score.doubleValue() > 0.2) {return 1;} else {return 0;}"
          }
        }
      ],
      "boost_mode": "replace"
    }
   },
   "sort": 
      {
         "eventcnt": {
            "order": "desc"
         }
      }

}

_score变为null。它无法通过排序获得_random_score吗?

我应该如何获得_score为1的数据?我应该在客户端过滤得分为1个数据吗?

1 个答案:

答案 0 :(得分:1)

如果按字段值排序,则需要使用track_scores。更多详情here

为了完整起见:

{
  "track_scores": true,
  "query": {
    "function_score": {
      "query": {
        "function_score": {
          "functions": [
            {
              "random_score": {
                "seed": 1
              }
            }
          ]
        }
      },
      "functions": [
        {
          "script_score": {
            "script": "if (_score.doubleValue() > 0.2) {return 1;} else {return 0;}"
          }
        }
      ],
      "boost_mode": "replace"
    }
  },
  "sort": {
    "eventcnt": {
      "order": "desc"
    }
  }
}