You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

256 lines
10 KiB

10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
  1. ![Tom Lingham Laravel Searchy](http://tomlingham.com/github/header-searchy.png)
  2. ## Laravel 5+ Searchy
  3. ### Database Searching Made Easy
  4. Searchy is an; easy-to-use, light-weight, MySQL only, Laravel package that makes running user driven searches on data in your models simple and effective.
  5. It uses pseudo fuzzy searching and other weighted mechanics depending on the search driver that you have enabled.
  6. It requires no other software installed on your server (so can be a little slower than dedicated search programs) but can be set up and ready to go in minutes.
  7. #### !! Laravel 4 !!
  8. Looking for Laravel 4 compatible Searchy? Checkout the 1.0 branch :)
  9. https://github.com/TomLingham/Laravel-Searchy/tree/1.0
  10. ## Installation
  11. Add `"tom-lingham/searchy" : "2.*"` to your composer.json file under `require`:
  12. ```json
  13. "require": {
  14. "laravel/framework": "5.*",
  15. "tom-lingham/searchy" : "2.*"
  16. }
  17. ```
  18. Run `composer update` in your terminal to pull down the package into your vendors folder.
  19. Add the service provider to the `providers` array in Laravel's `./config/app.php` file:
  20. ```php
  21. TomLingham\Searchy\SearchyServiceProvider::class
  22. ```
  23. Add the Alias to the `aliases` array in Laravel's `./config/app.php` file if you want to have quick access to it in your application:
  24. ```php
  25. 'Searchy' => TomLingham\Searchy\Facades\Searchy::class
  26. ```
  27. ## Usage
  28. To use Searchy, you can take advantage of magic methods.
  29. If you are searching the name and email column/field of users in a `users` table you would, for example run:
  30. ```php
  31. $users = Searchy::users('name', 'email')->query('John Smith')->get();
  32. ```
  33. You can also write this as:
  34. ```php
  35. $users = Searchy::search('users')->fields('name', 'email')->query('John Smith')->get();
  36. ```
  37. In this case, pass the columns you want to search through to the `fields()` method.
  38. These examples both return an array of Objects containing your search results. You can use `getQuery()` instead of `get()` to return an instance of the Database Query Object in case you want to do further manipulation to the results:
  39. ```php
  40. $users = Searchy::search('users')->fields('name', 'email')->query('John Smith')
  41. ->getQuery()->having('relevance', '>', 20)->get();
  42. ```
  43. ### Limit on results returned
  44. To limit your results, you can use Laravel's built in DatabaseQuery Builder method and chain further methods to narrow your results.
  45. ```php
  46. // Only get the top 10 results
  47. $users = Searchy::search('users')->fields('name', 'email')->query('John Smith')
  48. ->getQuery()->limit(10)->get();
  49. ```
  50. ### Searching multiple Columns
  51. You can also add multiple arguments to the list of fields/columns to search by.
  52. For example, if you want to search the name, email address and username of a user, you might run:
  53. ```php
  54. $users = Searchy::users('name', 'email', 'username')->query('John Smith')->get();
  55. ```
  56. If you need to build your table list dynamically, you can also pass an array of fields instead as the first argument (All other following arguments will be ignored):
  57. ```php
  58. $users = Searchy::users(['name', 'email', 'username'])->query('John Smith')->get();
  59. ```
  60. ### Searching Joined/Concatenated Columns
  61. Sometimes you may want to leverage searches on concatenated column. For example, on a `first_name` and `last_name` field but you only want to run the one query. To do this can separate columns with a double colon:
  62. ```php
  63. $users = Searchy::users('first_name::last_name')->query('John Smith')->get();
  64. ```
  65. ### Soft Deleted Records
  66. By default soft deletes will not be included in your results. However, if you wish to include soft deleted records you can do so by adding the `withTrashed()` after specifying your table and fields;
  67. ```php
  68. Searchy::users('name')->withTrashed()->query('Batman')->get();
  69. ```
  70. ### Return only specific columns
  71. You can specify which columns to return in your search:
  72. ```php
  73. $users = Searchy::users('first_name::last_name')->query('John Smith')->select('first_name')->get();
  74. // Or you can swap those around...
  75. $users = Searchy::users('first_name::last_name')->select('first_name')->query('John Smith')->get();
  76. ```
  77. This will, however, also return the `relevance` aliased column regardless of what is entered here.
  78. ## How to get a Laravel Eloquent Collection
  79. Transforming the search results into a collection of Laravel Eloquent models is outside the scope of this project. However, an easy way to achieve this without hitting your database more than necessary is to use the Eloquent `fill()` method.
  80. ```php
  81. $users = collect(array_map(function($result) {
  82. return (new \App\User())->fill(get_object_vars($result));
  83. }, Searchy::users('name', 'email')->query('Andrew')->get()));
  84. ```
  85. All this does is map a function over the Searchy results and then creates a new instance of the User model and hydrates the model using the `fill()` method.
  86. Then once it has an array of all the Eloquent User models, it simply uses the Laravel `collect()` method to return the array as Laravel Collection which is the same as you would receive from querying the Laravel model directly. `get_object_vars()` is simply a PHP method to extract the accessible object variables as an associative array.
  87. ## Configuration
  88. You can publish the configuration file to your `app` directory and override the settings by running `php artisan vendor:publish` to copy the configuration to your config folder as `searchy.php`
  89. You can set the default driver to use for searches in the configuration file. Your options (at this stage) are: `fuzzy`, `simple` and `levenshtein`.
  90. You can also override these methods using the following syntax when running a search:
  91. ```php
  92. Searchy::driver('fuzzy')->users('name')->query('Batman')->get();
  93. ```
  94. ## Drivers
  95. Searchy takes advantage of 'Drivers' to handle matching various conditions of the fields you specify.
  96. Drivers are simply a specified group of 'Matchers' which match strings based on specific conditions.
  97. Currently there are only three drivers: Simple, Fuzzy and Levenshtein (Experimental).
  98. ### Simple Search Driver
  99. The Simple search driver only uses 3 matchers each with the relevant multipliers that best suited my testing environments.
  100. ```php
  101. protected $matchers = [
  102. 'TomLingham\Searchy\Matchers\ExactMatcher' => 100,
  103. 'TomLingham\Searchy\Matchers\StartOfStringMatcher' => 50,
  104. 'TomLingham\Searchy\Matchers\InStringMatcher' => 30,
  105. ];
  106. ```
  107. ### Fuzzy Search Driver
  108. The Fuzzy Search Driver is simply another group of matchers setup as follows. The multipliers are what I have used, but feel free to change these or roll your own driver with the same matchers and change the multipliers to suit.
  109. ```php
  110. protected $matchers = [
  111. 'TomLingham\Searchy\Matchers\ExactMatcher' => 100,
  112. 'TomLingham\Searchy\Matchers\StartOfStringMatcher' => 50,
  113. 'TomLingham\Searchy\Matchers\AcronymMatcher' => 42,
  114. 'TomLingham\Searchy\Matchers\ConsecutiveCharactersMatcher' => 40,
  115. 'TomLingham\Searchy\Matchers\StartOfWordsMatcher' => 35,
  116. 'TomLingham\Searchy\Matchers\StudlyCaseMatcher' => 32,
  117. 'TomLingham\Searchy\Matchers\InStringMatcher' => 30,
  118. 'TomLingham\Searchy\Matchers\TimesInStringMatcher' => 8,
  119. ];
  120. ```
  121. ### Levenshtein Search Driver (Experimental)
  122. The Levenshtein Search Driver uses the Levenshetein Distance to calculate the 'distance' between strings. It requires that you have a stored procedure in MySQL similar to the following `levenshtein( string1, string2 )`. There is an SQL file with a suitable function in the `res` folder - feel free to use this one.
  123. ```php
  124. protected $matchers = [
  125. 'TomLingham\Searchy\Matchers\LevenshteinMatcher' => 100
  126. ];
  127. ```
  128. ## Matchers
  129. ### ExactMatcher
  130. Matches an exact string and applies a high multiplier to bring any exact matches to the top.
  131. ### StartOfStringMatcher
  132. Matches Strings that begin with the search string.
  133. For example, a search for 'hel' would match; 'Hello World' or 'helping hand'
  134. ### AcronymMatcher
  135. Matches strings for Acronym 'like' matches but does NOT return Studly Case Matches
  136. For example, a search for 'fb' would match; 'foo bar' or 'Fred Brown' but not 'FreeBeer'.
  137. ### ConsecutiveCharactersMatcher
  138. Matches strings that include all the characters in the search relatively positioned within the string. It also calculates the percentage of characters in the string that are matched and applies the multiplier accordingly.
  139. For Example, a search for 'fba' would match; 'Foo Bar' or 'Afraid of bats', but not 'fabulous'
  140. ### StartOfWordsMatcher
  141. Matches the start of each word against each word in a search.
  142. For example, a search for 'jo ta' would match; 'John Taylor' or 'Joshua B. Takeshi'
  143. ### StudlyCaseMatcher
  144. Matches Studly Case strings using the first letters of the words only
  145. For example a search for 'hp' would match; 'HtmlServiceProvider' or 'HashParser' but not 'hasProvider'
  146. ### InStringMatcher
  147. Matches against any occurrences of a string within a string and is case-insensitive.
  148. For example, a search for 'smi' would match; 'John Smith' or 'Smiley Face'
  149. ### TimesInStringMatcher
  150. Matches a string based on how many times the search string appears inside the string it then applies the multiplier for each occurrence.
  151. For example, a search for 'tha' would match; 'I hope that that cat has caught that mouse' (3 x multiplier) or 'Thanks, it was great!' (1 x multiplier)
  152. ### LevenshteinMatcher
  153. See *Levenshtein Driver*
  154. ## Extending
  155. ### Drivers
  156. It's really easy to roll your own search drivers. Simply create a class that extends `TomLingham\Searchy\SearchDrivers\BaseSearchDriver` and add a property called `$matchers` with an array of matcher classes as the key and the multiplier for each matcher as the values. You can pick from the classes that are already included with Searchy or you can create your own.
  157. ### Matchers
  158. To create your own matchers, you can create your own class that extends `TomLingham\Searchy\Matchers\BaseMatcher` and (for simple Matchers) override the `formatQuery` method to return a string formatted with `%` wildcards in required locations. For more advanced extensions you may need to override the `buildQuery` method and others as well.
  159. ## Contributing & Reporting Bugs
  160. If you would like to improve on the code that is here, feel free to submit a pull request.
  161. If you find any bugs, submit them here and I will respond as soon as possible. Please make sure to include as much information as possible.