Versions Compared

Key

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

...

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: 

Code Block
languagejava
themeEclipse
public class SitemapTemplatingFunction {

    private static final Logger log = LoggerFactory.getLogger(SitemapTemplatingFunction.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();
    }
}

...

For this example I'm defining a corpfn solrfn function that will be linked to our SitemapTemplatingFunction java class:

Image RemovedImage Added

For creating your FTL script please just copy the following snippet that invokes the getAllSolrIndexes method and generates the sitemap definition:

...

Code Block
languagexml
themeConfluence
<?xml version="1.0" encoding="UTF-8"?>
[#compress]<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
[#assign searchResults = corpfnsolrfn.getAllSolrIndexes() /]
[#list searchResults as item]

    [#assign slashCount = item["url"]?remove_ending("/")?split("/")]
    [#assign priority = (13 - slashCount?size)/10]

    <url>
        <changefreq>weekly</changefreq>
        <loc>${item["url"]?html}</loc>
        <priority>${priority}</priority>
    </url>

[/#list]
</urlset>
[/#compress]

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.

Sample Module

In git repository https://git.magnolia-cms.com/projects/SERVICES/repos/solr_sitemap/ there is a module which has the neccesary templates and class to generate a Sitemap based in SOLR. Module has the following contents:

  • Java class: SiteMapTemplatingFunction.java with the templating function necessary to get all Solr Indexes
  • Decorations folder: to modify Travel Demo templates and dialogs to allow hide pages in Sitemap.
  • Templates folder: with Sitemap template to use directly in Pages App. 

Usage of the module: You have to install the module and register the Templating function as described before