具有constant_score的elasticsearch bool节点

时间:2016-06-15 10:49:41

标签: elasticsearch

我试图用elasticsearch 2.3累积得分

我希望使用publishEnd IS NULL和booking = true获得常量分数。

之后我想根据品牌/型号/燃料获得不同的得分

这是我的搜索参数:

@api.multi
def main_val(self)

我想在结果上增加权重,而不是过滤它们。

对于持续的分数过滤是可以的,但我在为某些品牌增加重量方面遇到了问题。

添加bool节点时出现此错误:

  

parse_exception:无法解析搜索源。预期的字段名称,但得到[START_OBJECT]

谢谢,

菲尔。

修改 感谢以色列,这是正确的参数:

$params = [
        'index' => 'app',
        'type' => 'ad',
        'body' => [
            "from" => 0,
            "size" => 12,
            'query' => [
                "constant_score" => [
                    "filter" => [
                        "and" => [
                            ["missing" => [ "field" => "publishEnd" ]],
                            ["term" => ['booked' => 0]],
                        ],
                    ],
                ],
                'bool' =>  [
                    'filter' => [
                        ["term" => ['brand' => 'renault']],
                    ],
                ],
            ],
        ],
    ];

1 个答案:

答案 0 :(得分:1)

你得到这个例外,因为你在同一级别使用“constant_score”和“bool”。相反,你需要将它们包含在“必须”或“应该”在一个“bool”下。结果应该是这样的:

"query": {
    "bool": {            
    "must": [
       {"constant_score": {
       "filter": {
           "and": {
               "filters": [
                  {"missing": {
                     "field": "publishEnd"
                  }},
                  {"term": {
                     "booked": 0
                  }}
               ]
           }
       }
    }},
    {"bool": {
        "filter": {
            "term": {
               "brand": "renault"
            }
        }
    }}
    ]    
  }
}