Changing the Default Search Operator from OR to AND in Solr for Magento Enterprise
In this tutorial, we’ll guide you through the process of changing the default search operator from OR to AND when using Solr with Magento Enterprise Edition (EE). This will make your searches more specific by default, ensuring that only products matching both keywords are returned.
Understanding the Default Behavior
By default, Magento EE’s Solr implementation uses the OR operator for search queries. This means that a search for “red jacket” will return results for any product containing either the word “red” or the word “jacket”, regardless of whether they appear together.
Here are the query parameters and resulting hits from the current setup:
params={
...,
q="articles_title:red+jacket*+...",
...
}
hits=7 status=0 QTime=1
params={
spellcheck=true&sort=attr_sort_score_en+desc&...
fl=id&start=0&q=(Red+jacket)&q.op=AND
hits=645 status=0 QTime=5
}
Note the q.op=AND parameter, which is added automatically but doesn’t affect the actual search behavior.
Changing the Default Operator in Solr
Solr allows you to specify the default operator used for queries. In your case, you need to change it from OR to AND.
Step 1: Update schema.xml
Open the schema.xml file located in your Solr core directory (usually /app/code/core/Solr/Conf). Find the <solrQueryParser> element and modify the defaultOperator attribute:
<solrQueryParser defaultOperator="AND"/>
Save the changes and restart your Solr server.
Step 2: Verify the Changes
Perform a search in your Magento EE store. The results should now only include products that match both keywords specified in the query.
For example, searching for “red jacket” using the above configuration will return:
- Products with red jackets
- Jackets from the Red category (if available)
- Products mentioned as having a red jacket attribute
Products containing either “red” or “jacket”, but not both together, are no longer included.
Customizing Search Behavior Further
If you need more granular control over your search behavior, consider using Solr’s LocalParams feature. This allows you to specify query options at the request handler level in solrconfig.xml.
Step 1: Locate solrconfig.xml
Find the solrconfig.xml file in your Solr core directory.
Step 2: Add LocalParams to RequestHandler
Within the <requestHandler> section for your Magento EE search (usually named “magento_en”), add a new <lst name="defaults"> element with a <str name="q.op">AND</str> child:
<requestHandler name="magento_en" class="solr.SearchHandler">
<lst name="defaults">
<str name="q.op">AND</str>
</lst>
...
</requestHandler>
Step 3: Restart Solr
Save your changes and restart the Solr server.
Conclusion
By following these steps, you should now have successfully changed the default search operator from OR to AND in your Solr-powered Magento EE store. This improves the relevance of search results by ensuring that only products matching all specified keywords are returned.
Remember to test your changes thoroughly and consider any potential impacts on existing functionality before deploying them to a live environment.
For more advanced customizations and troubleshooting, check out our comprehensive guide on Managing Solr with Magento EE.
Happy coding!