Overview

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

/en/posts/425f987d9cbf9c/

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.

/en/posts/811761bd95fd58/

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.)

{
  "jsonapi": {
    "version": "1.0",
    "meta": {
      "links": {
        "self": {
          "href": "http://jsonapi.org/format/1.0/"
        }
      }
    }
  },
  "data": [...],
  "meta": {
    "count": 4
  },
  "links": {
    "self": {
      "href": "https://xxx/jsonapi/index/book"
    }
  }
}

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.

{
	"facets": [
		{
			"id": "field_hobby",
			"label": "authors » Content » hobby",
			"path": "field_hobby",
			"terms": [
				{
					"url": "https://xxx/jsonapi/index/book?filter%5Bfield_hobby-facet%5D%5Bcondition%5D%5Bpath%5D=field_hobby&filter%5Bfield_hobby-facet%5D%5Bcondition%5D%5Boperator%5D=IN&filter%5Bfield_hobby-facet%5D%5Bcondition%5D%5Bvalue%5D%5B0%5D=dance&filter%5Bfield_hobby-facet%5D%5Bcondition%5D%5Bvalue%5D%5B1%5D=play",
					"values": {
						"value": "play",
						"label": "play",
						"active": false,
						"count": 1
					}
				},
				{
					"url": "https://xxx/jsonapi/index/book?filter%5Bfield_hobby-facet%5D%5Bcondition%5D%5Bpath%5D=field_hobby&filter%5Bfield_hobby-facet%5D%5Bcondition%5D%5Boperator%5D=IN&filter%5Bfield_hobby-facet%5D%5Bcondition%5D%5Bvalue%5D%5B0%5D=dance&filter%5Bfield_hobby-facet%5D%5Bcondition%5D%5Bvalue%5D%5B1%5D=sing",
					"values": {
						"value": "sing",
						"label": "sing",
						"active": false,
						"count": 1
					}
				},
				{
					"url": "https://xxx/jsonapi/index/book?filter%5Bfield_hobby%5D=dance",
					"values": {
						"value": "dance",
						"label": "dance",
						"active": true,
						"count": 2
					}
				}
			]
		}
	]
}

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.