├── .travis.yml ├── LICENSE ├── README.md ├── include ├── RestfulDataProviderSearchAPI.php └── RestfulDataProviderSearchAPIInterface.php ├── modules └── restful_search_api_example │ ├── plugins │ └── restful │ │ └── basic_search │ │ └── node │ │ └── 1.0 │ │ ├── RestfulSearchAPIBasicSearch.class.php │ │ └── basic_search__1_0.inc │ ├── restful_search_api_example.info │ └── restful_search_api_example.module ├── restful_search_api.info ├── restful_search_api.module └── tests ├── RestfulSearchAPIFilterTestCase.test ├── RestfulSearchAPIHateoasTestCase.test ├── RestfulSearchAPISearchTestCase.test ├── RestfulSearchAPISortTestCase.test └── modules └── restful_search_api_test ├── plugins └── restful │ └── search │ └── 1.0 │ ├── RestfulSearchAPISearch.class.php │ └── search__1_0.inc ├── restful_search_api_test.info └── restful_search_api_test.module /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.4 5 | - 5.5 6 | - 5.6 7 | 8 | env: 9 | - PATH=$PATH:/home/travis/.composer/vendor/bin 10 | 11 | # This will create the database 12 | mysql: 13 | database: drupal 14 | username: root 15 | encoding: utf8 16 | 17 | install: 18 | # Grab Drush 19 | - composer global require "drush/drush:7.*" 20 | - phpenv rehash 21 | # Make sure we don't fail when checking out projects 22 | - echo -e "Host github.com\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config 23 | # LAMP package installation (mysql is already started) 24 | - sudo apt-get update 25 | - sudo apt-get install apache2 libapache2-mod-fastcgi 26 | # enable php-fpm, travis does not support any other method with php and apache 27 | - sudo cp ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf.default ~/.phpenv/versions/$(phpenv version-name)/etc/php-fpm.conf 28 | - sudo a2enmod rewrite actions fastcgi alias 29 | - echo "cgi.fix_pathinfo = 1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/php.ini 30 | - ~/.phpenv/versions/$(phpenv version-name)/sbin/php-fpm 31 | # Make sure the apache root is in our wanted directory 32 | - echo "$(curl -fsSL https://gist.githubusercontent.com/nickveenhof/11386315/raw/b8abaf9304fe12b5cc7752d39c29c1edae8ac2e6/gistfile1.txt)" | sed -e "s,PATH,$TRAVIS_BUILD_DIR/../drupal,g" | sudo tee /etc/apache2/sites-available/default > /dev/null 33 | # Set sendmail so drush doesn't throw an error during site install. 34 | - echo "sendmail_path='true'" >> `php --ini | grep "Loaded Configuration" | awk '{print $4}'` 35 | # Forward the errors to the syslog so we can print them 36 | - echo "error_log=syslog" >> `php --ini | grep "Loaded Configuration" | awk '{print $4}'` 37 | # Get latest drupal 8 core 38 | - cd $TRAVIS_BUILD_DIR/.. 39 | - git clone --depth 1 --branch 7.x http://git.drupal.org/project/drupal.git 40 | # Restart apache and test it 41 | - sudo service apache2 restart 42 | - curl -v "http://localhost" 43 | # Re-enable when trying to get CodeSniffer doesn't return a 403 anymore. 44 | #- composer global require drupal/coder:\>7 45 | 46 | before_script: 47 | - cd $TRAVIS_BUILD_DIR/../drupal 48 | # Update drupal core 49 | - git pull origin 7.x 50 | # Install the site 51 | - drush -v site-install minimal --db-url=mysql://root:@localhost/drupal --yes 52 | # Increase max_allowed_packet to avoid MySQL errors 53 | - echo -e "[server]\nmax_allowed_packet=64M" | sudo tee -a /etc/mysql/conf.d/drupal.cnf 54 | - sudo service mysql restart 55 | - phpenv rehash 56 | 57 | script: 58 | # Go to our Drupal module directory 59 | - mkdir $TRAVIS_BUILD_DIR/../drupal/sites/all/modules/restful_search_api 60 | - cp -R $TRAVIS_BUILD_DIR/* $TRAVIS_BUILD_DIR/../drupal/sites/all/modules/restful_search_api/ 61 | # Go to our Drupal main directory 62 | - cd $TRAVIS_BUILD_DIR/../drupal 63 | 64 | # Download and enable module and its dependencies 65 | - drush --yes dl entity 66 | # Patch Entity API. 67 | - curl -O https://www.drupal.org/files/issues/2086225-entity-access-check-node-create-3.patch 68 | - patch -p1 $TRAVIS_BUILD_DIR/../drupal/sites/all/modules/entity/modules/callbacks.inc < 2086225-entity-access-check-node-create-3.patch 69 | # Enable the RESTful modules 70 | - drush dl restful --yes 71 | - drush --yes pm-enable simpletest restful_search_api restful_search_api_test 72 | # Download entity validator. 73 | - drush dl entity_validator --dev --yes 74 | 75 | # Run the tests 76 | - cd $TRAVIS_BUILD_DIR/../drupal 77 | - php ./scripts/run-tests.sh --php $(which php) --concurrency 4 --verbose --color --url http://localhost "RESTful Search API" 2>&1 | tee /tmp/simpletest-result.txt 78 | - egrep -i "([1-9]+ fail)|(Fatal error)|([1-9]+ exception)" /tmp/simpletest-result.txt && exit 1 79 | - exit 0 80 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | {description} 294 | Copyright (C) {year} {fullname} 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | {signature of Ty Coon}, 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | 341 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Build Status](https://travis-ci.org/RESTful-Drupal/restful_search_api.svg?branch=7.x-1.x)](https://travis-ci.org/RESTful-Drupal/restful_search_api) 2 | 3 | # RESTful Search API 4 | Expose your Search API results with your RESTful API. 5 | 6 | ## Features 7 | Building a search engine integration with faceted search in Drupal is really 8 | easy with Search API and all the helper modules around it. With this module you 9 | can expose your search to your decoupled consumer. 10 | 11 | ### Get the output that you want for your search 12 | This falls in line with the philosophy of the RESTful module, where you get the 13 | exact output that you need. To do so you need to be declarative about it by 14 | implementing `publicFieldsInfo`. 15 | 16 | First of all configure your search index in Drupal using Search API to index the 17 | fields that you want to make searchable and you want to output. Once you have 18 | Search API configured, implement `publicFieldsInfo` to control how you expose 19 | each field that you need to expose with your HTTP API. 20 | 21 | #### How do I know the properties to map to? 22 | You will notice in the examples that you have to indicate the name of the 23 | property. Some times it is complicated to know what properties you have 24 | available in the search results that Search API returns, to be mapped by 25 | RESTful. In those situations it is helpful to use the `pass_through` option in 26 | your resource definition. That will output every available property regardless 27 | of the mappings that you have done in `publicFieldsInfo`. 28 | 29 | ### Sort by any public property 30 | You just need to provide the sort query string to sort your results: 31 | 32 | ``` 33 | curl https://www.example.org/api/search/lorem?sort=-created,id 34 | ``` 35 | 36 | Prepend a `-` in front of the sort key to sort in descending order. 37 | 38 | You can use any property that supports sorting in your search index, that will 39 | be passed to Search API. 40 | 41 | Additionally you can use any public property in your final output to sort the 42 | results. If the selected property does not support sorting in Search API the 43 | sorting will be applied to the output generated after making the query to Search 44 | API without sorting. Use this type of sorting only if you really need to, the 45 | preferred method is to use the supported sort properties in Search API. 46 | 47 | ### Filter by your facets 48 | Add some facets to the Search API configuration and use them in your search 49 | queries. To do so use 50 | [the same format used in RESTful](https://github.com/RESTful-Drupal/restful#filter-1). 51 | 52 | If a search has relevant facet information attached, those will be sent along 53 | with the search results. 54 | 55 | ```javascript 56 | // https://www.example.org/api/basic_search/elit?filter[comment_count][value]=2&filter[comment_count][operator]=">=" 57 | { 58 | "count": 23, 59 | "facets": { 60 | "author": [ 61 | { "filter": "\"0\"", "count": 12 }, 62 | { "filter": "\"1\"", "count": 11 } 63 | ], 64 | "comment_count": [ 65 | { "filter": "\"2\"", "count": 13 }, 66 | { "filter": "\"3\"", "count": 10 } 67 | ] 68 | }, 69 | "hal:basic_search": [ 70 | { "entity_id": 704, "version_id": 704, "relevance": 0.013727446 }, 71 | … 72 | ], 73 | "_links": { 74 | "self": { … }, 75 | "curies": { … } 76 | } 77 | } 78 | ``` 79 | ## Additional information 80 | See [this post in Medium](https://medium.com/@e0ipso/restful-drupal-with-search-api-f370050a26bb) with some additional information. 81 | -------------------------------------------------------------------------------- /include/RestfulDataProviderSearchAPI.php: -------------------------------------------------------------------------------- 1 | searchIndex; 46 | } 47 | 48 | /** 49 | * Set the search index machine name. 50 | * 51 | * @param string $searchIndex 52 | * The new name. 53 | */ 54 | public function setSearchIndex($searchIndex) { 55 | $this->searchIndex = $searchIndex; 56 | } 57 | 58 | /** 59 | * Set the total results count after executing the query. 60 | * 61 | * @param int $totalCount 62 | */ 63 | public function setTotalCount($totalCount) { 64 | $this->totalCount = $totalCount; 65 | } 66 | 67 | /** 68 | * Additional HATEOAS to be passed to the formatter. 69 | */ 70 | public function additionalHateoas() { 71 | return $this->hateoas; 72 | } 73 | 74 | /** 75 | * Constructs a RestfulDataProviderSearchAPI object. 76 | * 77 | * @param array $plugin 78 | * Plugin definition. 79 | * @param RestfulAuthenticationManager $auth_manager 80 | * (optional) Injected authentication manager. 81 | * @param DrupalCacheInterface $cache_controller 82 | * (optional) Injected cache backend. 83 | */ 84 | public function __construct(array $plugin, \RestfulAuthenticationManager $auth_manager = NULL, \DrupalCacheInterface $cache_controller = NULL) { 85 | parent::__construct($plugin, $auth_manager, $cache_controller); 86 | 87 | // Validate keys exist in the plugin's "data provider options". 88 | $required_keys = array('search_index'); 89 | $options = $this->processDataProviderOptions($required_keys); 90 | 91 | $this->searchIndex = $options['search_index']; 92 | } 93 | 94 | /** 95 | * {@inheritdoc} 96 | */ 97 | public function getTotalCount() { 98 | return $this->totalCount; 99 | } 100 | 101 | /** 102 | * {@inheritdoc} 103 | * 104 | * @throws \RestfulServerConfigurationException 105 | * If the provided search index does not exist. 106 | */ 107 | public function view($id) { 108 | // In this case the ID is the search query. 109 | $options = $output = array(); 110 | // Construct the options array. 111 | 112 | // Set the following options: 113 | // - offset: The position of the first returned search results relative to 114 | // the whole result in the index. 115 | // - limit: The maximum number of search results to return. -1 means no 116 | // limit. 117 | list($options['offset'], $options['limit']) = $this->parseRequestForListPagination(); 118 | 119 | try { 120 | // Query SearchAPI for the results 121 | $search_results = $this->executeQuery($id, $options); 122 | foreach ($search_results as $search_result) { 123 | $output[] = $this->mapSearchResultToPublicFields($search_result); 124 | } 125 | } 126 | catch (\SearchApiException $e) { 127 | // Relay the exception with one of RESTful's types. 128 | throw new \RestfulServerConfigurationException(format_string('Search API Exception: @message', array( 129 | '@message' => $e->getMessage(), 130 | ))); 131 | } 132 | 133 | // This is an emergency sort. Only apply it if no sort could be applied. 134 | $any_sort_applied = array_filter(array_values($this->sorted)); 135 | $result = reset($output); 136 | $available_keys = array_keys($result); 137 | if (!$any_sort_applied) { 138 | $this->manualArraySort($available_keys, $output); 139 | } 140 | 141 | return $output; 142 | } 143 | 144 | /** 145 | * {@inheritdoc} 146 | */ 147 | protected function parseRequestForListFilter() { 148 | // At the moment RESTful only supports AND conjunctions. 149 | $search_api_filter = new SearchApiQueryFilter('AND'); 150 | 151 | $request = $this->getRequest(); 152 | if (empty($request['filter'])) { 153 | // No filtering is needed. 154 | return $search_api_filter; 155 | } 156 | $url_params = $this->getPluginKey('url_params'); 157 | if (!$url_params['filter']) { 158 | throw new \RestfulBadRequestException('Filter parameters have been disabled in server configuration.'); 159 | } 160 | 161 | $public_fields = $this->getPublicFields(); 162 | 163 | foreach ($request['filter'] as $public_field => $value) { 164 | $field = empty($public_fields[$public_field]['property']) ? $public_field : $public_fields[$public_field]['property']; 165 | 166 | if (!is_array($value)) { 167 | // Request uses the shorthand form for filter. For example 168 | // filter[foo]=bar would be converted to filter[foo][value] = bar. 169 | $value = array('value' => $value); 170 | } 171 | // Set default operator. 172 | $value += array('operator' => '='); 173 | 174 | // Clean the operator in case it came from the URL. 175 | // e.g. filter[minor_version][operator]=">=" 176 | $value['operator'] = str_replace(array('"', "'"), '', $value['operator']); 177 | 178 | $this->isValidOperatorsForFilter(array($value['operator'])); 179 | 180 | $search_api_filter->condition($field, $value['value'], $value['operator']); 181 | } 182 | 183 | return $search_api_filter; 184 | } 185 | 186 | /** 187 | * Filter the query for list. 188 | * 189 | * @param \SearchApiQueryInterface $query 190 | * The query object. 191 | */ 192 | protected function queryForListFilter(\SearchApiQueryInterface $query) { 193 | $query->filter($this->parseRequestForListFilter()); 194 | } 195 | 196 | /** 197 | * Executes the Search API query and stores the total count. 198 | * 199 | * @param string $keywords 200 | * Keywords to search. 201 | * @param array $options 202 | * An array of options passed to search_api_query. 203 | * 204 | * @throws \RestfulServerConfigurationException 205 | * For invalid indices. 206 | * 207 | * @return array 208 | * The array of results. 209 | * 210 | * @see search_api_query() 211 | */ 212 | protected function executeQuery($keywords, array $options) { 213 | $index = search_api_index_load($this->getSearchIndex()); 214 | 215 | if (!$index) { 216 | throw new \RestfulServerConfigurationException(format_string('Search API Exception: Unknown index with ID @id.', array( 217 | '@id' => $this->getSearchIndex(), 218 | ))); 219 | } 220 | $query = $index->query($options); 221 | 222 | $this->queryForListSort($query); 223 | $this->queryForListFilter($query); 224 | $resultsObj = $query 225 | ->keys($keywords) 226 | ->execute(); 227 | 228 | $this->setTotalCount($resultsObj['result count']); 229 | $results = $index->loadItems(array_keys($resultsObj['results'])); 230 | 231 | // Add the index id and the relevance. 232 | foreach ($resultsObj['results'] as $id => $result) { 233 | $results[$id]->search_api_id = $result['id']; 234 | $results[$id]->search_api_relevance = $result['score']; 235 | } 236 | if (!empty($resultsObj['search_api_facets'])) { 237 | $this->hateoas['facets'] = $resultsObj['search_api_facets']; 238 | } 239 | $this->hateoas['count'] = $resultsObj['result count']; 240 | 241 | return $results; 242 | } 243 | 244 | /** 245 | * Sort the query for list. 246 | * 247 | * @param \SearchApiQueryInterface $query 248 | * The Search API query. 249 | * 250 | * @throws \RestfulBadRequestException 251 | * 252 | * @see \RestfulEntityBase::getQueryForList 253 | */ 254 | protected function queryForListSort(\SearchApiQueryInterface $query) { 255 | $public_fields = $this->getPublicFields(); 256 | 257 | // Get the sorting options from the request object. 258 | $sorts = $this->parseRequestForListSort(); 259 | 260 | $sorts = $sorts ? $sorts : $this->defaultSortInfo(); 261 | 262 | foreach ($sorts as $sort => $direction) { 263 | $property = empty($public_fields[$sort]['property']) ? $sort : $public_fields[$sort]['property']; 264 | try { 265 | $query->sort($property, $direction); 266 | 267 | // Mark this sort as applied. 268 | $this->sorted[$sort] = TRUE; 269 | } 270 | catch (\SearchApiException $e) { 271 | // Do not throw an exception, we will sort manually the array 272 | // afterwards. 273 | } 274 | } 275 | } 276 | 277 | /** 278 | * Overrides \RestfulBase::parseRequestForListSort 279 | */ 280 | protected function parseRequestForListSort() { 281 | $request = $this->getRequest(); 282 | $public_fields = $this->getPublicFields(); 283 | 284 | if (empty($request['sort'])) { 285 | return array(); 286 | } 287 | $url_params = $this->getPluginKey('url_params'); 288 | if (!$url_params['sort']) { 289 | throw new \RestfulBadRequestException('Sort parameters have been disabled in server configuration.'); 290 | } 291 | 292 | $sorts = array(); 293 | foreach (explode(',', $request['sort']) as $sort) { 294 | $direction = $sort[0] == '-' ? 'DESC' : 'ASC'; 295 | $sort = str_replace('-', '', $sort); 296 | 297 | $sorts[$sort] = $direction; 298 | 299 | // Initially mark all sort criteria as not applied. 300 | $this->sorted[$sort] = FALSE; 301 | } 302 | return $sorts; 303 | } 304 | 305 | /** 306 | * Return the default sort. 307 | * 308 | * @return array 309 | * A default sort array. 310 | */ 311 | public function defaultSortInfo() { 312 | return array(); 313 | } 314 | 315 | /** 316 | * Prepares the output array from the search result. 317 | * 318 | * @param object $result 319 | * Search result from Search API. 320 | * 321 | * @return array 322 | * The prepared output. 323 | */ 324 | protected function mapSearchResultToPublicFields($result) { 325 | if ($this->getPluginKey('pass_through')) { 326 | return (array) $result; 327 | } 328 | $output = array(); 329 | // Loop over all the defined public fields. 330 | foreach ($this->getPublicFields() as $public_field_name => $info) { 331 | $value = NULL; 332 | // If there is a callback defined execute it instead of a direct mapping. 333 | if ($info['callback']) { 334 | $value = static::executeCallback($info['callback'], array($result)); 335 | } 336 | // Map row names to public properties. 337 | elseif ($info['property']) { 338 | $value = $result->{$info['property']}; 339 | if (!empty($info['sub-property'])) { 340 | $parts = explode(static::NESTING_SEPARATOR, $info['sub-property']); 341 | foreach ($parts as $part) { 342 | $value = $value[$part]; 343 | } 344 | } 345 | } 346 | 347 | // Execute the process callbacks. 348 | if ($value && $info['process_callbacks']) { 349 | foreach ($info['process_callbacks'] as $process_callback) { 350 | $value = static::executeCallback($process_callback, array($value)); 351 | } 352 | } 353 | 354 | $output[$public_field_name] = $value; 355 | } 356 | 357 | return $output; 358 | } 359 | 360 | /** 361 | * If no sort could be applied via Search API, then sort the results manually. 362 | * 363 | * This is a last resource thing and arguably a good idea. If the results are 364 | * paginated it can lead to unexpected results. 365 | * 366 | * @param $available_keys 367 | * The available keys on the results array. 368 | * 369 | * @param $results 370 | * The array of search results from Search API. 371 | */ 372 | protected function manualArraySort($available_keys, &$results) { 373 | $sorts = $this->parseRequestForListSort(); 374 | $sorts = $sorts ? $sorts : $this->defaultSortInfo(); 375 | foreach ($sorts as $sort => $direction) { 376 | // Since this is an expensive operation only apply the first sort. 377 | if (in_array($sort, $available_keys)) { 378 | usort($results, function ($a, $b) use ($sort, $direction) { 379 | if ($direction == 'DESC') { 380 | return $a[$sort] < $b[$sort]; 381 | } 382 | return $a[$sort] > $b[$sort]; 383 | }); 384 | break; 385 | } 386 | } 387 | } 388 | 389 | } 390 | -------------------------------------------------------------------------------- /include/RestfulDataProviderSearchAPIInterface.php: -------------------------------------------------------------------------------- 1 | array( 16 | 'property' => 'search_api_id', 17 | 'process_callbacks' => array( 18 | 'intVal', 19 | ), 20 | ), 21 | 'version_id' => array( 22 | 'property' => 'vid', 23 | 'process_callbacks' => array( 24 | 'intVal', 25 | ), 26 | ), 27 | 'relevance' => array( 28 | 'property' => 'search_api_relevance', 29 | ), 30 | 'body' => array( 31 | 'property' => 'body', 32 | 'sub-property' => LANGUAGE_NONE . '::0::value', 33 | ), 34 | 'title' => array( 35 | 'property' => 'title', 36 | ), 37 | ); 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /modules/restful_search_api_example/plugins/restful/basic_search/node/1.0/basic_search__1_0.inc: -------------------------------------------------------------------------------- 1 | 'basic_search', 5 | 'name' => 'basic_search__1_0', 6 | 'label' => t('Basic Search'), 7 | 'description' => t('Provides basic info doing Search API searches.'), 8 | 'data_provider_options' => array( 9 | 'search_index' => 'default_node_index', 10 | ), 11 | 'class' => 'RestfulSearchAPIBasicSearch', 12 | 'authentication_types' => TRUE, 13 | 'authentication_optional' => TRUE, 14 | // Use pass_through to expose the complete search result objects. This is 15 | // useful while developing to allow you to know how to do the field mappings. 16 | // 'pass_through' => TRUE, 17 | ); 18 | -------------------------------------------------------------------------------- /modules/restful_search_api_example/restful_search_api_example.info: -------------------------------------------------------------------------------- 1 | name = RESTful Search API Example 2 | description = Shows how to create a resource backed by Search API. 3 | dependencies[] = restful_search_api 4 | core = 7.x 5 | -------------------------------------------------------------------------------- /modules/restful_search_api_example/restful_search_api_example.module: -------------------------------------------------------------------------------- 1 | 'Test search API integration', 21 | 'description' => 'Tests that searches return the expected results', 22 | 'group' => 'RESTful Search API', 23 | ); 24 | } 25 | 26 | /** 27 | * {@inheritdoc} 28 | */ 29 | public function setUp() { 30 | parent::setUp('entity', 'search_api', 'search_api_test', 'restful', 'restful_search_api_test'); 31 | 32 | $this->drupalLogin($this->drupalCreateUser(array('administer search_api'))); 33 | 34 | // Create server. 35 | $values = array( 36 | 'name' => 'Search API test server', 37 | 'machine_name' => 'test_server', 38 | 'enabled' => 1, 39 | 'description' => 'A server used for testing.', 40 | 'class' => 'search_api_test_service', 41 | ); 42 | search_api_server_insert($values); 43 | 44 | // Create index. 45 | $values = array( 46 | 'name' => 'Search API test index', 47 | 'machine_name' => 'test_index', 48 | 'item_type' => 'search_api_test', 49 | 'enabled' => 1, 50 | 'description' => 'An index used for testing.', 51 | 'server' => 'test_server', 52 | 'options' => array( 53 | 'fields' => array( 54 | 'id' => array( 55 | 'type' => 'integer', 56 | 'boost' => '1.0', 57 | 'indexed' => 1, 58 | ), 59 | 'title' => array( 60 | 'type' => 'text', 61 | 'boost' => '5.0', 62 | 'indexed' => 1, 63 | ), 64 | 'body' => array( 65 | 'type' => 'text', 66 | 'boost' => '1.0', 67 | 'indexed' => 1, 68 | ), 69 | 'type' => array( 70 | 'type' => 'string', 71 | 'boost' => '1.0', 72 | 'indexed' => 1, 73 | ), 74 | ), 75 | ), 76 | ); 77 | search_api_index_insert($values); 78 | 79 | // Insert items. 80 | $count = db_query('SELECT COUNT(*) FROM {search_api_test}')->fetchField(); 81 | for ($i = 1; $i <= static::NUM_ITEMS; ++$i) { 82 | $id = $count + $i; 83 | $values = array( 84 | 'id' => $id, 85 | 'title' => "Title $id", 86 | 'body' => $this->randomString(), 87 | 'type' => 'Item', 88 | ); 89 | db_insert('search_api_test')->fields($values)->execute(); 90 | module_invoke_all('entity_insert', search_api_test_load($id), 'search_api_test'); 91 | } 92 | 93 | // Index items. 94 | search_api_index_enable('test_index'); 95 | search_api_server_enable('test_server'); 96 | search_api_index_items(search_api_index_load('test_index')); 97 | } 98 | 99 | /** 100 | * Test the search results. 101 | */ 102 | public function testSearch() { 103 | $handler = restful_get_restful_handler('search', 1, 0); 104 | // Search all the items that contain the keyword title. 105 | $results = $handler->get('Title'); 106 | $this->assertFalse(empty($results), 'Some results were found.'); 107 | } 108 | 109 | } 110 | -------------------------------------------------------------------------------- /tests/RestfulSearchAPISortTestCase.test: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RESTful-Drupal/restful_search_api/313f7f4b8c0156a07804455f0fe9fde5f00b508a/tests/RestfulSearchAPISortTestCase.test -------------------------------------------------------------------------------- /tests/modules/restful_search_api_test/plugins/restful/search/1.0/RestfulSearchAPISearch.class.php: -------------------------------------------------------------------------------- 1 | array( 16 | 'property' => 'search_api_id', 17 | 'process_callbacks' => array( 18 | 'intVal', 19 | ), 20 | ), 21 | 'relevance' => array( 22 | 'property' => 'search_api_relevance', 23 | ), 24 | 'body' => array( 25 | 'property' => 'body', 26 | ), 27 | 'title' => array( 28 | 'property' => 'title', 29 | ), 30 | ); 31 | } 32 | 33 | } 34 | -------------------------------------------------------------------------------- /tests/modules/restful_search_api_test/plugins/restful/search/1.0/search__1_0.inc: -------------------------------------------------------------------------------- 1 | 'search', 5 | 'name' => 'search__1_0', 6 | 'label' => t('Search'), 7 | 'description' => t('Provides info doing Search API searches.'), 8 | 'data_provider_options' => array( 9 | 'search_index' => 'test_index', 10 | ), 11 | 'major_version' => 1, 12 | 'minor_version' => 0, 13 | 'class' => 'RestfulSearchAPISearch', 14 | ); 15 | -------------------------------------------------------------------------------- /tests/modules/restful_search_api_test/restful_search_api_test.info: -------------------------------------------------------------------------------- 1 | name = RESTful Search API test module 2 | description = Test module 3 | dependencies[] = restful_search_api 4 | core = 7.x 5 | -------------------------------------------------------------------------------- /tests/modules/restful_search_api_test/restful_search_api_test.module: -------------------------------------------------------------------------------- 1 |