Skip to content

Call hooks on content-types to change documents #211

@sargreal

Description

@sargreal

Description
When creating/updating an index, the strapi user should be able to change or convert the document structure of the document before it is submitted to the meilisearch index. This can be used to allow searching through connected content-types as well, since meilisearch currently can not search in nested data structures (except for arrays of strings with faceted search)

Basic example
Suppose two content types article and tag. Tags couldalso be stored in direct lists or strings in the content type, but then they would not be reusable. As such a document for an article may look something like this:

{
  "id": "1",
  "title": "Example Article",
  "content": "Some interesting content",
  "tags": [
    {
      "id": "1",
      "slug": "interesting"
    },
    {
      "id": "2",
      "slug": "new"
    }
  ]
}

for being able to search the article also with the tags, they should be stored as either string like "tags": "interesting new" or in a list to support faceted search "tags": ["interesting", "new"]

I think the most fitting place to put this would be inside api/article/services/article.js:

module.exports = {
  meiliSearchBeforeIndex(document) {
    document.tags = document.tags.map((tag) => tag.slug)
    return document;
  },
};

async should probably also be possible if one wants to maybe get some other data.

Implementation

Could probably be done in fetchRowBatch

async function fetchRowBatch({ start, limit, collection }) {
  const documents = await strapi.services[collection].find({
    _publicationState: 'preview',
    _limit: limit,
    _start: start,
  })
  if (typeof strapi.services[collection].meiliSearchBeforeIndex === "function") {
    return await Promise.all(documents.map(async (d) => await strapi.services[collection].meiliSearchBeforeIndex(d)))
  }
  return documents
}

Other
To fully use the potential of this feature, it should be possible to configure the indexes (such as which attributes are searchable or which are can be used in faceted search) as well through a similar mechanism, the strapi plugin config or the frontend.

Metadata

Metadata

Assignees

No one assigned

    Labels

    after-strapi-v4Issues to do after strapi v4 integration

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions