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.

270 lines
10 KiB

10 years ago
10 years ago
10 years ago
9 years ago
9 years ago
9 years ago
9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
8 years ago
9 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. ## Laravel 5+ Searchy
  2. ### Database Searching Made Easy
  3. 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.
  4. It uses pseudo fuzzy searching and other weighted mechanics depending on the search driver that you have enabled.
  5. 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.
  6. #### !! Laravel 4 !!
  7. Looking for Laravel 4 compatible Searchy? Checkout the 1.0 branch :)
  8. https://github.com/TomLingham/Laravel-Searchy/tree/1.0
  9. ## Installation
  10. Add `"tom-lingham/searchy" : "2.*"` to your composer.json file under `require`:
  11. ```json
  12. "require": {
  13. "laravel/framework": "5.*",
  14. "tom-lingham/searchy" : "2.*"
  15. }
  16. ```
  17. Run `composer update` in your terminal to pull down the package into your vendors folder.
  18. Add the service provider to the `providers` array in Laravel's `./config/app.php` file:
  19. ```php
  20. TomLingham\Searchy\SearchyServiceProvider::class
  21. ```
  22. 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:
  23. ```php
  24. 'Searchy' => TomLingham\Searchy\Facades\Searchy::class
  25. ```
  26. ## Usage
  27. To use Searchy, you can take advantage of magic methods.
  28. If you are searching the name and email column/field of users in a `users` table you would, for example run:
  29. ```php
  30. $users = Searchy::users('name', 'email')->query('John Smith')->get();
  31. ```
  32. You can also write this as:
  33. ```php
  34. $users = Searchy::search('users')->fields('name', 'email')->query('John Smith')->get();
  35. ```
  36. In this case, pass the columns you want to search through to the `fields()` method.
  37. 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:
  38. ```php
  39. $users = Searchy::search('users')->fields('name', 'email')->query('John Smith')
  40. ->getQuery()->having('relevance', '>', 20)->get();
  41. ```
  42. ### Limit on results returned
  43. To limit your results, you can use Laravel's built in DatabaseQuery Builder method and chain further methods to narrow your results.
  44. ```php
  45. // Only get the top 10 results
  46. $users = Searchy::search('users')->fields('name', 'email')->query('John Smith')
  47. ->getQuery()->limit(10)->get();
  48. ```
  49. ### Searching multiple Columns
  50. You can also add multiple arguments to the list of fields/columns to search by.
  51. For example, if you want to search the name, email address and username of a user, you might run:
  52. ```php
  53. $users = Searchy::users('name', 'email', 'username')->query('John Smith')->get();
  54. ```
  55. 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):
  56. ```php
  57. $users = Searchy::users(['name', 'email', 'username'])->query('John Smith')->get();
  58. ```
  59. ### Searching Joined/Concatenated Columns
  60. 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:
  61. ```php
  62. $users = Searchy::users('first_name::last_name')->query('John Smith')->get();
  63. ```
  64. ### Soft Deleted Records
  65. 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;
  66. ```php
  67. Searchy::users('name')->withTrashed()->query('Batman')->get();
  68. ```
  69. ### Return only specific columns
  70. You can specify which columns to return in your search:
  71. ```php
  72. $users = Searchy::users('first_name::last_name')->query('John Smith')->select('first_name')->get();
  73. // Or you can swap those around...
  74. $users = Searchy::users('first_name::last_name')->select('first_name')->query('John Smith')->get();
  75. ```
  76. This will, however, also return the `relevance` aliased column regardless of what is entered here.
  77. ## How to get a Laravel Eloquent Collection
  78. 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 `hydrate()` method.
  79. ```php
  80. \App\User::hydrate(Searchy::users('name', 'email')->query('Andrew')->get());
  81. ```
  82. This method creates a collection of models from a plain arrays. This is just our case because Searchy results are provided as arrays, and using `hydrate` we will converted them to instances of `User` model.
  83. ## Unicode Characters Support
  84. If you are having issues with the returned results because you have unicode characters in your search data, you can use the `FuzzySearchUnicodeDriver`.
  85. _PLEASE NOTE: There is no sanitization of strings passed through to the `FuzzySearchUnicodeDriver` prior to inserting into raw MySQL statements. You will have to sanitize the string yourself first or risk opening up your application to SQL injection attacks. You have been warned._
  86. To use, first follow the instructions to publish your configuration file (`php artisan vendor:publish`) and change your default driver from `fuzzy` to `ufuzzy`.
  87. ```php
  88. return [
  89. 'default' => 'ufuzzy',
  90. ...
  91. ]
  92. ```
  93. ## Configuration
  94. 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`
  95. You can set the default driver to use for searches in the configuration file. Your options (at this stage) are: `fuzzy`, `simple` and `levenshtein`.
  96. You can also override these methods using the following syntax when running a search:
  97. ```php
  98. Searchy::driver('fuzzy')->users('name')->query('Batman')->get();
  99. ```
  100. ## Drivers
  101. Searchy takes advantage of 'Drivers' to handle matching various conditions of the fields you specify.
  102. Drivers are simply a specified group of 'Matchers' which match strings based on specific conditions.
  103. Currently there are only three drivers: Simple, Fuzzy and Levenshtein (Experimental).
  104. ### Simple Search Driver
  105. The Simple search driver only uses 3 matchers each with the relevant multipliers that best suited my testing environments.
  106. ```php
  107. protected $matchers = [
  108. 'TomLingham\Searchy\Matchers\ExactMatcher' => 100,
  109. 'TomLingham\Searchy\Matchers\StartOfStringMatcher' => 50,
  110. 'TomLingham\Searchy\Matchers\InStringMatcher' => 30,
  111. ];
  112. ```
  113. ### Fuzzy Search Driver
  114. 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.
  115. ```php
  116. protected $matchers = [
  117. 'TomLingham\Searchy\Matchers\ExactMatcher' => 100,
  118. 'TomLingham\Searchy\Matchers\StartOfStringMatcher' => 50,
  119. 'TomLingham\Searchy\Matchers\AcronymMatcher' => 42,
  120. 'TomLingham\Searchy\Matchers\ConsecutiveCharactersMatcher' => 40,
  121. 'TomLingham\Searchy\Matchers\StartOfWordsMatcher' => 35,
  122. 'TomLingham\Searchy\Matchers\StudlyCaseMatcher' => 32,
  123. 'TomLingham\Searchy\Matchers\InStringMatcher' => 30,
  124. 'TomLingham\Searchy\Matchers\TimesInStringMatcher' => 8,
  125. ];
  126. ```
  127. ### Levenshtein Search Driver (Experimental)
  128. 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.
  129. ```php
  130. protected $matchers = [
  131. 'TomLingham\Searchy\Matchers\LevenshteinMatcher' => 100
  132. ];
  133. ```
  134. ## Matchers
  135. ### ExactMatcher
  136. Matches an exact string and applies a high multiplier to bring any exact matches to the top.
  137. ### StartOfStringMatcher
  138. Matches Strings that begin with the search string.
  139. For example, a search for 'hel' would match; 'Hello World' or 'helping hand'
  140. ### AcronymMatcher
  141. Matches strings for Acronym 'like' matches but does NOT return Studly Case Matches
  142. For example, a search for 'fb' would match; 'foo bar' or 'Fred Brown' but not 'FreeBeer'.
  143. ### ConsecutiveCharactersMatcher
  144. 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.
  145. For Example, a search for 'fba' would match; 'Foo Bar' or 'Afraid of bats', but not 'fabulous'
  146. ### StartOfWordsMatcher
  147. Matches the start of each word against each word in a search.
  148. For example, a search for 'jo ta' would match; 'John Taylor' or 'Joshua B. Takeshi'
  149. ### StudlyCaseMatcher
  150. Matches Studly Case strings using the first letters of the words only
  151. For example a search for 'hp' would match; 'HtmlServiceProvider' or 'HashParser' but not 'hasProvider'
  152. ### InStringMatcher
  153. Matches against any occurrences of a string within a string and is case-insensitive.
  154. For example, a search for 'smi' would match; 'John Smith' or 'Smiley Face'
  155. ### TimesInStringMatcher
  156. Matches a string based on how many times the search string appears inside the string it then applies the multiplier for each occurrence.
  157. 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)
  158. ### LevenshteinMatcher
  159. See *Levenshtein Driver*
  160. ## Extending
  161. ### Drivers
  162. 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.
  163. ### Matchers
  164. 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.
  165. ## Contributing & Reporting Bugs
  166. If you would like to improve on the code that is here, feel free to submit a pull request.
  167. 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.