I used views with search api. I added facets on a sidebar.

On initial load, the facets display properly. But when I refresh the page, it dissappears.

But when I change url by adding random url parameters, it displays. It seems that it only displays the facets on first load of a url.

I tried disabling all caching, including twig but it still happens.

Am I the only one experiencing this?
Let me know how can I help

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

bryanmanalo created an issue. See original summary.

borisson_’s picture

Issue tags: +D8 cacheability

This sounds like a caching issue, I've tagged it as such and I'll have a look at how to reproduce it. Do you have page cache / dynamic page cache enabled?

borisson_’s picture

Status: Active » Postponed (maintainer needs more info)

I've reinstalled drupal, search API and facets and I can't reproduce this.

bryanmanalo’s picture

Hmmm... that is weird. I tried it again just now, and was able to reproduce it using the following steps.

Steps to reproduce:

Download and enable the required modules:
1.) Download drupal 8.0.3
2.) Download facets
3.) Enabled facets (using drush)
4.) Download search_api
5.) Enable search_api and search_api_db (using drush)

Create sample content
6.) Add 2 new articles placing values on field_tags, and title.

Create search api server and index
7.) Create a search_api server selecting a database backend
8.) Create an index selecting content as datasource
9.) Add fields to index, selecting title and field_tags on the content subgroup.
10.) Do not modify processors

Create a view
11.) Create view of type index.
12.) Added fields to the view selecting indexed title, and indexed field_tags

Create facet
13.) Create a new facet, setting search api view as facet source, and tags as facet field
14.) Place block on the same url as the view.

Test it
15.) Go to the url of the created view.
16.) Refresh the page, and see the result.

Hope this helps. I'll try to debug more next week.

I have tried turning on/off page cache and dynamic cache, and got the same result.

bryanmanalo’s picture

I noticed that this only happens when I create my own database and index. It doesn't happen when I use the default search_api database and index (search_api_db_defaults).

On SearchApiVewsPage

  public function fillFacetsWithResults($facets) {
    // Check if there are results in the static cache.
86    $results = $this->searchApiResultsCache->getResults($this->pluginId);

    // If our results are not there, execute the view to get the results.
    if (!$results) {
      // If there are no results, execute the view. and check for results again!
      $view = Views::getView($this->pluginDefinition['view_id']);
      $view->setDisplay($this->pluginDefinition['view_display']);
      $view->execute();
94      $results = $this->searchApiResultsCache->getResults($this->pluginId);
    }

Line 86 is the culprit. Using my own db and index, this line returns null. Line 94 also returns null. But using search_api_db_defaults, line 86 always has a result.

OlgaRabodzei’s picture

Status: Postponed (maintainer needs more info) » Needs review
FileSize
509 bytes

The problem was in the returning type. The function \Drupal\search_api\Query\ResultsCacheInterface::getResults() must return "The results with the given search ID, if present; NULL otherwise.". But in \Drupal\facets\Plugin\Block\FacetBlock::getCacheMaxAge() we returned 0, so it was the mistake. Attached patch fixed this.

borisson_’s picture

getCacheMaxAge has nothing to do with the results. This sets the cacheability max age, see Drupal\Core\Cache\CacheableDependencyInterface

Status: Needs review » Needs work

The last submitted patch, 6: facets-fix-page-refresh-2663552-6.patch, failed testing.

The last submitted patch, 6: facets-fix-page-refresh-2663552-6.patch, failed testing.

alan-ps’s picture

I think that cache of views module is a main cause. From ViewExecutable::execute()

    if ($cache->cacheGet('results')) {
      if ($this->pager->usePager()) {
        $this->pager->total_items = $this->total_rows;
        $this->pager->updatePageInfo();
      }
    }
    else {
      $this->query->execute($this);
      // Enforce the array key rule as documented in
      // views_plugin_query::execute().
      $this->result = array_values($this->result);
      $this->_postExecute();
      $cache->cacheSet('results');
    }

So, SearchApiQuery::execute() won't be executed, if $cache->cacheGet('results') returns some data. As a result hook_search_api_query_alter() won't be executed and facets won't be displayed. Probably, it should be fixed in a search_api module?

OlgaRabodzei’s picture

OlgaRabodzei’s picture

Hi! I think that I found the solve. It's a mistake in view's settings.
So, go to your_view - Edit - advanced - caching and choose there "Search API specific"

borisson_’s picture

So "search api specific" or no caching should be the same thing, search api doesn't cache anything either (they also have max-age: 0). We have some code that disables views caches when saving a facet. http://cgit.drupalcode.org/facets/tree/src/Form/FacetForm.php#n325

Maybe we should look into disabling other cacheing abilities when facets are enabled on a view?

bryanmanalo’s picture

@borisson_ I have some time to work on this issue. On a high-level, do we have an understanding on what is causing the problem? Can you point me to the right direction?

borisson_’s picture

Version: 8.x-1.0-alpha1 » 8.x-1.x-dev

@bryanmanalo: you should first read up on the reason why we decided to disable cacheing for facets: http://cgit.drupalcode.org/facets/tree/src/Plugin/Block/FacetBlock.php?i...

The relevant snippet from that documentation is:

Search API gets those search results from a data source that can be external to Drupal. Therefore it is impossible to guarantee that the search results are in sync with the data managed by Drupal.

Facets won't work on a cached view for the foreseeable future, supporting that would be an absolute nightmare for the reason mentioned in there. What should happen in this issue is document that more clearly in the UI and/or readme file. If possible we should try to make sure that the cacheability options for the view are limited so people won't accidently disable the cacheing options.

OlgaRabodzei’s picture

@borisson_ : You are right about "search api specific" and "none" caching settings. They are the same. But when we create view by default cache specified as "Tag based".

borisson_’s picture

@OlgaRabodzei: as mentioned in #13, facets has code that should set the view's cacheability to "none". If that doesn't work anymore, we should figure out why not and fix that code (and possibly add a test for it as well)

bryanmanalo’s picture

I did not understand what is going at first but your comment #17 helped me. Once I set the caching to none, the facet is now displaying consistently.

I'll help check why the view is not set to none when selected as facet source.

Just a quick follow-up, was caching also disabled for facet_api to work in D7 and views? Is this issue only specific to using views as a facet source? What if I use a search results page do display the facets (not views)?

alan-ps’s picture

Status: Needs work » Needs review
FileSize
593 bytes

about d7: https://www.drupal.org/node/1612708

Regarding current issue: we should set current display for a cache option ('default' is used by default), before facet will be saved. Otherwise, we should use this:

- $display = &$view->storage->getDisplay($display);
+ $display = &$view->storage->getDisplay('default');

But we don't want change a cache type for all view. So, we should set a display before changing the options. I have attached a patch for review. It should help:)

borisson_’s picture

FileSize
1.44 KB
2.02 KB

I added a note in the readme.txt, which we need.

I also added a hook to remind people to disable views cacheing when saving a view that uses search api.
I think this is too invasive personally but let me know if y'all think this is better?

alan-ps’s picture

FileSize
58.9 KB

As for me this is unnecessary and note in the readme.txt will be enough.

In addition: does it mean that we should remove ability to change cache type? I mean this: http://cgit.drupalcode.org/facets/tree/src/Form/FacetForm.php#n325
The message looks like that we should change cache type manually.

Also, I'm not sure about all displays (see attached file). Probably, current display will be enough for this?
However, as I mentioned above, note in the readme.txt will be enough on my mind.

borisson_’s picture

FileSize
2.04 KB
1.71 KB

So, I think this should be enough information in the readme.

alan-ps’s picture

Status: Needs review » Reviewed & tested by the community

RTBC :)

borisson_’s picture

Status: Reviewed & tested by the community » Fixed

Committed, thanks to everyone for the help!

  • borisson_ committed 9022544 on 8.x-1.x
    Issue #2663552 by borisson_, alan-ps, OlgaRabodzei, bryanmanalo: Facets...

Status: Fixed » Closed (fixed)

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

claudiu.cristea’s picture

Well, I'm not satisfied with the idea that facets cannot be procesed on top of a cached view. I think we should support that. I have no glue how, given the actual architecture.