I run a site that has about 30,000 user-uploaded graphics, each of which is titled and tagged as its own node.

When uploading a graphic, users can reference another graphic that was used as a "template"... I use an entity reference field, along with a view I've created for this purpose... (see screenshot). This has worked great, but as our collection of graphics has grown, it has become clear that views is struggling in this autocomplete entity-selection role... performance is relatively bad and getting worse, and the search functionality is not adequately powerful to cope with thousands of nodes with similar titles... so trying to find the correct node to reference via the autocomplete field has become dificult and time consuming... you essentially need to know the exact title, and type it in perfectly in order to get what you want... which mostly defeats the purpose of having autocomplete search selection at all...

What I would like to do is use Apache Solr results in the entity refrence autocomplete selection widget. This would allow for improved performance, less db strain, and better results when people type in an approximate title or keywords.

I have Apache Solr and the Apache Solr Views module installed and working on my site... if I create an Apache Solr View, it even allows me to create an "Entity Reference" display (screenshot)... but this display does not show up when I edit the field in question...

Any suggestions? Should't it be possible to use Apache Solr Views for auto complete entity reference selection, as long as the entity IDs are available in the view?

Any help is much appreciated!

Comments

ShaunDychko’s picture

While just doing some preliminary searching on the same topic, it might be worth trying the other collection of modules that support Apache Solr: https://www.drupal.org/project/search_api and https://www.drupal.org/project/search_api_solr. The Search API project includes a module for Views integration.

JordanMagnuson’s picture

Project: Entity reference » Apache Solr Views

Thanks for the info on Search API. However, my site is currently utilizing the Drupal ApacheSolr module heavily, and my understanding is that it is not advisable to utilize both the ApacheSolr module, and the Search API module at the same time.

Any idea on how entity reference autocomplete could be made to work with ApacheSolr and ApacheSolr Views?

(I'm going to move this thread to the ApacheSolr Views issue queue to see if anyone there can advise...

JordanMagnuson’s picture

Project: Apache Solr Views » Entity reference

Looking into this a bit further, the main issue seems to be the fact that the entity reference views selection handler is not set up to work with a view that has a base table that is different from the base table of the entity that is beings searched.

Any view that is set up with ApacheSolr Views will have "apachesolr__solr" as the base table, which won't work with entity reference currently.

See EntityReference_SelectionHandler_Views.class.php:

if ($view->base_table == $entity_info['base table']) {
...
}

Any idea of how one could get this to work? Entity reference autocomplete would be a lot more powerful if one could utilize Solr as the backend...

MiroslavBanov’s picture

That's interesting. I was wondering what was it in apachesolr_views that prohibited the entity reference display.

Maybe you can create a different entityreference "selection" plugin in another module, and it will not do the base table check.

JordanMagnuson’s picture

Have gotten this partially working by:

1. Removing the check for $view->base_table == $entity_info['base table'] in EntityReference_SelectionHandler_Views.class.php.

2. Hardcoding $id_field_alias = 'entity_id' in entityreference_plugin_style.inc ($id_field_alias = $this->display->handler->id_field_alias does not work out of the box for the apachesolr powered view).

The autocomplete field now actually displays some results based on the apachesolr view, and if I select an item it is referenced correctly on save... but no search is actually taking place: I get the same 10 default results regardless of what text I type in the autocomplete field. See screenshot: http://i.imgur.com/wQUWEhY.png

So how can I set this up to actually search through the results using Apache Solr as the search backend?

JordanMagnuson’s picture

Here's an export of the view that I'm using:

$view = new view();
$view->name = 'solr_test_view';
$view->description = '';
$view->tag = 'default';
$view->base_table = 'apachesolr__solr';
$view->human_name = 'Solr Test View';
$view->core = 7;
$view->api_version = '3.0';
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */

/* Display: Master */
$handler = $view->new_display('default', 'Master', 'default');
$handler->display->display_options['title'] = 'Solr Test View';
$handler->display->display_options['use_more_always'] = FALSE;
$handler->display->display_options['access']['type'] = 'none';
$handler->display->display_options['cache']['type'] = 'none';
$handler->display->display_options['query']['type'] = 'views_query';
$handler->display->display_options['exposed_form']['type'] = 'basic';
$handler->display->display_options['pager']['type'] = 'full';
$handler->display->display_options['pager']['options']['items_per_page'] = '10';
$handler->display->display_options['style_plugin'] = 'table';
$handler->display->display_options['style_options']['columns'] = array(
  '_version_' => '_version_',
);
$handler->display->display_options['style_options']['default'] = '-1';
$handler->display->display_options['style_options']['info'] = array(
  '_version_' => array(
    'sortable' => 0,
    'default_sort_order' => 'asc',
    'align' => '',
    'separator' => '',
    'empty_column' => 0,
  ),
);
/* Field: Apache Solr: label */
$handler->display->display_options['fields']['label']['id'] = 'label';
$handler->display->display_options['fields']['label']['table'] = 'apachesolr__solr';
$handler->display->display_options['fields']['label']['field'] = 'label';
$handler->display->display_options['fields']['label']['label'] = '';
$handler->display->display_options['fields']['label']['element_label_colon'] = FALSE;
/* Sort criterion: Apache Solr: label */
$handler->display->display_options['sorts']['label']['id'] = 'label';
$handler->display->display_options['sorts']['label']['table'] = 'apachesolr__solr';
$handler->display->display_options['sorts']['label']['field'] = 'label';
$handler->display->display_options['sorts']['label']['order'] = 'DESC';
/* Filter criterion: Apache Solr: bundle */
$handler->display->display_options['filters']['bundle']['id'] = 'bundle';
$handler->display->display_options['filters']['bundle']['table'] = 'apachesolr__solr';
$handler->display->display_options['filters']['bundle']['field'] = 'bundle';
$handler->display->display_options['filters']['bundle']['value'] = 'graphic';

/* Display: Entity Reference */
$handler = $view->new_display('entityreference', 'Entity Reference', 'entityreference_1');
$handler->display->display_options['defaults']['title'] = FALSE;
$handler->display->display_options['pager']['type'] = 'some';
$handler->display->display_options['defaults']['style_plugin'] = FALSE;
$handler->display->display_options['style_plugin'] = 'entityreference_style';
$handler->display->display_options['style_options']['grouping'] = array(
  0 => array(
    'field' => 'label',
    'rendered' => 0,
    'rendered_strip' => 0,
  ),
);
$handler->display->display_options['style_options']['search_fields'] = array(
  'label' => 'label',
);
$handler->display->display_options['defaults']['style_options'] = FALSE;
$handler->display->display_options['defaults']['row_plugin'] = FALSE;
$handler->display->display_options['row_plugin'] = 'entityreference_fields';
$handler->display->display_options['row_options']['separator'] = '';
$handler->display->display_options['defaults']['row_options'] = FALSE;
JordanMagnuson’s picture

It occurs to me that since I want apache solr to be used as the actual backend of the autocomplete searching, the best approach to solving this issue may be to simply integrate with the Apache Solr Autocomplete module, and provide a mode for that under "entity selection mode" in the entity reference widget settings. See screenshot: http://i.imgur.com/ckr7h6P.png

Thoughts?

a.milkovsky’s picture

Is it doable in drupal 8?