Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch bool node with constant_score

I'm trying to cumulate score with elasticsearch 2.3

I want a constant score with publishEnd IS NULL and booking = true.

And after that I want different scoring depending on brand/model/fuel

Here is my search params :

$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']],
                    ],
                ],
            ],
        ],
    ];

I want to add weight on results, not filtering them.

It's ok for constant score filtering, but I got problems about adding weight for certain brands.

I got this error when I add bool node :

parse_exception: failed to parse search source. expected field name but got [START_OBJECT]

Thanks,

Phil.

Edit Thanks to israelst, here is the correct parameters :

    $params = [
        'index' => 'app',
        'type' => 'ad',
        'body' => [
            "from" => 0,
            "size" => 12,
            'query' => [
                "bool" => [
                    'should' => [
                        "constant_score" => [
                            "filter" => [
                                "and" => [
                                    ["missing" => [ "field" => "publishEnd" ]],
                                    ["term" => ['booked' => 0]],
                                ],
                            ],
                        ],
                        'filter' => [
                            ["term" => ['brand' => 'renault']],
                        ],
                    ],
                ],
            ],
        ],
    ];
like image 620
Philippe R. Avatar asked Apr 12 '26 16:04

Philippe R.


1 Answers

You get this exception because you are using "constant_score" and "bool" in the same level. Instead, you need to wrap them inside a "must" or "should" under one "bool". The outcome should be something like:

"query": {
    "bool": {            
    "must": [
       {"constant_score": {
       "filter": {
           "and": {
               "filters": [
                  {"missing": {
                     "field": "publishEnd"
                  }},
                  {"term": {
                     "booked": 0
                  }}
               ]
           }
       }
    }},
    {"bool": {
        "filter": {
            "term": {
               "brand": "renault"
            }
        }
    }}
    ]    
  }
}
like image 193
israelst Avatar answered Apr 16 '26 02:04

israelst



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!