I'm using Solr with Magento Enterprise. I'm trying to change the default search operator from OR
to AND
to make searches more specific by default.
The first thing I tried was to to change defaultOperator
in schema.xml
which did not have the desired effect (it started using AND
between fields, not keywords).
<solrQueryParser defaultOperator="AND"/>
I then read about LocalParams and tried adding that to several requestHandler
sections in solrconfig.xml
(I'm just guessing where it's supposed to go, I can't find any helpful documentation).
<requestHandler name="magento_en" class="solr.SearchHandler">
<lst name="defaults">
<str name="q.op">AND</str>
I also poked around in the code (app/core/core/Enterprise/Search
), hard-coded {!q.op=AND}
to the queries but still couldn't get it to work.
I imagine it's a simple configuration change, can anyone point me in the right direction?
Edit: To clarify, a search for "red jacket" (without quotes) should return results for "red AND jacket". I'm only interested in products that are actually red jackets, not red shoes and/or blue jackets. A manual search for "red AND jacket" returns the results that I'm after.
Currently a search performs these queries:
INFO: [] webapp=/solr path=/select params={start=0&q=articles_title:red+jacket*+articles_summary:red+jacket*+articles_text:red+jacket*+cms_title:red+jacket*+cms_content:red+jacket*&json.nl=map&wt=json&fq=store_id:1+store_id:0&version=1.2&rows=4} hits=7 status=0 QTime=1
09/01/2013 10:46:21 AM org.apache.solr.core.SolrCore execute
INFO: [] webapp=/solr path=/select params={spellcheck=true&sort=attr_sort_score_en+desc&spellcheck.extendedResults=true&json.nl=map&wt=json&spellcheck.collate=true&version=1.2&rows=1&fl=id&start=0&q=(Red+jacket)&spellcheck.dictionary=magento_spell_en&q.op=AND&spellcheck.count=2&qt=magento_en&fq=(visibility:3+OR+visibility:4)+AND+store_id:1} hits=645 status=0 QTime=5
09/01/2013 10:46:21 AM org.apache.solr.core.SolrCore execute
INFO: [] webapp=/solr path=/select params={facet=on&sort=score+desc&json.nl=map&wt=json&version=1.2&rows=24&fl=id&start=0&facet.query=category_ids:8&facet.query=category_ids:46&facet.query=category_ids:88&facet.query=category_ids:126&facet.query=category_ids:168&facet.query=category_ids:180&facet.query=category_ids:207&facet.query=category_ids:224&facet.query=category_ids:242&facet.query=category_ids:276&q=(Red+jacket)&q.op=AND&facet.field=attr_nav_multi_colourway&qt=magento_en&fq=(visibility:3+OR+visibility:4)+AND+store_id:1} hits=645 status=0 QTime=5
09/01/2013 10:46:22 AM org.apache.solr.core.SolrCore execute
INFO: [] webapp=/solr path=/select params={facet=on&sort=attr_sort_score_en+desc&json.nl=map&wt=json&rows=100&version=1.2&start=0&facet.query=category_ids:8&facet.query=category_ids:46&facet.query=category_ids:88&facet.query=category_ids:126&facet.query=category_ids:168&facet.query=category_ids:180&facet.query=category_ids:207&facet.query=category_ids:224&facet.query=category_ids:242&facet.query=category_ids:276&q=(Red+jacket)&q.op=AND&facet.field=attr_nav_multi_colourway&qt=magento_en&fq=(visibility:3+OR+visibility:4)+AND+store_id:1} hits=645 status=0 QTime=6
09/01/2013 10:46:22 AM org.apache.solr.core.SolrCore execute
INFO: [] webapp=/solr path=/select params={facet=on&sort=attr_sort_score_en+desc&json.nl=map&wt=json&rows=100&version=1.2&start=0&facet.query=category_ids:8&facet.query=category_ids:46&facet.query=category_ids:88&facet.query=category_ids:126&facet.query=category_ids:168&facet.query=category_ids:180&facet.query=category_ids:207&facet.query=category_ids:224&facet.query=category_ids:242&facet.query=category_ids:276&q=(Red+jacket)&q.op=AND&facet.field=attr_nav_multi_colourway&qt=magento_en&fq=(visibility:3+OR+visibility:4)+AND+store_id:1} hits=645 status=0 QTime=3
Thanks to Macilias' link on the dismax parser plugin I've found a way to accomplish this with the settings in the solrconfig.xml. In this file there are requestHandler nodes for a bunch of different languages. I modified the english one since our store is in english. By default the xml looked like this:
<requestHandler name="magento_en" class="solr.SearchHandler">
<lst name="defaults">
<str name="qf">fulltext_1_en^1.0 fulltext_2_en^2.0 fulltext_3_en^3.0 fulltext_4_en^4.0 fulltext_5_en^5.0</str>
<str name="pf">fulltext_1_en^1.0 fulltext_2_en^2.0 fulltext_3_en^3.0 fulltext_4_en^4.0 fulltext_5_en^5.0</str>
<int name="ps">1</int>
<str name="mm">1</str>
<str name="defType">dismax</str>
<str name="echoParams">explicit</str>
<str name="spellcheck.onlyMorePopular">false</str>
<str name="spellcheck.extendedResults">false</str>
<str name="spellcheck.count">1</str>
</lst>
<arr name="last-components">
<str>spellcheck</str>
</arr>
</requestHandler>
The important parameter here is "mm" which stands for Minimum 'Should' Match. The dismax parser uses this instead of a default operator to determine how multiple search terms should be handled. A value of 1 means only one term from the query must match (same behavior as OR). A value of 100% mean all the terms must match (same behavior as AND). More complex values can be used as well. Follow the link above for more info. After changing settings in the solrconfig.xml file you'll need to restart the Solr server before they take effect.
This video is also a good Magento Solr resource: http://www.youtube.com/watch?v=07uIJSXdqpU They talk about Minimum Match around the 24 minute mark.
I ended up using q.op which changed the operator to AND instead of OR. For example:
?q=text:small cars&q.op=AND
Try the following (untested):
q={!q.op=AND df=articles_title}red jacket&fq=articles_summary:(red AND jacket)&fq=articles_text:(red AND jacket)
and the rest of the fields are used in a similar fashion with fq
parameter.
The above will return all those records where all the mentioned fields contain term red and jacket. However, if you are required to return a record where atleast one field contains red AND jacket, then I suggest that you use a copyfield to map all those fields to a single field and then search against the copyfield type.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With