Hello,

I am having difficulty creating faceted search using Search API, Facet API, Views, and Drupal Commerce. In Search API I created an index for my product display bundle and added the product entity reference field. I then added a taxonomy field which is attached to the product entity. The taxonomy field is used to specify the color of the product (each product has 1 color). I then added a facet for this taxonomy field. I created a view based on the above index and added fields from both the product display bundle and the product entity.

When I filter by color, the view returns all of the products with that particular color, but only for the product display nodes that reference 1 product. For product display nodes with multiple products (one of which contains the color I selected), the first product referenced is returned. For example:

Product display node 1
reference: product 1 (color: black)

Product display node 2
reference: product 2 (color: white)
reference: product 3 (color: black)

Filter by color: black

View Results:

Product display node 1
Product 1
color: black

Product display node 2
Product 2
color: white

The results should be:

Product display node 1
Product 1
color: black

Product display node 2
Product 3
color: black

I am not sure if this is related to user error, Facet API, Search API, or Drupal Commerce. Any help would be appreciated.

Thanks for a great module,
Paul

Comments

cpliakas’s picture

Project: Facet API » Search API
Version: 7.x-1.0-beta8 » 7.x-1.0-rc1

Hi Paul.

Thanks for posting and for the kind words. Judging by what you are describing I don't think this is an issue with Facet API since the module is really only responsible for exposing a consistent API that backend modules such as Search API can use to render facet displays in a consistent manner. In other words, Facet API doesn't handle the actual facet calculations, only the display. Bumping to Search API as a next step.

Thanks,
Chris

drunken monkey’s picture

Component: Miscellaneous » Views integration

This is an inherent limitation of the Search API, or rather the Commerce data structure. Or Views? Anything, really.
Anyways, when showing nodes you can't just filter on a specific product and then expect only that product to be returned. Instead, the node will be displayed as long as one product matches, and along with all its products. Additionally, there is currently a restriction in the Entity API's Views integration that will only show a single referenced item in some cases, which might also apply here and add to the confusion.

I can't really say how to implement your use case, only that what you describe is expected behaviour as far as I can tell.

pnigro’s picture

Thank you for the detailed explanations cpliakas and drunken monkey. If I come up with a solution, I will post it.

khiminrm’s picture

Subscribe

Esculap’s picture

Any workaround solution?

Esculap’s picture

Subscribe

florian.cathala’s picture

Subscribe

djames22’s picture

Ok, even though I barely know how Drupal works, I managed to throw together a module that accomplishes this task. There are still some kinks; however, this code is working pretty much as expected from my minimal testing. To use this, create a taxonomy with your colors or prints, then add the field to your product variation type. Once that is completed, go to Configuration-> Search API-> [your search index]-> Fields and make sure that your product variations >> print or color field is added to the index. Once that is completed, go to the facets tab and enable your product variations >> print or color. Once you have completed this, go and enable the block in whichever region you would like. To see this module's effect, add an image to each product variation and select the appropriate print or color from your print or color taxonomy field. You should now be ready to apply this very rough module.

<?php
function search_results_custom_theme() { 
  $theme = array();
  $theme['node--product--type--product--list'] = array(
    'render element' => 'node',
    'template' => '/profiles/commerce_kickstart/modules/commerce_kickstart/commerce_kickstart_product_ui/theme',
    );
  return $theme;
}

function search_results_custom_preprocess_node(&$variables){
$searcher = <strong>'search_api@product_display';</strong> // your server id
$facetApiAdapter = facetapi_adapter_load($searcher);
$activeItems = $facetApiAdapter->getAllActiveItems();
$node_products=$variables['node']->field_product['und'];
if($activeItems){
foreach($node_products as $node_product){
$product_id=$node_product['product_id'];
$product=commerce_product_load($product_id);
if($product-><strong>field_print</strong>['und'][0]['tid']==reset($activeItems)['value']){
if($product-><strong>field_images</strong>['und'][0]['uri']){
$variables['content']['<strong>product:field_images</strong>'][0]['#item']['uri']=$product-><strong>field_images</strong>['und'][0]['uri'];
$variables['content']['product:field_images'][0]['#item']['filename']=$product-><strong>field_images<strong>['und'][0]['filename'];
}
}
}
}
}
?>

I have put emphasis on the specific field names that will need to be changed (to match the machine names of your fields) in order for this module to work.

"field_print" is the machine name of my print taxonomy that I use to tag each product variation.

"field_images" is the machine name of my image field on my variation types.

"search_api@product_display" is the machine name for my search server id (I found this in the database under that facetapi table).

To apply this, use ftp or some kind of file manager and go to the root directory, then navigate to sites->all->modules and create a directory (name it whatever you want). Then create a .module file with the above code in it and then another .info file with the following:

name = Search Results Custom
description = "Provides customized search results"
package = Search
core = 7.x
dependencies[] = facetapi
dependencies[] = search_api
dependencies[] = search_api_facetapi

version = "7.x-1.x-dev"
core = "7.x"
project = "Search Results Custom"
datestamp = "1380626876"

I know that this module is probably very inefficient and hackish, but I'm not really a programmer and this was the best I could come up with in a few days. Hopefully a similar feature can be added in to the drupal commerce search core.

Hope this helps somebody =)

harings_rob’s picture

That doesnt really look like a viable solution.

What might be usefull:

Make it so that that it uses the first referenced product.

And build the products like this

Prod 1:
Color 1
Color 2

Prod2:
Color 2
Color 1

IckZ’s picture

Is there any better workarround for this issue now?

drunken monkey’s picture

There is the Search API Grouping module now, which might help with this scenario by indexing the products separately.

legolasbo’s picture

Status: Active » Closed (outdated)

This issue has not seen activity in over 2,5 years. I am therefore closing this issue to clean up the issue queue. Feel free to re-open and update this issue if you feel this issue is still relevant and of importance.