Skip to main content
Meilisearch is optimized for handling paragraph-sized chunks of text. Datasets with many documents containing large amounts of text may lead to reduced search result relevancy. In this guide, you will see how to use JavaScript with Node.js to split a single large document and configure Meilisearch with a distinct attribute to prevent duplicated results.

Requirements

  • A running Meilisearch project
  • A command-line console
  • Node.js v18

Dataset

stories.json contains two documents, each storing the full text of a short story in its text field:
Meilisearch works best with documents under 1kb in size. This roughly translates to a maximum of two or three paragraphs of text.

Splitting documents

Create a split_documents.js file in your working directory:
Next, run the script on your console, specifying the path to your JSON dataset:
This script accepts one argument: a path pointing to a JSON dataset. It reads the file and parses each document in it. For each paragraph in a document’s text field, it creates a new document with a new id and text fields. Finally, it writes the new documents on stories-split.json.

Generating unique IDs

Right now, Meilisearch would not accept the new dataset because many documents share the same primary key. Update the script from the previous step to create a new field, story_id:
The script now stores the original document’s id in story_id. It then creates a new unique identifier for each new document and stores it in the primary key field.

Configuring distinct attribute

This dataset is now valid, but since each document effectively points to the same story, queries are likely to result in duplicated search results. To prevent that from happening, configure story_id as the index’s distinct attribute:
Users searching this dataset will now be able to find more relevant results across large chunks of text, without any loss of performance and no duplicates.

Conclusion

You have seen how to split large documents to improve search relevancy. You also saw how to configure a distinct attribute to prevent Meilisearch from returning duplicate results. Though this guide used JavaScript, you can replicate the process with any programming language you are comfortable using.