Hi there. I could use some support getting search facets up and running.

I'm setting up a Drupal Commerce web store, and want to enable facetted searching for the products within the store. Eventually I'd like to filter by price, brand, etc. For now, however (for the sake of this post), I'm focused on a brand facet (filter based on a taxonomy field for the brand).

I'm certain I have all the correct api's / modules installed (definitely: facet api, search api, entity api, search facets, search views). I've created the search server (just a local database search) and Product Display index, along with a search view that filters for the full text search. Everything works great with the view, it displays everything when no search term is entered, and doing simple searches by keyword returns just the expected results (can be sorted, etc). I'll note that in this view, the field for the "brand" is also populated properly. So far so good.

I start to have problems as soon as I do anything with facets. As soon as I enable any facet (on the search index settings tab for "facets") then the search-view stops showing any results. I set the block up for the corresponding facet to display in a sidebar, but it also doesn't show anything (except the "empty text" if I've set it). This leads me to think the facet has no items for some reason? I've tried to "add facet for missing values" or set the "minimum" to 0, but I still never get any closer... the search-view displays absolutely no results, and the facet block only displays the empty text.

If I disable that facet, then the search view returns to displaying / sorting correctly again.

What am I missing? From what I've read, facet search should be possible with this collection of modules. In fact, the demo store for commerce kickstart seems to do a pretty good job of providing the facets also. I've reviewed everything I can imagine and just can't seem to get it running for the life of me. Any pointers appreciated.

Estimated Value and Story Points

This issue was identified as a Beta Blocker for Drupal 8. We sat down and figured out the value proposition and amount of work (story points) for this issue.

Value and Story points are in the scale of fibonacci. Our minimum is 1, our maximum is 21. The higher, the more value or work a certain issue has.

Value : 2
Story Points: 2

Comments

cpliakas’s picture

Hi JemBijoux.

Facet API is simply a display layer an abstraction for interaction with the backend. Drupal Commerce = Search API, so I would start there actually performs the facet calculations.

Thanks,
Chris

StaceyF’s picture

For anyone else having this issue, I solved this by giving the database user connected with the drupal website "CREATE TEMPORARY TABLES" permissions. Once I enabled this, everything worked properly. Hope it helps someone else!

user654’s picture

.

cpliakas’s picture

Project: Facet API » Search API Database Search
Component: Usability » Code
Issue summary: View changes

Moving to the Search API Database Search's queue.

user654’s picture

.

StaceyF’s picture

@pinkonomy:

Sorry for the delay (been on holidays). Not sure if you've figured it out by now but how you'll set the "CREATE TEMPORARY TABLES" permission will depend on you are able to administer your website's database and it's users. For me, that was using the cPanel control panel that came with the hosting. The hosting company this website was using had the ability enabled for me to create database users and set their corresponding permissions (click on the the database user name assigned to the database). Your setup may be different in cPanel if that is what your hosting is using. If you are still having issues I'd recommend contacting your hosting company as they should be able to help you. If you're working locally, you can use the command line to set permissions.

I hope that helps.

drunken monkey’s picture

Version: 7.x-1.2 » 7.x-1.x-dev
Category: Support request » Bug report
Status: Active » Needs review
StatusFileSize
new4.33 KB

While I don't think we can/should do anything about this not working without the proper permission, we should still properly catch and report the exception, return the other search results normally and also note this problem in the README.txt. Thanks for bringing it to my attention!

Patch attached, please review!

drunken monkey’s picture

Title: Search facets have no items when they should; search results empty upon enabling any facet. » Facets require "CREATE TEMPORARY TABLES" permission
drunken monkey’s picture

Status: Needs review » Fixed

Re-rolled and committed.

  • Commit 4c8b55a on 7.x-1.x by drunken monkey:
    Issue #1912332 by drunken monkey: Fixed error when facet calculation...

Status: Fixed » Closed (fixed)

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

ioannis.cherouvim’s picture

Thanks. The title of this issue saved me after 3 hours of trying to figure out why my facets where empty.

drunken monkey’s picture

Project: Search API Database Search » Search API
Version: 7.x-1.x-dev » 8.x-1.x-dev
Component: Code » Database backend
Status: Closed (fixed) » Patch (to be ported)

Needs to be ported to D8.

drunken monkey’s picture

Issue tags: +Novice
nick_vh’s picture

Issue summary: View changes
Issue tags: +beta blocker
dpovshed’s picture

Assigned: Unassigned » dpovshed
dpovshed’s picture

Status: Patch (to be ported) » Needs review
StatusFileSize
new804 bytes

Please take a look at the patch.

Since in the proposed solution try/catch used inside Database::getTemporaryResultsTable() function, it should cover also usage of temp tables in getAutocompleteSuggestions(), not only in getfacets().

nick_vh’s picture

I haven't looked in to the issue yet but it looks like the original patch was much broader? For example, it also adds documentation to the readme file that we need temporary table permissions etc. Also the error message is different? If you are radically changing a patch, please add some information why and that it covers the whole use-case.

And maybe a silly question, but can we somehow test if a certain permission failed?

dpovshed’s picture

Assigned: dpovshed » Unassigned
StatusFileSize
new1.27 KB

Nick,

1) thanks for pointing my attention about README.txt update, I added that in next version of the patch;
2) changing error message text: maybe I was not clear, but now this can be generated not only while working with Facets;
3) lesser amount of code changed: I wrapped with try/catch only the place where we do "create temp table"; if there something wrong with query manipulations like $db_query->getExpressions() and $db_query->distinct() this IMHO not related to the ticket (but I am OK if it is better to wrap entire function context);
4) about testing - have no idea how to test this even manually :) I did not found anything related in UI. But I had generated exception and tested that current catch/watchdog_exception stores readable and informative message in log.

I'll un-assign myself from the issue in case someone with better knowledge of SearchAPI internals wants to chime in.

drunken monkey’s picture

Thanks a lot for your work, looks good!
Just a few minor things:

  1. +++ b/search_api_db/README.txt
    @@ -45,6 +45,10 @@ See [1] for details.
    +Also, using facets and autocomplete suggestions with a database server will
    +only work if the database user Drupal is using has the
    +"CREATE TEMPORARY TABLES" permission (or similar, in DBMSs other than MySQL).
    

    Should break at as close to 80 characters as possible.

  2. +++ b/search_api_db/src/Plugin/search_api/backend/Database.php
    @@ -2062,7 +2062,14 @@ class Database extends BackendPluginBase {
    +    catch(\Exception $e) {
    

    As in D7, we should just catch the PDOException here, I'd say.

  3. +++ b/search_api_db/src/Plugin/search_api/backend/Database.php
    @@ -2062,7 +2062,14 @@ class Database extends BackendPluginBase {
    +      watchdog_exception('search_api', $e, "Error creating temporary table: %message", array('%message' => $e->getMessage()));
    

    Please use the same pattern as used elsewhere for watchdog_exception() calls. (With that, the error message variable will also automatically be provided.)

Please see the attache patch, which I'd call RTBC.

And maybe a silly question, but can we somehow test if a certain permission failed?

I don't think so, especially not in a DBMS-independent way. And I don't think it's worth it to try, just for a better error message in an edge case.

borisson_’s picture

Status: Needs review » Reviewed & tested by the community

I agree with RTBC.

drunken monkey’s picture

Status: Reviewed & tested by the community » Fixed

Thanks for reviewing!
Committed.
Thanks again, everyone!

Status: Fixed » Closed (fixed)

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

dimr’s picture

I got this issue with Drupal 8.4.2, Facet dev version (released alpha-11) trying to use the "Range slider" with a Date Range field and PostgreSQL 9.6.6.

I have a view with a list of elements and a block with the facet widget "Range slider" but when I try to move the slider then I get the error:

Drupal\Core\Database\DatabaseExceptionWrapper while trying to create a temporary table: SQLSTATE[42601]: Syntax error: 7 ERROR: syntax error at or near ")" LINE 4: ...IN ('event')) AND (t.field_date_period BETWEEN '1479917913') ^: CREATE TEMPORARY TABLE {db_temporary_1} AS SELECT DISTINCT t.item_id AS item_id FROM {search_api_db_default_index} t WHERE (t.type IN (:db_condition_placeholder_0)) AND (t.field_date_period BETWEEN :db_condition_placeholder_1); Array ( [:db_condition_placeholder_0] => event [:db_condition_placeholder_1] => 1479917913 ) in Drupal\search_api\Entity\Server->search() (line 461 of web/modules/contrib/search_api/src/Entity/Server.php).