The CMS offers a flexible interface for fulltext searching. Implementing projects and define their own SearchProviders, or use an existing one or even multiple providers.
Note: Currently, the CMS shipps with an ElasticSearch SearchProvider. Instruction see below.
ch.insign.commons.search
Contains a basic search provider framework, including:
Usage:
SearchManager sm = new SearchManager();
sm.register(new ElasticSearchProvider());
SearchQuery query = new SearchQuery(keywords);
List<SearchResult> results = sm.search(query);
From within the cms, you reach the SearchManager via CMS.getSearchManager()
Defaults in the cms' application.conf:
play-cms/conf/application.conf:
# Search providers
cms.search.elastic.enabled = false
# Elastic settings
elastic.host = "http://localhost:9200"
elastic.snippet.number = 2
elastic.snippet.size = 100
# Needs to match the cluster name in ELs config/elasticsearch.yml
# Ensure different cluster names are used for all independent machines (e.g. test, production, dev) in the same network!
elastic.cluster = "elasticsearch"
Note: ElasticSearch is disabled by default. It is recommended to use a local.conf
file to individually enable/disable ElasticSearch and give unique cluster names.
# ElasticSearch search provider
cms.search.elastic.enabled = true
# needs to match the cluster name in ELs config/elasticsearch.yml
elastic.cluster = "elasticsearch-local-dev-bachi"
By default, there is a search route enabled which gives a rough debug search output:
GET /search/:query ch.insign.cms.controllers.FrontendController.search(query: String)
In production however, you will create your own cms page as a search result page (by adding a SearchResultBlock
)
<form action="@PageBlock.find.byKey(MyCmsSetup.KEY_SEARCHRESULT_PAGE).getNavItem.getURL">
<input type="text" name="query" class="form-control" placeholder="Search …">
<button type="submit"></button>
</form>
To exclude html content from the search index, you need to mark it in your templates:
Note: The SearchProvider's indexing code needs to add &indexer=true
to each request. This param is not protected as it is meant to remove public content from the searchindexer. This makes testing easy, just add &indexer=true
to your url.
sudo service start | stop | restart
and the service's automatically started on startupconf/elasticsearch.yml
and set the cluster name (matching the name defined in your Play's config, see above). Restart the server.[info] application - Elastic: Trying to connect to cluster 'http://localhost:9200' on 'elasticsearch-local-bachi'
[info] application - ElasticSearchProvider: Added/updating url http://localhost:9000/handtuch?indexer=true