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.

194 lines
8.0 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
10 years ago
10 years ago
10 years ago
  1. Laravel 4+ Searchy
  2. ========================================
  3. ### Database Searching Made Easy
  4. Searchy is an easy-to-use Laravel 4+ 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 a bit slower than dedicated search programs) but can be set up and ready to go in minutes.
  7. Installation
  8. ----------------------------------------
  9. Add `"tom-lingham/searchy" : "dev-master"` to your composer.json file under `require`:
  10. ```
  11. "require": {
  12. "laravel/framework": "4.*",
  13. "tom-lingham/searchy" : "dev-master"
  14. }
  15. ```
  16. Run `composer update` in your terminal to pull the package down into your vendors folder.
  17. Add the service provider to the `providers` array in Laravel's app/config/app.php file:
  18. ```php
  19. 'TomLingham\Searchy\SearchyServiceProvider'
  20. ```
  21. Add the Alias to the `aliases` array in Laravel's app/config/app.php file:
  22. ```php
  23. 'Searchy' => 'TomLingham\Searchy\Facades\Searchy'
  24. ```
  25. Usage
  26. ----------------------------------------
  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');
  31. ```
  32. you can also write this as:
  33. ```php
  34. $users = Searchy::search('users')->fields('name', 'email')->query('John Smith');
  35. ```
  36. In this case, pass the columns you want to search through to the `fields()` method.
  37. These examples both return a Laravel DB Query Builder Object, so you will need to chain `get()` to actually return the results in a usable object:
  38. ```php
  39. $users = Searchy::search('users')->fields('name', 'email')->query('John Smith')->get();
  40. ```
  41. #### Searching multiple Columns
  42. You can also add multiple arguments to the list of fields/columns to search by.
  43. For example, if you want to search the name, email address and username of a user, you might run:
  44. ```php
  45. $users = Searchy::users('name', 'email', 'username')->query('John Smith');
  46. ```
  47. Configuration
  48. ----------------------------------------
  49. You can publish the configuration file to your `app` directory and override the settings by running `php artisan config:publish tom-lingham/searchy`
  50. You can set the default driver to use for searches in the configuration file. Your options (at this stage) are: `fuzzy`, `simple` and `levenshtein`.
  51. You can also override these methods using the following syntax when running a search:
  52. ```php
  53. Searchy::driver('fuzzy')->users('name')->query('Bat Man')->get();
  54. ```
  55. Drivers
  56. ----------------------------------------
  57. Searchy takes advantage of 'Drivers' to handle matching various conditions of the fields you specify.
  58. Drivers are simply a specified group of 'Matchers' which match strings based on specific conditions.
  59. Currently there are only three drivers: Simple, Fuzzy and Levenshtein (Experimental).
  60. #### Simple Search Driver
  61. The Simple search driver only uses 3 matchers each with the relevant multipliers that best suited my testing environments.
  62. ```php
  63. protected $matchers = [
  64. 'TomLingham\Searchy\Matchers\ExactMatcher' => 100,
  65. 'TomLingham\Searchy\Matchers\StartOfStringMatcher' => 50,
  66. 'TomLingham\Searchy\Matchers\InStringMatcher' => 30,
  67. ];
  68. ```
  69. #### Fuzzy Search Driver
  70. 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.
  71. ```php
  72. protected $matchers = [
  73. 'TomLingham\Searchy\Matchers\ExactMatcher' => 100,
  74. 'TomLingham\Searchy\Matchers\StartOfStringMatcher' => 50,
  75. 'TomLingham\Searchy\Matchers\AcronymMatcher' => 42,
  76. 'TomLingham\Searchy\Matchers\ConsecutiveCharactersMatcher' => 40,
  77. 'TomLingham\Searchy\Matchers\StartOfWordsMatcher' => 35,
  78. 'TomLingham\Searchy\Matchers\StudlyCaseMatcher' => 32,
  79. 'TomLingham\Searchy\Matchers\InStringMatcher' => 30,
  80. 'TomLingham\Searchy\Matchers\TimesInStringMatcher' => 8,
  81. ];
  82. ```
  83. #### Levenshtein Search Driver (Experimental)
  84. 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.
  85. ```php
  86. protected $matchers = [
  87. 'TomLingham\Searchy\Matchers\LevenshteinMatcher' => 100
  88. ];
  89. ```
  90. Matchers
  91. ----------------------------------------
  92. #### ExactMatcher
  93. Matches an exact string and applies a high multiplier to bring any exact matches to the top.
  94. When sanitize is on, if the expression strips some of the characters from the search query then this may not be able to match against a string despite entering in an exact match.
  95. #### StartOfStringMatcher
  96. Matches Strings that begin with the search string.
  97. For example, a search for 'hel' would match; 'Hello World' or 'helping hand'
  98. #### AcronymMatcher
  99. Matches strings for Acronym 'like' matches but does NOT return Studly Case Matches
  100. For example, a search for 'fb' would match; 'foo bar' or 'Fred Brown' but not 'FreeBeer'.
  101. #### ConsecutiveCharactersMatcher
  102. 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.
  103. For Example, a search for 'fba' would match; 'Foo Bar' or 'Afraid of bats', but not 'fabulous'
  104. #### StartOfWordsMatcher
  105. Matches the start of each word against each word in a search.
  106. For example, a search for 'jo ta' would match; 'John Taylor' or 'Joshua B. Takeshi'
  107. #### StudlyCaseMatcher
  108. Matches Studly Case strings using the first letters of the words only
  109. For example a search for 'hp' would match; 'HtmlServiceProvider' or 'HashParser' but not 'hasProvider'
  110. #### InStringMatcher
  111. Matches against any occurrences of a string within a string and is case-insensitive.
  112. For example, a search for 'smi' would match; 'John Smith' or 'Smiley Face'
  113. #### TimesInStringMatcher
  114. Matches a string based on how many times the search string appears inside the string it then applies the multiplier for each occurrence.
  115. 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)
  116. #### LevenshteinMatcher
  117. See *Levenshtein Driver*
  118. Extending
  119. ----------------------------------------
  120. #### Drivers
  121. 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.
  122. #### Matchers
  123. 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.
  124. Contributing & Reporting Bugs
  125. ----------------------------------------
  126. If you would like to improve on the code that is here, feel free to submit a pull request.
  127. 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.
  128. Road Map
  129. ----------------------------------------
  130. To the future! The intention is to (eventually):
  131. 1. Remove Searchy's dependancy on Laravel
  132. 2. Include more drivers for more advanced searching (Including file system searching, indexing and more)
  133. 3. Implement an AJAX friendly interface for searching models and implementing auto-suggestion features on the front end
  134. 4. Speed up search performance and improve result relevance