Skip to main content

Full text search with TNTSearch in laravel


                       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
  • 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.







Comments

  1. 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.

    ReplyDelete
  2. The quality of your articles and contents is great.

    ReplyDelete

Post a Comment

Popular posts from this blog

Google blogger Ideas panel

Google blogger Ideas  I opened by blogger today, and..   I got this.  Google blogger Ideas  A panel suggesting a topic on which I can write my next blog. It's fetching unanswered question from web according to your previous post and topics. It was something, I was really looking for, after all it takes time to finding subject on which to write next and still being in the same niche.  Awesome feature Blogger! 

Apache : setup basic auth with apache in windows

Authentication is any process by which you verify that someone is who they claim they are. Authorization is any process by which someone is allowed to be where they want to go or to have information that they want to have. I will show here how to set up basic auth on the apache with windows. Pre-requests  Windows VPS Apache server ( That's it ) ( In windows it might be difficult to setup the Apache alone. So instead use something ling xampp , wamp or laragon .) RestClient (  I personally use the postman , but you can use your preferable client)  Windows VPS provider Steps  Enable the necessary modules in the Apache Create the password file Set the auth directives in the virtual host file. Verify basic auth. Enable the  necessary   modules  in the Apache Open the httpd.conf file in the apache's conf folder. httpd.conf file Enable the necessary modules to make the basic auth working. Necessary modules  mod_auth_basic

Firebase - update a spacific fields of single element of object of array in firestore

Firebase - update a spacific fields of single element of object of array in firestore  Its actully advisable to use map instead of array when ever it is possible. But, there are cetain cases where you don't have option to do so.  For example, you are directly saving the response from some outer source without any modification and they send you an array. In this case you will have array to work with. Firestore does not support array here is why  "bad things can happen if you have multiple clients all trying to update or delete array elements at specific indexes. In the past, Cloud Firestore addressed these issues by limiting what you can do with arrays " For more details information you can refer to Kato Richardson post Best Practices: Arrays in Firebase .  Firestore document having array [ used from stackoverflow question ] Suppose you have array of object something like shown in array. Now you want to update endTime field of the object on the index [1]