cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

SOLR free text search with AND operator between words

0 Likes
2,413

By default, SOLR searches with multiple words, returns results with an OR operator between the words

For ex: If I search for black pant, it will return all results with word black as well as all products with word pant

I need it to be an AND operator, i.e. return products that has both black and pant in it.

Can this be achieved with simple configuration changes at either hybris or solr end. If not, how exactly can we modify the java classes to achieve this?

Accepted Solutions (0)

Answers (2)

Answers (2)

disaac
Discoverer
0 Likes

I think part of the issue here is that the two terms may be found in different Solr attributes. For example there may be both a field for "color" and another one for "clothingType". For this reason the Phrase Query Slop will not help.

Currently, Hybris is generating a SolrQuery similar to the following:

[SolrQueryDebuggingListener]

Parsed Solr Query:

+(DisjunctionMaxQuery((color_string:black | clothingType_string:black))

DisjunctionMaxQuery((color_string:pant | clothingType_string:pant))

DisjunctionMaxQuery((someOtherField_text:*black* | yetAnotherField_text:*black*))

DisjunctionMaxQuery((someOtherField_text:*pant* | yetAnotherField_text:*pant*))

DisjunctionMaxQuery((someDescriptionField_string:black pant | yetAnotherDescriptionField_text"black pant" | andAnotherField_text_en_string_mv:black pant)))

We would probably want something more like the following:

[SolrQueryDebuggingListener]

Parsed Solr Query:

(DisjunctionMaxQuery((color_string:black | clothingType_string:black) & (color_string:pant | clothingType_string:pant))

DisjunctionMaxQuery((someOtherField_text:*black* | yetAnotherField_text:black*) & (someOtherField_text:*pant* | yetAnotherField_text:pant*))

DisjunctionMaxQuery((someDescriptionField_string:black pant | yetAnotherDescriptionField_text"black pant" | andAnotherField_text_en_string_mv:black pant)))

disaac
Discoverer
0 Likes

Actually, we probably need the query to be a bit more like the following:

[SolrQueryDebuggingListener]

Parsed Solr Query:

+(DisjunctionMaxQuery(

(color_string:black | clothingType_string:black | someOtherField_text:*black* | yetAnotherField_text:*black*)

&

(color_string:pant | clothingType_string:pant | someOtherField_text:*pant* | yetAnotherField_text:*pant*))

DisjunctionMaxQuery((someDescriptionField_string:black pant | yetAnotherDescriptionField_text"black pant" | andAnotherField_text_en_string_mv:black pant)))

mansurarisoy
Contributor
0 Likes

I suggest you to look for Solr forums to get more accurate answer to your question.

I think your need is similar to the question in the following link: https://stackoverflow.com/questions/18224527/performing-exact-match-on-solr-search

There are two solutions proposed in the question.

  1. You could either search for Black AND Pant or change your default operator in schema.xml to be AND instead of OR. Beware that this setting is global. I think the default operator change may be done easily but this setting may be removed from Solr in current releases. The first option may need a lot of customization in Hybris side and may have side effects.
  2. Modifying query parser in solrconfig.xml by setting mm field like <str name="mm">100%</str> or setting an additional attribute for query parameter. Modifying solrconfig.xml should be easy piece but may affect all field types. Setting an additional when querying may need customization.

You may also use Free Text Phrase Query with slop value which satisfies your needs, but this does not cover all your needs. For example if you set slop value as 1 the search result may include "Black Short Pant" but not "Black Short Cotton Pant". And you also need to disable Free Text Query on the field.

Hope this helps,