<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by TNT Studio on Medium]]></title>
        <description><![CDATA[Stories by TNT Studio on Medium]]></description>
        <link>https://medium.com/@tntstudiohr?source=rss-e3b1a075c595------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*uc-WxuwbDLbxx6n5.png</url>
            <title>Stories by TNT Studio on Medium</title>
            <link>https://medium.com/@tntstudiohr?source=rss-e3b1a075c595------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 21:37:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tntstudiohr/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Did you mean functionality with Laravel Scout]]></title>
            <link>https://medium.com/@tntstudiohr/did-you-mean-functionality-with-laravel-scout-422b682c7df2?source=rss-e3b1a075c595------2</link>
            <guid isPermaLink="false">https://medium.com/p/422b682c7df2</guid>
            <category><![CDATA[full-text-search]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <category><![CDATA[laravel-scout]]></category>
            <category><![CDATA[tntsearch]]></category>
            <dc:creator><![CDATA[TNT Studio]]></dc:creator>
            <pubDate>Thu, 19 Jan 2017 00:54:06 GMT</pubDate>
            <atom:updated>2017-01-19T00:58:06.108Z</atom:updated>
            <content:encoded><![CDATA[<p>Did you mean functionality with Laravel Scout</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*tMkbiZ_hYWNaSlNk.jpg" /></figure><p>In this tutorial, we’ll show you how to create “Did you mean” functionality fast and easy. Data we’ll use will be a large city database consisting of more than 3 million cities. The idea is to show the correct city name in case your users misspell them. The list of cities comes from MaxMind, Inc and you can find the list <a href="https://www.maxmind.com/en/free-world-cities-database">here</a>. To understand what we are building take a look at the <a href="http://cities.tnt.studio/">demo page</a>. Setting up Laravel isn’t covered, but you can find plenty of tutorials on how to do this. Our project will depend on Laravel Scout and TNTSearch so lets install those dependencies:</p><p>composer require teamtnt/laravel-scout-tntsearch-driver</p><p>Add this to your providers array in config\app.php</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/20bf3b6fdfa3b14b67f2c99d4552afa6/href">https://medium.com/media/20bf3b6fdfa3b14b67f2c99d4552afa6/href</a></iframe><p>After that publish the Scout config file like:</p><p>php artisan vendor:publish --provider=&quot;Laravel\Scout\ScoutServiceProvider&quot;</p><p>And in your `.env, set TNTSearch as the default driver:</p><p>SCOUT_DRIVER=tntsearch</p><p>In config/scout.php set the storage_path</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ff72e5fc7144ca14aeaea95841ef7b5/href">https://medium.com/media/1ff72e5fc7144ca14aeaea95841ef7b5/href</a></iframe><p>Make sure this directory is writable. Lets start with a basic command that will download and import the list of cities to our database. The database migration and the model look as follows:</p><p>php artisan make:model City --migration</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6b3e2dabb42fb6bc38289098d4d2d796/href">https://medium.com/media/6b3e2dabb42fb6bc38289098d4d2d796/href</a></iframe><p>Add public $timestamps = false; to your City model since we don&#39;t need timestamps. Notice the column n_grams, this will help us later to achieve the did you mean functionality. Now, lets populate the table. Our dataset is a regular file where each line represents a city. A simple command will do the job:</p><p>php artisan make:command ImportCities</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c6368eb1e73cd75b5716bf379c2a5de6/href">https://medium.com/media/c6368eb1e73cd75b5716bf379c2a5de6/href</a></iframe><p>Don’t forget to register the command in app\Console\Kernel.php</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c703b4b3d76aa71575668074df4e78ea/href">https://medium.com/media/c703b4b3d76aa71575668074df4e78ea/href</a></iframe><p>The command will automatically download the file from Maxmind, unzip it and import the cities to our database. We’ll only import cities that have a population greater than 0. Once we have the cities in our database, let’s create the inverted index that scout will consume.</p><p>php artisan tntsearch:import App\City</p><p>Ok, now we can do a basic search for a city:</p><p>App\City::serach(&#39;Berlin&#39;)-&gt;get()</p><p>Although this works fine, it’s still not able to fetch a city if you make a typo. Let’s take care of this.</p><p>php artisan make:command CreateCityTrigrams</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/05e88b26dfa21262f855d10f8b05c5c2/href">https://medium.com/media/05e88b26dfa21262f855d10f8b05c5c2/href</a></iframe><p>This creates the index of trigrams that we’ll query if we don’t find anything in our cities.index</p><p>Our CityController.php looks like:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b1017ee13c0137bc4059bbec55191a63/href">https://medium.com/media/b1017ee13c0137bc4059bbec55191a63/href</a></iframe><h3>How does it work</h3><p>We threw a lot of code at you above. Although this will work perfectly it’s also important that you understand why it’s working and what the logic behind is. Lets, for example, take the city “Berlin”. Now, if we break it into trigrams we become the following:</p><p>__b _be ber erl rli lin in_ n__</p><p>Let’s assume you make a typo and instead of Berlin you type “Berln”. The trigrams now look like:</p><p>__b _be ber erl rln ln_ n__</p><p>As you can see, 5 trigrams match the trigrams from the correct form and only 2 don’t match.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*KjAB-7MJhhNk60sG.jpg" /></figure><p>Each trigram set of the correct form is stored in cityngrams.index. If we query the index with a mistyped word &quot;berln&quot; well get pretty accurate results since 5 matches were found. It will also return some other results that match the trigrams so we&#39;ll have to do one more step to get the best suggestions possible. The levenshtein distance between two words will simply tell us how many characters we need to change to get the same word. So the levenshtein distance between &quot;Berlin&quot; and &quot;Berln&quot; is 1 because we only need to add an &quot;i&quot; for the words to match. So we&#39;ll sort our result from our cityngrams.index by levenshtein distance and return the suggestions to the user. Pretty simple but powerful, isn&#39;t it?</p><p>The front end of the <a href="http://cities.tnt.studio/">demo page</a> is build with <a href="https://facebook.github.io/react/">react</a>. In the upcoming months will create an on-line course on how to get started with React and Laravel so make sure to subscribe to our newsletter on our site. Also, follow us on twitter <a href="https://twitter.com/nticaric">@nticaric</a> and <a href="https://twitter.com/sasatokic">@sasatokic</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=422b682c7df2" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Solving the search problem with Laravel and TNTSearch]]></title>
            <link>https://medium.com/@tntstudiohr/solving-the-search-problem-with-laravel-and-tntsearch-dfa16aa9173b?source=rss-e3b1a075c595------2</link>
            <guid isPermaLink="false">https://medium.com/p/dfa16aa9173b</guid>
            <category><![CDATA[tntsearch]]></category>
            <category><![CDATA[php]]></category>
            <category><![CDATA[laravel]]></category>
            <dc:creator><![CDATA[TNT Studio]]></dc:creator>
            <pubDate>Wed, 13 Apr 2016 15:58:49 GMT</pubDate>
            <atom:updated>2016-04-13T15:58:49.336Z</atom:updated>
            <content:encoded><![CDATA[<p>Adding search functionality to your project can sometimes be tiresome. It is often required to search against large text documents where the database <em>LIKE </em>operator simply isn’t enough. It cannot handle cases where you don’t write the exact search query. The query “%Laravel — The PHP Framework%” is not the same as “%The PHP Framework — Laravel%” and often you won’t get any results for such queries. It’s not even worth discussing how much MySQL <em>LIKE</em> operator performance sucks.</p><p>You might argue that databases like MySQL have full text search support and it shouldn’t be to hard to use it. Well, you might be right, but this approach has also its down sides. If you’re not using the latest version of MySQL, you’ll see that there is no full text search support for InnoDB tables. If you have support, you won’t be able to search on multiple columns. The match queries are a little bit complicated and they don’t get you good results. Of course there are great solutions out there to handle full text search, most famous ones being Elasticsearch and Sphinx, but often they seem like just an overkill especially for “smaller” projects.</p><p>What’s the solution then you may ask? Luckily, there is a package written in pure PHP that does the job for you. You might be wondering how good or fast pure PHP can be. Well, to comfort you here are some numbers. To build up a <a href="http://tntsearch.tntstudio.us/">demo page</a> for the search which has 57000 Tv Shows and 130000 Actors and all is searchable:</p><pre>Indexing: 30sec</pre><pre>Fetching results: 0.001 - 0.02sec (on a MacBook Pro computer depending on search query)</pre><p>As you can see the fetching is extremely fast and since the package is using state of the art relevance algorithms the relevance is much higher than in other PHP solutions. The performance of the package is great because the index is built on top of a sqlite database which is an extension written in C and comes default with PHP. And you cannot beat C :)</p><p>In this tutorial we’ll show how simple it is to build a documentation with full text search support. PHPUnit has great documentation, but it is missing the search functionality. Our task in this tutorial is to implement it.</p><p>For the documentation layout we’ll be useing <a href="http://getuikit.com/">http://getuikit.com/</a>. It’s as they claim a lightweight and modular front-end framework for developing fast and powerful web interfaces.</p><p>Before we can build an inverted index we need to somehow get the phpunit documentation. Our approach here is to simply scrape the latest version of the docs. We’ll create a laravel command that will be responsible for scraping the phpunit site. After the scraping we’ll create the index.</p><p>Let’s create a file and call it <em>IndexDocumentation.php</em>. It will be placed in<br><em>app/Console/Commands</em></p><p>The command name will be <em>docs:index</em> so that we later can run</p><pre> <em>php artisan docs:index</em></pre><p>The same command name is used to index the official Laravel documentation btw :)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2d2e87d22c9146082e2d11b3fd524732/href">https://medium.com/media/2d2e87d22c9146082e2d11b3fd524732/href</a></iframe><p>Scraping the phpunit site is very simple and can be done with one function. What we’re doing here is that we’re going to the <a href="http://phpunit.de/">phpunit.de</a> site, taking the table of contest and saving it to index.hml. Each chapter is saved to its own html file. All of the documentation is saved to resources/docs/</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2c8a36572f76fd780eda9ee4060f84a6/href">https://medium.com/media/2c8a36572f76fd780eda9ee4060f84a6/href</a></iframe><p>Once we have scraped all of the documentation, it’s time to build the inverted index. An inverted index stores all of the words that are contained in the documents and their positions. For example it tells us that the words “Martin Fowler” can be found only in <em>writing-tests-for-phpunit.html</em>. Here’s a definition from wikipedia:</p><blockquote>In computer science, an inverted index (also referred to as postings file or inverted file) is an index data structure storing a mapping from content, such as words or numbers, to its locations in a database file, or in a document or a set of documents (named in contrast to a Forward Index, which maps from documents to content).</blockquote><p>Using <a href="https://github.com/teamtnt/tntsearch">TNTSearch</a>, building the index is trivial. We need to provide it with some basic configuration. The <em>storage</em> directive represents where the index will be stored. The driver is <em>filesystem</em> since it can also read from a database, but what we want here is the files that we downloaded before. The <em>location</em> directive looks for the documentation files. <em>Exclude</em> and <em>extension </em>directives<em> </em>are self explanatory.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b3bee500e98a775a4cf1c18f616aab7e/href">https://medium.com/media/b3bee500e98a775a4cf1c18f616aab7e/href</a></iframe><p>The command is now complete and we can run <em>php artisan docs:index</em>. This will create an index that we can query against.</p><h4>Searching the index</h4><p>Now that we have the index it won’t be a problem to search for relevant documentation. The frontend part will have twitter’s <a href="https://twitter.github.io/typeahead.js/">typeahead plugin</a> and on every keypress it will hit the server asking for search results. The route that we’ll hit is<em> /search </em>and it will dispatch our request to the <em>DocsController.php</em>. The search method looks like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/299101e41c19372486bc9b7024094ccb/href">https://medium.com/media/299101e41c19372486bc9b7024094ccb/href</a></iframe><p>A handy feature is the <em>asYouType</em> property, which means it will return results even for single letters. The only thing left is to prepare the output for the frontend:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/62d50329e9a1675d61e4688f589ff3c7/href">https://medium.com/media/62d50329e9a1675d61e4688f589ff3c7/href</a></iframe><p>There are couple of helper function like <em>highlight</em> or <em>snippet</em> which lets you better format the output.</p><p>A working example of this documentation can be found <a href="http://phpunit.tntstudio.us/">here</a> or if you want another showcase take a look <a href="http://tntsearch.tntstudio.us/">here</a>. If you like the package, don’t forget to star it on <a href="https://github.com/teamtnt/tntsearch">github</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=dfa16aa9173b" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>