Search API works with core Rest module's implementation on views "data" to return row field results, but not for entity rows.

Original issue

Something we came up with during our D8 BoF in Amsterdam: for better supporting HEADless Drupal et al., it might be a good idea to just provide an endpoint where you can execute any search (via GET parameters, no UI) and get the complete results (including facets, spellchecking, highlighting, etc.) back as JSON, XML or whatever.

Shouldn't really be all that hard to implement, one large issue might be security, though.

Or maybe implement it as a Views display, so people could configure it beforehand to exclude sensitive results (or restrict access to certain roles).

Issue fork search_api-2348117

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drunken monkey’s picture

veronicaSeveryn’s picture

Not sure if I am posting inside the right issue, but is there a way to display Search API View result as JSON output at the moment?

I have tried creating a "REST Export" View output on a Search API Indexed based view, but it just returns empty results
screenshot

drunken monkey’s picture

That's a different issue, please create a new one. It does sound like something that should already work, though, so if it doesn't it might be a bug.

skorzh’s picture

That module can be useful in someone is looking similar for drupal 7: https://www.drupal.org/project/restful_search_api

malik.kotob’s picture

I wasn't able to reproduce #2. As such, I'm not sure that we need this issue any longer. With the advent of REST export displays in core views, you can create a REST export for each index as desired. From what I've seen, it also supports filtering, permissions, etc. just fine.

@drunken monkey, is there any reason why the REST export route wouldn't work for all backends? I've only tested with a DB backend.

drunken monkey’s picture

@drunken monkey, is there any reason why the REST export route wouldn't work for all backends? I've only tested with a DB backend.

This should be 100% independent of the backend plugin, so if Views REST export displays work with Search API, that's indeed a fix for this issue.
Can someone else confirm this works for them?

brunodbo’s picture

I created a REST export display on a Search API based view (as described in #2), and it worked for me with the 'Fields' display plugin: I was able to get JSON output of the view. When using the 'Entity' display plugin however, I got the error mentioned in #2858814: REST views: fatal error when using row plugin and Views is aware of non-Entity-based tables.

ekes made their first commit to this issue’s fork.

ekes’s picture

So Rest Rows work; but entity doesn't. The core issue does highlight this, but seems broadened out. I can't fathom what core should be doing differently; but I can see how to make entity rows work with core rest module views.

Rest Entity Row plugin is: (a) getting a language manager for the entity type early on in the query and (b) retrieving the entity from $row->_entity. Neither of which work with Search API views results.

Just as Search API provides a SearchAPIRow plugin, you can provide a Row plugin for "data" results. Which on render returns the $row->_object->getEntity() if appropriate. See fork. It seems not to need the preRender() that SearchAPIRow has, $this->_object is always already loaded? With all the work the Rest Entity Row was doing for translation language, I wonder if this wouldn't need something to switch the language of the entity (but SearchAPIRow doesn't).

https://git.drupalcode.org/issue/search_api-2348117/-/commit/a2329e7fa5a... with all the unused use statements left in - oops. But you get the idea.

ekes’s picture

Title: Provide a REST endpoint for each index to execute arbitrary searches » Support core REST data views
Issue summary: View changes
drunken monkey’s picture

Component: Framework » Views integration
Status: Active » Needs review
FileSize
4.68 KB
4.15 KB

Great job, thanks a lot! Seems to work a treat.
However, it also seems like the Views REST display can handle typed data instead of entities without problem, so actually no need to return NULL for non-entities (as far as I can see). Adapted your code accordingly, along with some other minor clean-up.

Could you also tell me why you explicitly check for the existence of the datasource? Seems like that’s not needed in that code at all, and would anyways lead to lots of other errors in other places in the code. (For instance, item loading won’t work without the datasource, so in 99% of cases your first if would already catch that case.)

Uploading as a patch, since the test bot can’t handle our issue forks for some reason. (Cf. #3190024: Problem with test dependencies when testing issue forks.)

ekes’s picture

> Could you also tell me why you explicitly check for the existence of the datasource?

A cut and paste too far, in SearchApiRow the datasource is then actually used, here it's simpler.

  • drunken monkey committed 8ed2b1c on 8.x-1.x authored by ekes
    Issue #2348117 by ekes, drunken monkey: Added support for core REST data...
drunken monkey’s picture

Status: Needs review » Fixed

Awesome, thanks a lot again! Then this looks RTBC to me.
Committed. Thanks again!

Status: Fixed » Closed (fixed)

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