1

I am working on a project where i would like to be able to create a search api view that has a relation to the referencing entity.

The use case:

I have a content type: Products page that holds the commercial information about the product

I then have a content type Product that holds all the specifics (article number, serial numbers etc)

I have created a search api view on the content type Products Page that shows all the commercial information in the catalog with some facets end an exposed full text search box.

Each product node has an entity reference to the Products page node it belongs to

Of course, i would like visitor to be able to search for article numbers in the fulltext search. The result needs to to show the Products Page node that is being referenced by the product node that holds the article number that was searched for.

However i cannot see how i can create the relation that you would normally use when using a regular view?

Addtionally and example below:

Products Page node: Chairs (with body and image) Product nodes: - Wooden chair (art nr 123, and entity ref to Chairs) - Metal chair (art nr 456, and entity ref to Chairs)

So when ppl search art nr 123, i want my view to show the Products page node "chairs".

Thanks in advance!

  • Honestly, I would set up a search through the normal means where the user can find 'Metal chair' or 'Wooden chair' which is a result for a single node, but redirect them via the template file when they click on it. – Darvanen Mar 4 '15 at 12:49
1

Implement hook_entity_property_info_alter()

function MY_MODULE_entity_property_info_alter(&$info) {
    $info['node']['properties']['article_number'] = array(
        'type' => 'text',
        'label' => t('Article number'),
        'getter callback' => 'MY_MODULE_get_article_number',
    );
}

function MY_MODULE_get_article_number($node) {
    if ($node->type == 'product_page') {
        // Return the article number from the referenced product node
    }
}

Go to admin/config/search/search_api/index/{index_name}/fields and select the property. Now you can use this property like any other field.

In your case you need to configure this field in you view. I assume you added Search: Fulltext search to your filter criteria. Click on this filter and add the property to the Searched fields settings.

I hope this works for you.

| improve this answer | |
1

You could also try https://www.drupal.org/project/entityreference_backreference as a more generic solution.

| improve this answer | |
0

I've just created this configuration and it worked for me.

Create the index, lets say Products index.

In the Fields Tab, select the fields you want to index from the main content, which in your case should be art nr in full text mode.

Then go to the filters tabs and select Bundle filter then check Only those from the selected bundles and select Product.

Then clear all index and index again.

Now you can create a view for the index, then create the relation and add the filelds you want to show checking the relation.

Now expose the fulltext search, and search for the number.

You can't apply a relation to the Rendered entity as you can do to content in normal views but i think you can handle with fields to show the result data.

| improve this answer | |
0

You can add an extra property to your entity via hook_entity_property_info_alter(). Return the article ID via the defined getter callback. Select the property in the fields settings of your search api index.

In your view you can add this property to the "Searched fields" setting of the "Fulltext search" filter.

If you need more details, let me know.

| improve this answer | |
  • Dear Jasper, I read a little bit about this, but could not find a good explanation on this (imho), any elaboration on this is very welcome! – Guardian Mar 9 '15 at 15:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.