Skip to content

Full-text base-model search

Search through your SQL models with the power of Elasticsearch.

Perform quick and easy full-text searches with search():

User::search('loves espressos');

Search for the phrase loves espressos across all fields and return the base User models

Search with full control using the viaIndex() method:

BaseModel::viaIndex()->{build your ES Eloquent query}->first();
BaseModel::viaIndex()->{build your ES Eloquent query}->get();
BaseModel::viaIndex()->{build your ES Eloquent query}->paginate();
BaseModel::viaIndex()->{build your ES Eloquent query}->avg('orders');
BaseModel::viaIndex()->{build your ES Eloquent query}->distinct();
BaseModel::viaIndex()->{build your ES Eloquent query}->{etc}
  • Since the viaIndex() taps into the Index Model, the results will be instances of Index Model, not the Base Model.
  • This can be useful for display purposes, such as highlighting embedded fields.
  • However, in most cases you’ll need to return and work with the Base Model

To search and return results as Base-Models:

Section titled “To search and return results as Base-Models:”
  • Simply chain ->asBase() at the end of your query:
User::viaIndex()->searchTerm('david')->limit(3)->get()->asBase();
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->get()->asBase();
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->first()->asBase();
2. Use getBase() instead of get()->asBase()
Section titled “2. Use getBase() instead of get()->asBase()”
User::viaIndex()->searchTerm('david')->orderByDesc('created_at')->getBase();
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')->getBase();

To search and paginate results as Base-Models

Section titled “To search and paginate results as Base-Models”
  • Complete the query string with ->paginateBase()
// Returns a pagination instance of Users ✔️:
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')
->paginateBase(10);
// Returns a pagination instance of IndexedUsers:
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')
->paginate(10);
// Will not paginate ❌ (but will at least return a collection of 10 Users):
User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')
->paginate(10)->asBase();

User::viaIndex()->searchTerm('nara')
->where('state','active')
->limit(3)
->getBase();

This searches all users who are active for the term ‘nara’ across all fields and return the top 3 results.

User::viaIndex()->searchPhrase('Ice bathing')
->orderByDesc('created_at')
->limit(5)
->getBase();

Searches all fields for the phrase ‘Ice bathing’ and returns the 3 newest results. Phrases match exact words in order.

User::viaIndex()->searchTerm('David',['first_name^3', 'last_name^2', 'bio'])
->first()->asBase();

Searches for the term ‘David’, boosts the first_name field by 3, last_name by 2, and also checks the bio field. Returns first result with the highest score.

User::viaIndex()->where('status', 'active')
->filterGeoPoint('home.location', '5km', [0, 0])
->orderByGeo('home.location',[0, 0])
->getBase();

Finds all active users within a 5km radius from the coordinates [0, 0], ordering them from closest to farthest. Not kidding.

User::viaIndex()->whereRegex('favourite_color', 'bl(ue)?(ack)?')
->getBase();

Finds all users whose favourite color is blue or black.

User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?')
->paginateBase(10);

Paginate search results.

User::viaIndex()->whereNestedObject('user_logs', function (Builder $query) {
$query->where('user_logs.country', 'Norway')
->where('user_logs.created_at', '>=',Carbon::now()->modify('-1 week'));
})->getBase();

Searches nested user_logs for users who logged in from Norway within the last week. 🤯

User::viaIndex()->searchFuzzy('quikc')
->orSearchFuzzy('brwn')
->orSearchFuzzy('foks')
->getBase();

No spell, no problem. Search Fuzzy.

User::viaIndex()->searchTerm('espresso')->withHighlights()->get();

Searches for ‘espresso’ across all fields and highlights where it was found.

Note: Returning the base model will not highlight the results

User::viaIndex()->searchPhrasePrefix('loves espr')->withHighlights()->get();

Searches for the phrase prefix ‘loves espr’ across all fields and highlights where it was found.