Every site which has an aim to provide information to the end user inform of blog or artilce has to have an efficient search. You can do it with sql query using %like% and where queries. But its not sufficient. Some time user don't have idea about the text of field you are using so it might not get information through your site. So in that case we need some kind of thing that can do full-text search.
For example, You write an article name " Google ".
Google is a search engine. Search engine is the program that helps to find the information we need from the database. Google is one of the best search engines in the world. ... ..
Now you are just searching with name field with pure mysql. So if someone search for google. it will get result but , if will search for search engine result will be null. Here you can use full-text-search engine to resolve this problem.
We have several options for full-text-search engine
- Algolia Search
- I have heard much about this and from others experience i can assume that it is very good service. But it is paid.
- Elastic Search
- It is also very good option. Its free and not service but a package. It runs over java so you have to install java and it use much resource, so if you plannig to use elastic search in production you might have to purchase other vps to make it work. Running both elastics search and hosting site from same server might make your site slow.( It does this on my local development pc having 4gb of ram)
- TNTSearch
- My favourite. This is my favourite package. It is free. Runs with pure php. Needs less resources and give good result if you set it up correclty according to you need.
Setup TNTSearch with your laravel project
- Install laravel-scout
- For detail about how to install laravel scout visit this link Setup Laravel scout
- Setup the tnt-search-scout driver
- It requries
- PHP >= 5.5
- PDO PHP Extension
- SQLite PHP Extension
- mbstring PHP Extension
- If you get any error during installation please check this you have fullfilled upper requiremntes.
- For detail about how to setup tnt serach driver visit this link Setup TNTsearch For Laravel
- Import the data with the scout
- After setting up scout and tnt search driver you need some data on which you can do search process.
- Laravel Documentation says's scout is solution for full-text search engine to your eloquent model
- Add the line use searchable in the model class you want to use for searching.class Abc extends Eloquent{use Searchable; .....}
- So scout will search on the every field of your model which you import for the purpose of search. For example, the posts model in which you store the title, tags and body of your post.
- For detail how to import the model visit this link Import data to search driver
- Querying
- Up to now you have setup every thing you are ready to search. So just in your route or controller method use search function on model like this$posts = App\Post::search('Star Trek')->get();
- This will search the words "start" , "trek", and "start trek" plus some more other words that are relavent to it in the search database of post table. So if you even use this word in body some where still it will be shown in the search result.
So now you can have your local search engine for your site.
Something usefull
Sometime using tntsearch you might get problems like when you search for the partial keyword you get no result. While search for the whole keyword you get result. You can solve this by setting up the fuzziness correctly.
Just a small correction: Algolia has a community edition that is free. It is extremely efficient and much better than TNT. But I still prefer TNT because the data remains on the server and not on Algolia's server.
ReplyDeleteThe quality of your articles and contents is great.
ReplyDeleteThank you, Thomas
Delete