Vivek Sangani
Vivek Sangani

Reputation: 401

Elastic Search with laravel for multiple fields

I have integrated elastic search in laravel with help of below tutorial.

https://appdividend.com/2018/06/30/laravel-elasticsearch-tutorial-example/

According to this tutorial search with single fields is working fine. i.e.

// Article table has column 'title','body' and 'tags'.

 Route::get('/search', function() {
      $articles = Article::searchByQuery(['match' => ['title' => 'Test']]);
      return $articles;
 });

But i want to search with multiple column values like 'title' ,'body' etc.

Anyone suggest an idea how to search with multiple column?

Upvotes: 2

Views: 1383

Answers (2)

Manoj Patel
Manoj Patel

Reputation: 11

$array = modelName::searchByQuery(
    [
        'multi_match' => [
            'query' => 'search param', 
            'type' =>  "cross_fields",
            'fields' => [ 
                'field1',
                'field2',
                'more fields'
            ]
        ]
    ]
);

Upvotes: -2

Lucas Luan de Melo
Lucas Luan de Melo

Reputation: 152

You can use the multimatch query, above is some sample how this can be done.

GET _search
{
    "query": {
        "bool": {
            "must": {
              "multi_match" : {
                "query": "stuff you want to seach", 
                "type":  "cross_fields",
                "fields": [ 
                   "title^10",
                   "body^9",
                   "tags^8"
                ]
              }
            }
        }
    }
}

Upvotes: 5

Related Questions