I'm running commerce alongside solr to improve search functionality, but I'm struggling to find a way to handle this use case. I need my search results to be able to filter out items with zero quantity (but still have them visible in case a customer wants to check prices on out of stock products). I'm currently doing this by indexing the stock level of the referenced products, but it's obviously not ideal.

When I update stock quantities using the views editable table module, the nodes aren't marked as updated, so they aren't marked for reindexing. Being commerce products, I'm not even sure if they would affect the product display that's being indexed, but that's a different kettle of fish.

Ideally what I need is to be able to pull the relationship of the referenced products into the search view, then allow to filter the search results by the quantities on the related entities. I can't see any way that I can filter search results without using indexed fields though. Is this a hard coded limitation? Is there another way I can achieve this?

Comments

sjvellenga created an issue. See original summary.

sjvellenga’s picture

Found a workaround for the moment, but I would still like to see a solution that doesn't require indexing.

Rules came to the rescue. On saving content, I'm running an immediate index on the product (which carries over to the product display in my use case). It's going to be more intensive on the server, but hey, if it works, it works.

drunken monkey’s picture

Status: Active » Fixed

Rules came to the rescue. On saving content, I'm running an immediate index on the product (which carries over to the product display in my use case). It's going to be more intensive on the server, but hey, if it works, it works.

Good to hear – that's more or less exactly what I would have suggested. Not ideal, as you say, but it should work.

I can't see any way that I can filter search results without using indexed fields though. Is this a hard coded limitation?

Yes, it is. If the field is not indexed, Solr can't know about it, so there wouldn't be any chance of filtering on that without messing up paging, facets, etc.
If messing up paging, facets and possibly other data would be acceptable, you can implement some custom solution by just going through the returned results and removing those with empty stock.(hook_search_api_results_alter() or hook_search_api__solr_search_results_alter(), or with Views hooks/plugins.)
Or, if there is just a small number of products with no stock, you can retrieve them from the database at search time and just add a nid NOT IN (…) condition to the query. (How to actually do that depends on whether you add it at the Search API or Solr level.)
Finally, you can use Solr atomic updates to manually update just the stock level when that changes, instead of using Rules to re-index the entire document.

Also, if you aren't interested in the actual stock level, just whether it's zero or not, you can try to change the mechanism (either in Rules, if possible, or custom code) to only re-index when the stock level changes to/from 0.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.