Full-text base-model search
Search through your SQL models with the power of Elasticsearch.
Quick Search
Section titled “Quick Search”Perform quick and easy full-text searches with search():
User::search('loves espressos');Search for the phrase
loves espressosacross all fields and return the baseUsermodels
Advanced Search
Section titled “Advanced Search”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}Index-Model vs Base-Model Results
Section titled “Index-Model vs Base-Model Results”- 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:”1. Use asBase()
Section titled “1. Use asBase()”- 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”Use: paginateBase()
Section titled “Use: paginateBase()”- 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();Examples:
Section titled “Examples:”1. Basic Term Search
Section titled “1. Basic Term Search”User::viaIndex()->searchTerm('nara') ->where('state','active') ->limit(3) ->getBase();User::viaIndex()->searchTerm('nara') ->where('state','active') ->limit(3) ->get();This searches all users who are
activefor the term ‘nara’ across all fields and return the top 3 results.
2. Phrase Search
Section titled “2. Phrase Search”User::viaIndex()->searchPhrase('Ice bathing') ->orderByDesc('created_at') ->limit(5) ->getBase();User::viaIndex()->searchPhrase('Ice bathing') ->orderByDesc('created_at') ->limit(5) ->get();Searches all fields for the phrase ‘Ice bathing’ and returns the 3 newest results. Phrases match exact words in order.
3. Boosting Terms fields
Section titled “3. Boosting Terms fields”User::viaIndex()->searchTerm('David',['first_name^3', 'last_name^2', 'bio']) ->first()->asBase();User::viaIndex()->searchTerm('David',['first_name^3', 'last_name^2', 'bio']) ->first();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.
4. Geolocation Filtering
Section titled “4. Geolocation Filtering”User::viaIndex()->where('status', 'active') ->filterGeoPoint('home.location', '5km', [0, 0]) ->orderByGeo('home.location',[0, 0]) ->getBase();User::viaIndex()->where('status', 'active') ->filterGeoPoint('home.location', '5km', [0, 0]) ->orderByGeo('home.location',[0, 0]) ->get();Finds all active users within a 5km radius from the coordinates [0, 0], ordering them from closest to farthest. Not kidding.
5. Regex Search
Section titled “5. Regex Search”User::viaIndex()->whereRegex('favourite_color', 'bl(ue)?(ack)?') ->getBase();User::viaIndex()->whereRegex('favourite_color', 'bl(ue)?(ack)?') ->get();Finds all users whose favourite color is blue or black.
6. Pagination
Section titled “6. Pagination”User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?') ->paginateBase(10);User::viaIndex()->whereRegex('favorite_color', 'bl(ue)?(ack)?') ->paginate(10);Paginate search results.
7. Nested Object Search
Section titled “7. Nested Object Search”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();User::viaIndex()->whereNestedObject('user_logs', function (Builder $query) { $query->where('user_logs.country', 'Norway') ->where('user_logs.created_at', '>=',Carbon::now()->modify('-1 week'));})->get();Searches nested user_logs for users who logged in from Norway within the last week. 🤯
8. Fuzzy Search
Section titled “8. Fuzzy Search”User::viaIndex()->searchFuzzy('quikc') ->orSearchFuzzy('brwn') ->orSearchFuzzy('foks') ->getBase();User::viaIndex()->searchFuzzy('quikc') ->orSearchFuzzy('brwn') ->orSearchFuzzy('foks') ->get();No spell, no problem. Search Fuzzy.
9. Highlighting Search Results
Section titled “9. Highlighting Search Results”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
10. Phrase prefix search
Section titled “10. Phrase prefix search”User::viaIndex()->searchPhrasePrefix('loves espr')->withHighlights()->get();Searches for the phrase prefix ‘loves espr’ across all fields and highlights where it was found.