Vivek Choudhary
Vivek Choudhary

Reputation: 684

Elastic Search query for Laravel framework

I'm totally new to elastic search. I'm trying to fetch records from elastic search.

$params = Array
(
    [index] => elastic_index
    [size] => 10
    [body] => Array
        (
            [query] => Array
                (
                    [bool] => Array
                        (
                            [must] => Array
                                (
                                    [match] => Array
                                        (
                                            [uniqueField] => uniqueValue
                                        )

                                )

                            [filter] => Array
                                (
                                    [range] => Array
                                        (
                                            [intValue] => Array
                                                (
                                                    [lte] => 120
                                                )

                                        )

                                    [terms] => Array
                                        (
                                            [type] => Array
                                                (
                                                    [0] => some
                                                    [1] => values
                                                 
                                                )

                                        )

                                )

                        )

                )

        )

)
$data = $Elasticsearch\ClientBuilderObject->search($params);

So the query without range filter is working but when I'm trying to filter it then I'm getting the following error.

{"error":{"root_cause":[{"type":"parsing_exception","reason":"[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":97}],"type":"parsing_exception","reason":"[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]","line":1,"col":97},"status":400}

I checked the docs but couldn't find any solution. I will be thankful if you give me some tips here or some website where I can solve my problem.

Upvotes: 0

Views: 556

Answers (1)

lockonzero
lockonzero

Reputation: 126

I am guessing that you are building an array like this?

"filter" => [
    "range" => [
        "intValue" => [
            "lte" => 120
        ]
    ],
    "terms" => [
        "type" => ["some", "values"]
    ]
]

Can you try changing it to this?

"filter" => [
    [
        "range" => [
            "intValue" => [
                "lte" => 120
            ]
        ]
    ],
    [
        "terms" => [
            "type" => ["some", "values"]
        ]
    ]
]

Upvotes: 2

Related Questions