Solr For e-Commerce Search Engine - Exact AND Partial Matches

First off, I’m not sure where to put this post… I thought there used to be an ecommerce related topic in the forums.

I am implementing Solr on a custom written PHP eCommerce application, using the PHP Solarium library to interface with Solr. I have a couple questions on the best way to query Solr. Users can search for products using either product skus (part numbers) or general keywords. What I’d like to do is if someone enters an exact part number into the search box, the customer is brought right to the exact item. If it isn’t an exact match to a sku, then just perform a normal search (looking at partial part numbers or keywords). The only way I can think of doing this is to run a search twice - first run a query on a string field to check for an exact match. If the results returned are equal to one, then display the match to the user, otherwise send another search to Solr querying all of the fields that need to be checked (sku, product name, description, etc.) and return all relevant matches. Is this the best way to go about this?

A specific example of this would be a product with a sku of ABC123456. A user may search for ABC123456 and in that scenario I want to simply redirect the user to the item page for ABC123456. However if a user searches for 123456 there would be no exact match on the sku so I would then treat 123456 as a keyword and query all fields on that keyword.

Is there a better way to go about this using Solr? Any suggestions? I’ve set up several fields in my Solr schema. Example:

  • sku = text_en_splitting_tight
  • sku_exact = string
  • name = text_general
  • description = text_general
  • etc…

I’ve taken all of the text related fields and copied them into a single field called text. Right now I have the search app query sku_exact:[search term] OR text:[search term]. Results are somewhat accurately being returned but since I’m matching sku OR text fields, if I enter a sku number it’s also returning results based on the text field, especially when the search term contains dashes (as an example).

I’m kind of stuck and any guidance would be appreciated. Thanks!