Vincent Decaux
Vincent Decaux

Reputation: 10724

ElasticSearch 7 - combine filters

I use ES 7 and Laravel implementation, I want to combine a range and a term match, according to documentation, I did this :

    $items = $client->search([
        'index' => $instance->getSearchIndex(),
        'type' => $instance->getSearchType(),
        'body' => [
            'size' => 50,
            'query' => [
                'bool' => [
                    'must' => [
                        'multi_match' => [
                            'fields' => config('elasticsearch.fields'),
                            'query' => $query,
                        ],
                    ],
                    'filter' => [
                        'bool' => [
                            'must' => [
                                'range' => [
                                    'note' => [
                                        'gte' => config('elasticsearch.note_minimum')
                                    ]
                                ],
                                'term' => [
                                    'type_video_id' => 5
                                ],
                            ],
                        ],
                    ],
                ]
            ],
        ],
    ]);

And got this error :

"parsing_exception","reason":"[range] malformed query, expected [END_OBJECT] but found [FIELD_NAME]

I only found documentation and examples for ES 2 about combining queries, did something change ?

I want my query to match the fields, and be filtered according to filter.

Upvotes: 1

Views: 169

Answers (2)

Val
Val

Reputation: 217554

Here is the right way to do this:

$items = $client->search([
    'index' => $instance->getSearchIndex(),
    'type' => $instance->getSearchType(),
    'body' => [
        'size' => 50,
        'query' => [
            'bool' => [
                'must' => [
                    'multi_match' => [
                        'fields' => config('elasticsearch.fields'),
                        'query' => $query,
                    ]
                ],
                'filter' => [
                    [
                        'range' => [
                            'note' => [
                                'gte' => config('elasticsearch.note_minimum')
                            ]
                        ]
                    ],
                    [
                        'term' => [
                            'type_video_id' => 5
                        ]
                    ]
                ]
            ]
        ]
    ]
]);

Upvotes: 2

Joe Zack
Joe Zack

Reputation: 3308

I don't have a way to test this, but I'm seeing a couple brackets where you need curlies. Also, don't you need you need $ before those "config"?

{
  "query" => {
    "bool" => {
      "must" => [
        {
          "multi_match" => {
            "query" => $query,
            "fields" => $config('elasticsearch.fields')
          }
        }
      ],
      "filter" => {
        {
          "range" => {
            "note" => {
              "gte" => $config('elasticsearch.note_minimum')
            }
          }
        },
        {
          "term" => {
            "type_video_id" => {
              "value" => "5"
            }
          }
        }
      }
    }
  }
}

If this doesn't work, can you paste what the string looks like after your variables get rendered?

Upvotes: 0

Related Questions