This is a summary of how to install and run Solr in Magnolia. The information provided in this entry has been retrieved from several places and people: Magnolia documentation, Milan Divilek and Jonathan Ayala hints, Solr official documentation, a Mykong site entry and a bit of self investigation. As an overview, we will follow 4 main steps:
Install and configure Solr
- Activate and run Crawler
- Create java class to query and provide Solr search results
Create FTL file for invoking Solr search and rendering the result into a XML file
- Install and configure Solr -
For this tutorial I'm using magnolia 5.7.2, EE (generated via mvn org.apache.maven.plugins:maven-archetype-plugin:2.4:generate -DarchetypeCatalog=https://nexus.magnolia-cms.com/content/groups/public/)
I included the following dependencies in my pom:
Now, we can mount the project in our server, start and install Magnolia (no restart will be needed beyond this point).
Download and install Solr from here http://lucene.apache.org/solr/mirrors-solr-latest-redir.html. For your comfort, I would recommend you to set SOLR_HOME var and add it to your PATH in order to run Solr command without using the absolute path.
As explained here, we must create a new Magnolia config set by duplicating the $SOLR_HOME/server/solr/configsets/_default folder and naming it to magnolia_data_driven_schema_configs.
In this new configuration set you need to create or modify two files, solrconfig.xml and managed-schema (be aware that different Solr versions may require different content in the Solr configuration files, this wiki entry is expecting you to run Magnolia 5.7.2).
Start Solr using the command
> ./solr start
You will get something like:
Now we can check Solr status by using the following command:
> ./solr status
As the output says, Solr is running on port 8963, so we can go to http://localhost:8983/solr/#/ and check it the admin dashboard is working.
Next step is create Magnolia core. A core is a running instance of a Lucene index along with all the Solr configuration required to use it. For this purpose, we must execute the command:
> ./solr create_core -c magnolia -d magnolia_data_driven_schema_configs
If everything went ok, you will see the message: Created new core 'magnolia' and you will be able of accessing the url http://localhost:8983/solr/#/magnolia/query (or if you rather, just refresh the http://localhost:8983/solr/#/ and in the core selector you must be able of selecting the new core we just created).
- Activate and run Crawler -
By injecting the dependency 'magnolia-solr-search-provider', we will have a solr-search-provider module preconfigured that will fit our needs.
So, we are deactivating Index and activating crawling (as said above, they aren’t mutually inclusive). For that taks, we go to content-indexer/config/crawlers/ and add our site (e.g, trave_demo site). In fieldMappings we add the elements we want to retrieve from the site (in the example, image tag will only work if the images are embebbed in the app, if they are linked to dam workspace it wont work, we will need to retrieve the assets linked to the JCR elements). We set webIndexer to false and crawlers to true as explains the following image:
- Create java class to query and provide Solr search results -
This java class that will implement the getAllSolrIndexes() method (courtesy of Mikaël Geljić ) that will be invoked in our FTL from a custom templating function. The purpose of this class is provide the Solr query Search and return the matching elements. This java class is like a model class and it´s included into this sample module:
- Create FTL file -
As said above, we need to define a templating function that will be called by the FTL script and will invoke the getAllSolrIndexes() method from the java class created before. This file will also render the result into a XML file (the elements we are allowed to add to this generation are the same elements we place in the fieldsMapping configuration).
For this example I'm defining a solrfn function that will be linked to our SitemapTemplatingFunction java class:
For creating your FTL script please just copy the following snippet that invokes the getAllSolrIndexes method and generates the sitemap definition:
This document describes the XML schema for the Sitemap protocol.
Now you can access to the absolute URL of the FTL script to test the sitemap generation. You can also go to http://localhost:8983/solr/#/magnolia/query and execute a Search query on Magnolia core to see if all the expected elements are getting found and shown.