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.

200 lines
8.6 KiB

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