Full-text search highlights nested object

I am writing to report an issue I encountered while using the text search feature in MongoDB. Specifically, I am facing challenges with extracting and aggregating highlighted text from nested fields in the search results.

Here is an overview of the problem:

Context: I am using the text search feature in MongoDB to search for specific keywords across multiple fields within my documents.

Problem Description: When performing a text search, I am able to retrieve search results that contain highlighted text, which indicates where the keyword matches were found. However, I am struggling to properly aggregate and organize the highlighted text, especially when it is located within nested fields.

Example: For instance, consider the following document structure:

{ "content": {
    "_id": "<document_id>",
    "distributionContent": {
      "experienceContent": [
        { "experienceTranslation": {"title": "Sample Title", "_id": "1111_1"},
          "_id": "1111",
          "experienceId": "<experience_id>",
          "languageCode": "en"
        },
        { "experienceTranslation": {"title": "Sample Title 2", "_id": "222_2"},
          "_id": "222",
          "experienceId": "<experience_id>",
          "languageCode": "en"
        }
      ]
    }
  },
  // Other fields...
}

In this example, the text search may match the keyword “test” within the experienceTranslation.title field. However, I need to determine the corresponding languageCode for each matched occurrence, which is located at a higher level in the document structure (distributionContent.experienceContent.languageCode).

Challenge: The challenge arises because experienceContent is a list, and I need to somehow correlate each matched text with its corresponding languageCode.

Expected Outcome: Ideally, I would like to retrieve a structured representation of the search results, where each matched text is associated with its parent languageCode.

Request for Assistance: I would greatly appreciate any guidance or assistance you can provide to help me address this issue effectively. Additionally, if there are any best practices or alternative approaches for handling nested fields in text search results, I would be eager to learn more about them.

Hi @Serena_C , can you provide a sample document of what the expected outcome looks like? Are you looking to only return the subdocuments of “experienceContent” where there is a text match on experienceTranslation.title? Or are you looking to promote the subdocuments of experienceContent to the top-level? The $unwind or $replaceRoot aggregation stages may be useful for you.

1 Like