ElasticSearch功能评分查询

时间:2016-10-03 17:44:00

标签: elasticsearch elasticsearch-2.0

以下是我的function_score查询。我想为产品质量更好的文档提供额外的分数。

但搜索响应中的_score始终为0.我缺少什么? THX。

当我删除bool查询并仅使用术语过滤器替换它时,分数不为零。我猜这是关于查询bool但无法弄清楚为什么。

Elasticsearch版本是2.4

$AdminPwd = ConvertTo-SecureString -string "foobar2016!" -AsPlainText -Force
$UserPwd = ConvertTo-SecureString -string "foobar2016!" -AsPlainText -Force

$NewAdmin = "NewAdmin"

New-LocalUser -Name $NewAdmin -FullName "New Admin" -Password $AdminPwd
Add-LocalGroupMember -Member $NewAdmin -Group "Administrators"

Get-LocalUser | ForEach-Object {

    if ($_.Name -ne $NewAdmin)
    {
        $_ | Set-LocalUser -Password $UserPwd
        $_ | Disable-LocalUser 
    }
}

1 个答案:

答案 0 :(得分:4)

正如@Val所说。

bool.filter为所有文档指定0分,因为未指定评分查询(link)。

如果您需要分数,可以在查询中添加"must": {"match_all": {}}match_all会将1.0分配给所有文档(link)。

以下是match_all的查询:

{
  "from": 0,
  "size": 20,
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": { 
              "match_all": {}
          },
          "filter": [
            {
              "bool": {
                "should": {
                  "terms": {
                    "categories.category1Id": [
                      63
                    ]
                  }
                }
              }
            }
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "updatedDate": {
              "origin": "2016-10-03 05:10:18",
              "scale": "0.5h",
              "decay": 0.1,
              "offset": "1h"
            }
          }
        },
        {
          "filter": {
            "term": {
              "productQuality": "EXCELLENT"
            }
          },
          "weight": 7
        },
        {
          "filter": {
            "term": {
              "productQuality": "HIGH"
            }
          },
          "weight": 5
        },
        {
          "filter": {
            "term": {
              "productQuality": "MEDIUM"
            }
          },
          "weight": 3
        },
        {
          "filter": {
            "term": {
              "productQuality": "LOW"
            }
          },
          "weight": 1
        }
      ],
      "score_mode": "sum"
    }
  }
}