Algolia: searching multiple indices in Laravel

谁说我不能喝 提交于 2019-12-11 05:45:54

问题


I would like to set up algolia to search multiple indices in Laravel, using something simple like this.

Route::get('/search/{query}', function ($query) {
$queries = [
    [
        'indexName' => 'movies_index',
        'query' => $query,
        'hitsPerPage' => 3
    ],
    [
        'indexName' => 'directors',
        'query' => $query,
        'hitsPerPage' => 3,
    ],
    [
        'indexName' => 'screenwriters',
        'query' => $query,
        'hitsPerPage' => 10
    ]
];



var_dump($queries);});

But I'm a beginner in laravel and algolia as well so I'm not entirely sure how to go on about it.


回答1:


As per the Algolia Docs states:

// perform 3 queries in a single API call:
//  - 1st query targets index `categories`
//  - 2nd and 3rd queries target index `products`

$queries = [
  [
    'indexName' => 'categories',
    'query' => $myQueryString,
    'hitsPerPage' => 3
  ],
  [
    'indexName' => 'products',
    'query' => $myQueryString,
    'hitsPerPage' => 3,
    'facetFilters' => 'promotion'
  ],
  [
    'indexName' => 'products',
    'query' => $myQueryString,
    'hitsPerPage' => 10
  ]
];

$results = $client->multipleQueries($queries);

var_dump($results['results']);

Don't forget to initialize your $client first:

$client = new \AlgoliaSearch\Client('APP_ID', 'APP_KEY');


来源:https://stackoverflow.com/questions/49846344/algolia-searching-multiple-indices-in-laravel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!