Overview

In the following article, I investigated how to search nested fields using Strapi.

This time, I will investigate how to do the same thing with Drupal. For this investigation, Book and Author content has already been registered in the following article.

The following article was helpful for filtering methods.

https://www.drupal.org/docs/core-modules-and-themes/core-modules/jsonapi-module/filtering

Search Examples

The following searches are performed against:

/jsonapi/node/book?

Search for books containing an author with hobby=dance

  • SHORT

filter[field_authors.field_hobby]=dance

or

filter[field_authors.field_hobby][value]=dance

  • NORMAL

filter[ex1][condition][path]=field_authors.field_hobby&filter[ex1][condition][value]=dance

Search for books containing an author whose hobby contains “dan”

  • SHORT

filter[field_authors.field_hobby][operator]=CONTAINS&filter[field_authors.field_hobby][value]=dan

  • NORMAL

filter[ex1][condition][path]=field_authors.field_hobby&filter[ex1][condition][operator]=CONTAINS&filter[ex1][condition][value]=dan

(Reference) Search for books containing an author whose hobby is play or sing

filter[ex1][condition][path]=field_authors.field_hobby&filter[ex1][condition][operator]=IN&filter[ex1][condition][value][1]=sing&filter[ex1][condition][value][2]=play

(Reference) Using the Search API

By using the following module, it appears possible to search across multiple content types, specify field names, add facets, and more.

https://www.drupal.org/project/jsonapi_search_api

The following article introduces how to use it, so please refer to it.

</en/posts/8d7aa7c33abffc/#search-api>

Creating an Index

For example, configure the Search API index as follows.

This allows you to also retrieve book information from the following URL.

/jsonapi/index/book

Compared to the standard jsonapi (/jsonapi/node/book, etc.), the meta field includes a count, which allows you to check the total number of search results. (There may be a way to add this to the standard jsonapi as well, but this is unknown due to insufficient investigation.)

{}"}""}"}j,dm,ls""}ae"i"}ovmttcnsnee"}aaoke"artl""uslhpsai"}::n"frii"nst:"e"o:ke"[{":f:nslh.:{""{"fr.{:{::"e.4:f]""{",h1{:t.t0"p"hs,t:t/p/:x/x/xj/sjosnoanpaip.io/rign/dfeoxr/mbaoto/k1".0/"

Filtering

Also, for “searching books containing an author with hobby=dance,” since we assigned the property path field_authors:entity:field_hobby to the machine name field_hobby in the earlier index creation, it could be executed with the following simple query.

  • SHORT

filter[field_hobby]=dance

  • NORMAL

filter[ex1][condition][path]=field_hobby&filter[ex1][condition][value]=dance

Facets

Furthermore, (based on uncertain knowledge at the time of writing this article,) one of the major advantages of using the Search API is the ability to use facets.

{}"]f{}a""""]cilpt{}{}{}edaae""},""},""}t"btruv""""uv""""uv""""s:ehmravlacravlacravlac"l"sllaacollaacollaaco:"":""ulbtu"ulbtu"ulbtuf:::euein:euein:euein[i"selvtselvtselvte"f[""""e"""""e"""""e"laih:::":h:::":h:::":duet:t:t:_tlt{""1t{""1t{""2hhdpppfpssfpddtoo_sllasiiasaarbrh:aal:nnl:nnubso/yys/ggs/cceyb/""e/""e/ee,"»bx,,,x,,,x"",yxxx,,C"xxxo,///njjjtssseooonnnntaaappp»iii///hiiionnnbdddbeeeyxxx"///,bbbooooookkk???fffiiilllttteeerrr%%%555BBBfffiiieeelllddd___hhhooobbbbbbyyy--%ff5aaDcc=eedtta%%n55cDDe%%"55,BBccoonnddiittiioonn%%55DD%%55BBppaatthh%%55DD==ffiieelldd__hhoobbbbyy&&ffiilltteerr%%55BBffiieelldd__hhoobbbbyy--ffaacceett%%55DD%%55BBccoonnddiittiioonn%%55DD%%55BBooppeerraattoorr%%55DD==IINN&&ffiilltteerr%%55BBffiieelldd__hhoobbbbyy--ffaacceett%%55DD%%55BBccoonnddiittiioonn%%55DD%%55BBvvaalluuee%%55DD%%55BB00%%55DD==ddaannccee&&ffiilltteerr%%55BBffiieelldd__hhoobbbbyy--ffaacceett%%55DD%%55BBccoonnddiittiioonn%%55DD%%55BBvvaalluuee%%55DD%%55BB11%%55DD==pslianyg"",,

Summary

There may be some inaccurate information, but we hope this serves as a useful reference for searching nested structures in Drupal and combining JSON:API with the Search API.