question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make it possible to chose which sub-fields of the related collection we should keep

See original GitHub issue

Problem

If you have a collection in Strapi that has a relation with another collection, once you index it to MeiliSearch the output is the following:

 {
    "id": 2,
    "name": "Italian",
    "restaurants": [
      {
        "id": 1,
        "name": "PetitPatapon",
        "description": "Delicieux",
        "created_by": 1,
        "updated_by": 1,
        "created_at": "2021-03-03T12:09:10.979Z",
        "updated_at": "2021-03-03T12:09:10.988Z"
      },
      {
        "id": 2,
        "name": "LaLoLaLiTa",
        "description": "Ma que bello",
        "created_by": 1,
        "updated_by": 1,
        "created_at": "2021-03-03T12:17:46.503Z",
        "updated_at": "2021-03-03T12:17:46.515Z"
      }
    ]
  },

But for MeiliSearch to create facets the best way would be to transform the restaurants object into the following:

 {
    "id": 2,
    "name": "Italian",
    "restaurants": ["LaLoLaLiTa", "PetitPatapon"]
 },

Solution

It would be better that on indexation, if a relation is discovered, the user is prompted to chose one field or to ignore the questions.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:6 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
bidoubiwacommented, Nov 10, 2021

Thanks to @harish2704 it will become possible to choose your fields by adding a transformer to your collection so that you can change the data before sending it to MeiliSearch.

See #287

example in the restaurant collection.

  // api/restaurant/models/restaurant.js

module.exports = {
  meilisearch: {
    transformEntry(entry) {
      const transformedEntry = entry
      // remove created by and updated by fields
      delete transformedEntry.created_by
      delete transformedEntry.updated_by
      return transformedEntry
    },
  },
}

It will be available next release!

2reactions
taker93commented, Jul 1, 2021

Ok, so for now I found a workaround to make facets on relations work:

When you’ve created your post types go to your strapi /api/<CONTENT_TYPE> folder. In there your should find 4 folders:

  • config
  • controllers
  • models
  • services

Go to the services folder and open your posts.js.

In there you can customize how posts are read from the database. As the meilisearch plugin is using the find()-Method of your types service, you need to overwrite its functionality like the following:

"use strict";

/**
 * Read the documentation (https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html#core-services)
 * to customize this service
 */

// use lodash as it is inherited by strapi itself
const _ = require("lodash");

module.exports = {
  /**
   * Retrieve records.
   *
   * @return {Array}
   */
  async find(params, populate) {
    return strapi
      .query("restaurant")
      .find(params, populate)
      .then((entities) => {
        return entities.map((entity) => {
          // check if relation is present
          if (entity.categories) {
            // flatten relation array with lodash
            // "key" is the key you want to represent the category for meilisearch-facets
            // if you do not want to override the standard field "categories" just change entity.categories to something else
            entity.categories = _.map(_.flatten(entity.categories), "key");
          }
          return entity;
        });
      });
  },
};

More on customizing your backend: https://strapi.io/documentation/developer-docs/latest/development/backend-customization.html

I think this functionality (lodash and flattening) could be the way for the meilisearch-plugin to provide such functionality as long multi-dimensional searches are not possible by meilisearch itself.

Plugin needs to be changed that it recognizes that the content-type you want to add to meilisearch has relations on other types. Therefor it should be possible to have an UI-PopUp which shows all fields of the type. In there you could choose which fields you want to add to the index. Besides choosing the fields you could override the keys and transformation. Transformation: Use-Cases are Enums and relations. Relations could be flattened to one field, Enums mapped to another value.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Control Subfields - OCLC
The subfields included here are used to provide various identifiers, relationships, and sources of terms and codes. They are also used to ...
Read more >
Understanding MARC Bibliographic: Parts 7 to 10
In general, every field MUST have a subfield 'a' ($a). One exception that is often seen is in Field 020 (ISBN), when the...
Read more >
Ability to search via free text MARC tag and subfields
We would like to have an advanced search option that allows us to enter the MARC tag number (and subfields) for searching, ...
Read more >
Psychology subfields
They apply psychological science to understand how we make decisions and perceive our world. Climate and environmental psychology. Climate and environmental ...
Read more >
Creating conditional ticket fields - Zendesk help
By limiting the number of fields that initially display, you make it so that agents and end users complete only the fields that...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found