Getting "parsing_exception","reason":"[terms] unknown token [END_ARRAY] trying to do multiple field filter

I would like to create a query which matches based on a phrase query but also filters based off of multiple fields. I am using PHP API syntax, but its pretty obvious how to translate the syntax. This is my query:

$searchParams = [
    'index' => 'main_search',
    'type' => 'my_type',
    'explain' => false,
    'body' => [
        'from' => $offset,
        'size' => $perPage * 2,
        'query' => [
            'function_score' => [
                'query' => [
                    'bool' => [
                        'filter' =>[
                            'bool' =>[
                                'must' => [
                                    ['terms' => ['price' => $price]],
                                    ['terms' => ['media_type' => $mediaType]],
                                ]
                            ]
                        ],
                        'should' => [
                            'multi_match' => [
                                'query' => $query,
                                'type' => 'most_fields',
                                'fields' => ['author','category','title', 'tag_name_list', 'tag_name_list^3', 'title.stemmed', 'title.shingles', 'title.autocomplete', 'tag_name_list.stemmed^3', 'tag_name_list.shingles^3'],
                            ]
                        ]
                    ]
                ],
                'script_score' => [
                    'script' => [
                        'lang' => 'painless',
                        'inline' => "
                                    double avgRating = (doc['num_ratings'].value  == 0) ? 2 : doc['avg_rating'].value;
                                    long numRatings = (doc['num_ratings'].value == null) ? 0 : doc['num_ratings'].value;
                                    double rating = ((numRatings * 2.0/(numRatings + 2.0)) * avgRating) + ((2.0/(numRatings + 2.0)) * 2.0);
                                    rating * _score"
                    ]
                ]
            ],
        ]
    ]
];

When I was using one filter (without the bool inside of the filter) it was working. Now it is not. The input $mediaType variable and the $price variable are arrays of values that I want to filter by. Anyone know why I am getting "parsing_exception","reason":"[terms] unknown token [END_ARRAY] trying to do multiple field filter after [media_type] ?

Its because $mediaType was wrapped in another array. So it was [[0 => val1, 1 => val2, 2 => val3]] instead of [0 => val1, 1 => val2, 2 => val3]

1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.