LiipSearchBundle
This bundle provides a uniform interface for full text search with various
search engines and a controller with twig templates to render search forms and
results.
Note: You are looking at Version 2 of this bundle, which saw large changes
compared to Version 1.
Introduction
This search bundle simplifies adding search to your site.
Provided for you are:
- A controller to render a search box and the search page with twig templates
- A service to query google site search
- A service which provides paging for the search results
Built-in Search Engines Support
For now, Google site search is supported
out of the box. There is one implementation using the
Google REST API
and one implementation using the Custom Search Element
feature that loads the search with only javascript in the frontend.
Contributions for other services are welcome.
Installation
Install the bundle with composer require liip/search-bundle
.
Include the bundle in app/Kernel.php.
Add your preferred search engine in app/config/config.yml:
liip_search:
clients:
google_rest:
api_key: '%google.api_key%'
search_key: '%google.search_key%'
Or if you use the javascript Google custom search engine:
liip_search:
clients:
google_cse:
cse_id: '%google.search_key%'
Usage
You can display a search box anywhere on the page with the liip_search_box twig function:
{{ liip_search_box(query, 'query-field-id', 'css-class') }}
You can customize the search box with these parameters:
- query - default query to display
- fieldId - HTML id to use for the search input field. Use different ids when having more than one search box on your page, e.g. in the header and in content.
- cssClass - A css class to apply to the whole search box
<form>
.
Create a route for the search action. The easiest is to just use the provided
routing.xml from your main project routing.xml:
liip_search:
resource: "@LiipSearchBundle/Resources/config/routing.xml"
It defaults to the URL /search
. If you want a different route, use the prefix
option when including the route or configure your own route using
%liip_search.controller.search_action%
as default value for _controller
.
Customizing Templating
The search result templates provided by this bundle extend the
LiipSearchBundle::layout.html.twig
template. To integrate with the rest of your
site, you have two options:
- Create
app/Resources/LiipSearchBundle/views/layout.html.twig
and make it extend your base layout, putting aliip_search_content
block where you want the search results. - Create
app/Resources/LiipSearchBundle/views/Search/search.html.twig
and build your own templating structure - you should be able touse
thesearch_results.twig.html
template to get theliip_search_content
block.
Of course you can also override any of the templates to customize what they
should do. See
http://symfony.com/doc/master/book/templating.html#overriding-bundle-templates
Configuration Reference
This is the full reference of what you can configure under the liip_search
key:
search_factory
string, default value: null
Specify a custom service that implements the Liip\SearchBundle\SearchFactoryInterface
.
This service will be used by the controller to create Pagerfanta
instances to handle
the search.
If you configure one of the search engine services, you do not need to set this
field.
search_route
string, default value: liip_search
The name of the route that will handle submitted search requests.
restrict_language
boolean, default value: false
Change this to true if you want to ask the search service to restrict the
results to the language of the request.
Google Search REST API Integration
Configuring any of these options enables the google search engine service. They
are located under clients.google_rest
.
api_key
string, required
Your Google API key
search_key
string|array, required
The key identifying your Google Search Engine.
May be a list of keys indexed by locale to use different engines per locale.
If you control locales through separate search engines, you do not need to set
restrict_language
to true unless you want your custom search engines to
receive a language restriction additionally.
api_url
string, default value: https://www.googleapis.com/customsearch/v1
The Google Search API URL for REST calls
restrict_to_site
string, default value: null
If left empty, all sites configured for the google search engines are searched.
Set to a a domain to limit to that domain.
Google Custom Search Engine Integration
Configuring this section activates a different controller that renders the
Javascript fragment to enable the CSE search. This configuration is located
under clients.google_cse
.
cse_id
string|array, required
The key identifying your Google Custom Search Engine.
May be a list of keys indexed by locale to use different engines per locale.
CSE does not support the restrict_language
, so different search engines per
language are your only option to restrict the language of search results.
Troubleshooting
Google Custom Search Engine
If you get SearchException
saying "Empty response received from Google Search
Engine API", try copying the URL that is output into a browser. You should get
JSON response, but likely it will haves an error status.
If you get a status 500 with an empty message, chances are that you need to
renew the search engine in the Google admin panel.
Adding your own Search Service
Implement the Liip\SearchBundle\SearchInterface
and configure it as a service.
Then set liip_search.search_client
to that service name.
TODO
- Use guzzle to talk to google REST API
- Add support for refinements (more like this) with info in search result array that can be passed to SearchInterface::refineSearch
- Expose more of the google search parameters
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is furnished
to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
liip_search:
search_factory: ~
search_route: liip_search
query_param_name: query
page_param_name: page
restrict_language: false
max_per_page: 10
clients:
google_rest:
enabled: false
api_key: ~ # Required
search_key: # Required
# Prototype
locale: []
search_api_url: https://www.googleapis.com/customsearch/v1
restrict_to_site: false
google_cse:
enabled: false
cse_id: # Required
# Prototype
locale: []
-
Merge pull request #28 from liip/analysis-8QMbPP
By dbu, 4 years ago
-
Applied fixes from StyleCI
By StyleCIBot, 4 years ago
-
Merge pull request #27 from vpowler/patch-1
By dbu, 4 years ago
-
Rename LiipSearchBundle.it.xml to LiipSearchBundle.it.xliff
By vpowler, 4 years ago
-
Merge pull request #25 from liip/twig_deprecation_fixes
By dbu, 5 years ago
-
switch form Twig_Function_Method to Twig_SimpleFunction
By lsmith77, 5 years ago
-
Merge pull request #24 from liip/fix_key_config
By dbu, 5 years ago
-
fix handling of the search keys in the config
By lsmith77, 5 years ago
-
allow 3.0, set an upper bound for php
By lsmith77, 5 years ago
-
Merge pull request #23 from liip/analysis-XarQoq
By lsmith77, 5 years ago
-
Applied fixes from StyleCI
By StyleCIBot, 5 years ago
-
added styleci
By lsmith77, 5 years ago
-
Merge pull request #21 from liip/merge-1.0-to-master
By dbu, 5 years ago
-
Merge remote-tracking branch 'origin/1.0' into merge-1.0-to-master
By dbu, 5 years ago
-
Merge pull request #20 from liip/drop_2_1_support
By dbu, 5 years ago
-
require symfony 2.3
By lsmith77, 5 years ago
-
Pass '0' estimated count in case of empty query
By Donato Rotunno, 5 years ago
-
Better documentation for google custom search
By dbu, 5 years ago
-
Do not display pager when no items are returned
By Donato Rotunno, 5 years ago
-
Check data items exist
By Donato Rotunno, 5 years ago
-
update readme
By dbu, 5 years ago
-
Create LiipSearchBundle.it.xml
By lsmith77, 5 years ago
-
Merge pull request #17 from liip/refactor-bundle-pagerfanta
By lsmith77, 5 years ago
-
cleanup google cse frontend search implementation
By dbu, 5 years ago
-
do not require any client to be configured
By lsmith77, 5 years ago
-
continue refactoring and drop our pager in favor of pagerfanta
By dbu, 5 years ago
-
further cleanup
By dbu, 5 years ago
-
Improved markup semantic and added a few css classes for easier styling
By Donato Rotunno, 5 years ago
-
large refactoring
By dbu, 5 years ago
-
CS cleanup
By dbu, 5 years ago