Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

  1. Install and configure Solr

  2. Activate and run Crawler
  3. Create java class to query and provide Solr search results
  4. 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/)

...

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:

Code Block
languagejava
themeEclipse
public class SitemapTemplatingFunction {

    private static final Logger log = LoggerFactory.getLogger(CorporateWebsiteTemplatingFunctions.class);

    public Collection<SolrDocument> getAllSolrIndexes() throws SolrServerException, IOException {
        return getAllSolrIndexes(null, null);
    }

    public Collection<SolrDocument> getAllSolrIndexes(String type) throws SolrServerException, IOException {
        return getAllSolrIndexes(type, null);
    }

    public Collection<SolrDocument> getAllSolrIndexes(String type, String... fields) throws SolrServerException, IOException {
        MagnoliaSolrBridge magnoliaSolrBridge = Components.getComponent(MagnoliaSolrBridge.class);
        SolrClient solrClient = magnoliaSolrBridge.getSolrClient();

        SolrQuery solrQuery = new SolrQuery();
        solrQuery.setQuery("*");
        solrQuery.setStart(0);
        solrQuery.setSort("url", SolrQuery.ORDER.asc);

        // perform query with zero rows, to resolve number of results
        solrQuery.setRows(0);
        QueryResponse queryResponseNumberOfResults = solrClient.query(solrQuery);

        // resolve number of results
        int numberOfResults = Long.valueOf(queryResponseNumberOfResults.getResults().getNumFound()).intValue();
        log.debug("Found {} document(s)", numberOfResults);
        solrQuery.setRows(numberOfResults);

        if (fields != null && fields.length > 0) {
            solrQuery.setFields(fields);
        }
        solrQuery.addField("url");

        if (type != null) {
            solrQuery.addFilterQuery("type:" + type);
        }

        // perform query to get all result
        QueryResponse queryResponse = solrClient.query(solrQuery);
        return queryResponse.getResults();
    }
}

- 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).

...