Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
    Guides
    Libraries and tools
    Guides
    Libraries and tools
  • Log in
  • Create account
  • Server-side search

    How to search on the server using Laravel Scout with Algolia Scout Extend.

    Searching

    You can search in aSearchable class using thesearch method.The search method accepts a single string to search in yourSearchable class index:
    PHP
    $articles = Article::search('Star Trek')->get();
    You can also search withpagination,and work withsoft deleted models.To learn more, see theLaravel Scout documentation.

    Search with numerical filters

    You can use thewhere method to filter your search results.With Scout Extended, this method shares the same API as the Laravel Query Builder,so you can filter results either with a comparison or a numerical range:
    PHP
    $articles = Article::search('Star Trek')->where('views','>',100)->get();$articles = Article::search('Star Trek')->where('created_at','>=',now()->subDays(7))->get();$articles = Article::search('Star Trek')->where('views',100)->get();// views = 100
    Thewhere method supports the following operators:<,<=,=,!=,>=,>.

    Search with numerical range filters

    ThewhereBetween method filters results for which the provided field falls between the given range:
    PHP
    $products = Products::search('Star Trek')    ->whereBetween('price', [100,200])    ->get();

    Search with multiple filter values

    ThewhereIn method filters results for which the value of the provided field is part of the given array:
    PHP
    $products = Products::search('Star Trek')    ->whereIn('id', [1,2])    ->get();

    Search with locational filters

    ThearoundLatLng method adds a geolocation parameter to the search request.You can define a point with its coordinates.This method is syntactic sugar, and you can use the methodwith to specify more location details such asaroundLatLng andaroundRadius.
    PHP
    $articles = Article::search('query')    ->aroundLatLng(48.8588536,2.3125377)    ->get();

    Search with custom search parameters

    Thewith method gives you complete access to customize searchAPI parameters.
    PHP
    $articles = Article::search('Star Trek')    ->with([        'hitsPerPage' => 30,        'filters' => 'attribute:value',        'typoTolerance' => false,    ])->get();

    Retrieve the number of hits

    Thecount method returns the number of hits the query matches:
    PHP
    $count = Article::search('Star Trek')->count();

    Retrieve hit metadata

    You can use thescoutMetadata method to retrieve an array with more information about any hit.The key_highlightResult holds all the highlighted attributes.By default, Algolia highlights all the searchable attributes.You can change this with thearoundLatLng parameter.
    PHP
    $metadata = Article::search('Star Trek')->get()->first()->scoutMetadata();$highlightResult = $metadata['_highlightResult'];
    If you set thegetRankingInfo search parameter totrue, thegetRankingInfo holds detailed ranking information.It lets you see whichranking criteria played a role in selecting each model:
    PHP
    $metadata = Article::search('Star Trek')->get()->first()->scoutMetadata();$rankingInfo = $metadata['_rankingInfo'];

    Was this page helpful?

    Client-side searchGet status overview
    ⌘I

    [8]ページ先頭

    ©2009-2025 Movatter.jp