Migrating a site from Solr to OpenSearch
If your project/site was built with Solr for search, then you’ll need to upgrade to OpenSearch when upgrading to CrafterCMS 4.1. For that, you’ll need to update the code of all existing projects/sites to use OpenSearch client and query language.
Updating to OpenSearch
To update your site to use OpenSearch instead of Solr you can follow these steps:
Overwrite the target in the Deployer to use OpenSearch instead of Solr
Index all existing content in OpenSearch
Find all references to
searchService
in your FreeMarker templates and replace them with the OpenSearch clientFind all references to
searchService
in your Groovy scripts and replace them with the OpenSearch clientDelete the unused Solr core if needed (can be done using the Solr Admin UI or the
data/indexes
folder)Update
craftercms-plugin.yaml
to use OpenSearch as the search engine
Overwrite the target
For authoring environments:
1curl --request POST \
2 --url http://DEPLOYER_HOST:DEPLOYER_PORT/api/1/target/create \
3 --header 'content-type: application/json' \
4 --data '{
5 "env": "preview",
6 "site_name": "SITE_NAME",
7 "template_name": "local",
8 "repo_url": "INSTALL_DIR/data/repos/sites/SITE_NAME/sandbox",
9 "disable_deploy_cron": true,
10 "replace": true
11 }'
For delivery environments:
1curl --request POST \
2 --url http://DEPLOYER_HOST:DEPLOYER_PORT/api/1/target/create \
3 --header 'content-type: application/json' \
4 --data '{
5 "env": "default",
6 "site_name": "SITE_NAME",
7 "template_name": "remote",
8 "repo_url": "INSTALL_DIR/data/repos/sites/SITE_NAME/published",
9 "repo_branch": "live",
10
11 ... any additional settings like git credentials ...
12
13 "replace": true
14 }'
Note
For a detailed list of parameters see createTarget
The create target operation will also create the new index in OpenSearch.
Index all site content
To reindex all existing content execute the following command:
1curl --request POST \
2 --url http://DEPLOYER_HOST:DEPLOYER_PORT/api/1/target/deploy/ENVIRONMENT/SITE_NAME \
3 --header 'content-type: application/json' \
4 --data '{
5 "reprocess_all_files": true
6 }'
Update the site code
Because both Solr and OpenSearch are based on Lucene, you will be able to keep most of your queries unchanged, however features like sorting, facets and highlighting will require code changes.
Note
To take full advantage of OpenSearch features it is recommended to replace query strings with other type of queries provided by the OpenSearch DSL
Warning
If you are using any customization or any advance feature from Solr, you will need to find an alternative using OpenSearch.
To update your code there are two possible approaches:
Examples
This is a basic example of replacing Crafter Search service with OpenSearch
1def q = "${userTerm}~1 OR *${userTerm}*"
2
3def query = searchService.createQuery()
4query.setQuery(q)
5query.setStart(start)
6query.setRows(rows)
7query.setParam("sort", "createdDate_dt asc")
8query.setHighlight(true)
9query.setHighlightFields(HIGHLIGHT_FIELDS)
10
11def result = searchService.search(query)
12
13def documents = result.response.documents
14def highlighting = result.highlighting
Using the OpenSearch Client the code will look like this:
1import org.opensearch.client.opensearch._types.SortOrder
2
3def q = "${userTerm}~1 OR *${userTerm}*"
4
5// Execute the query
6def result = OpenSearchClient(r -> r
7 .query(q -> q
8 .queryString(s -> s
9 .query(q as String)
10 )
11 )
12 .from(start)
13 .size(rows)
14 .sort(s -> s
15 .field(f -> f
16 .field(createdDate_dt)
17 .order(SortOrder.Asc)
18 )
19 )
20 .highlight(h -> {
21 HIGHLIGHT_FIELDS.each { field ->
22 h.fields(field, f -> f)
23 }
24 })
25, Map)
26
27// OpenSearch response (highlight results are part of each hit object)
28def documents = result.hits().hits()
For additional information you can read the official Java Client documentation and DSL documentation.
Notice in the given example that the query string didn’t change, you will need to update only the code that builds and executes the query. However OpenSearch provides new query types and features that you can use directly from your Groovy scripts.
If any of your queries includes date math for range queries, you will also need to update them to use the OpenSearch date math syntax described here.
Example
1createdDate_dt: [ NOW-1MONTH/DAY TO NOW-2DAYS/DAY ]
1createdDate_dt: [ now-1M/d TO now-2d/d ]
In Solr there were two special fields _text_
and _text_main_
, during indexing the values of other fields were
copied to provide a simple way to create generic queries in all relevant text. OpenSearch provides a different
feature that replaces those fields Multi-match query
Example
1_text_: some keywords
1.multiMatch(m -> m
2 .query('some keywords')
3)
OpenSearch also offers the possibility to query fields with postfixes using wildcards
1.multiMatch(m -> m
2 .query('some keywords')
3 .fields('*_t', '*_txt', '*_html')
4)
Update “craftercms-plugin.yaml” to use OpenSearch
Your site has a craftercms-plugin.yaml
file that contains information for use by CrafterCMS.
We’ll have to update the file to use OpenSearch as the search engine.
Edit your craftercms-plugin.yaml
, and remove the following property:
1searchEngine: CrafterSearch
And make sure to commit your changes to craftercms-plugin.yaml
.