Not getting 'highlights' and 'score' values in Atlas function

Hi.

I’m running the same function directly in a node.js function, and in Atlas function, accessed through web endpoint.

The function is using Atlas Search.

When I run it directly, everything is fine, and I get back the highlights and scores.
However, when I run it with Atlas function, it doesn’t return back the highlights and scores (but the other fields are returned OK).

The code is:

        const searchQuery = [
            {
                $search: {
                    index: 'default',
                    text: {
                        query: 'את',
                        path: { wildcard: '*' }
                    },
                    highlight: {
                        path: { wildcard: '*' }
                    }
                }
            },
            {
                $project: {
                    score: { $meta: 'searchScore' },
                    highlights: { $meta: 'searchHighlights' },
                    title: 1,
                    content: 1,
                    key: 1,
                    _id: 0
                }
            }
        ];

        const searchResults = await collection.aggregate(searchQuery).toArray();

Please help.

Have you tried to run the function in the Atlas Function UI? Do you see the expected results there? Here’s an updated version of your script that I modified to run against one of my database collections that includes a default atlas search index… The collection contains books and I’m searching for a book with the word pooh in the title… as in “Winnie the Pooh”.

exports = async function(payload) {
    const searchQuery = [
        {
            $search: {
                index: 'default',
                text: {
                    query: 'pooh',
                    path: { wildcard: '*' }
                },
                highlight: {
                    path: { wildcard: '*' }
                }
            }
        },
        {
            $project: {
                score: { $meta: 'searchScore' },
                highlights: { $meta: 'searchHighlights' },
                title: 1,
                content: 1,
                key: 1,
                _id: 0
            }
        }
    ];

    const collection = context.services.get("mongodb-atlas").db("library").collection("books");
    const searchResults = await collection.aggregate(searchQuery).toArray();
    
    console.log(JSON.stringify(searchResults));
    return searchResults;
};

Note that I’m using console.log to view the results from within the Atlas Function UI… here’s the results:


> ran at 1717144676422
> took 587.299693ms
> logs: 
[{"title":"Winnie the Pooh","score":16.059213638305664,"highlights":[{"score":1.3859853744506836,"path":"title","texts":[{"value":"Winnie the ","type":"text"},{"value":"Pooh","type":"hit"}]},{"score":1.205136775970459,"path":"synopsis","texts":[{"value":"Little Ones Who Love Winnie-the-","type":"text"},{"value":"pooh","type":"hit"},{"value":" And Terrific Textures To Touch And Feel Will Delight In This Year's Winnie-the-pooh's Touch And Feel. ","type":"text"}]},{"score":1.3047959804534912,"path":"synopsis","texts":[{"value":"The Youngest ","type":"text"},{"value":"Pooh","type":"hit"},{"value":" Admirers Won't Be Able To Keep Their Hands Off This Charming Book, Filled With Ernest H. ","type":"text"}]},{"score":1.5236109495162964,"path":"synopsis","texts":[{"v
> result: 
[
  {
    "title": "Winnie the Pooh",
    "score": {
      "$numberDouble": "16.059213638305664"
    },
    "highlights": [
      {
        "score": {
          "$numberDouble": "1.3859853744506836"
        },
        "path": "title",
        "texts": [
          {
            "value": "Winnie the ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "1.205136775970459"
        },
        "path": "synopsis",
        "texts": [
          {
            "value": "Little Ones Who Love Winnie-the-",
            "type": "text"
          },
          {
            "value": "pooh",
            "type": "hit"
          },
          {
            "value": " And Terrific Textures To Touch And Feel Will Delight In This Year's Winnie-the-pooh's Touch And Feel. ",
            "type": "text"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "1.3047959804534912"
        },
        "path": "synopsis",
        "texts": [
          {
            "value": "The Youngest ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": " Admirers Won't Be Able To Keep Their Hands Off This Charming Book, Filled With Ernest H. ",
            "type": "text"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "1.5236109495162964"
        },
        "path": "synopsis",
        "texts": [
          {
            "value": "Milne's Timeless Volumes-and The Mirth And Merriment Of Winnie-the-",
            "type": "text"
          },
          {
            "value": "pooh",
            "type": "hit"
          },
          {
            "value": ".",
            "type": "text"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "1.3859853744506836"
        },
        "path": "longTitle",
        "texts": [
          {
            "value": "Winnie the ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "2.368772268295288"
        },
        "path": "genres",
        "texts": [
          {
            "value": "Winnie-the-",
            "type": "text"
          },
          {
            "value": "pooh",
            "type": "hit"
          },
          {
            "value": " (fictitious character), fiction",
            "type": "text"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "2.4874861240386963"
        },
        "path": "genres",
        "texts": [
          {
            "value": "Winnie-the-",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": " (Fictitious character)",
            "type": "text"
          }
        ]
      }
    ]
  },
  {
    "title": "The Te of Piglet (Winnie-the-Pooh)",
    "score": {
      "$numberDouble": "11.674216270446777"
    },
    "highlights": [
      {
        "score": {
          "$numberDouble": "1.329585075378418"
        },
        "path": "title",
        "texts": [
          {
            "value": "The Te of Piglet (Winnie-the-",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": ")",
            "type": "text"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "1.117163896560669"
        },
        "path": "synopsis",
        "texts": [
          {
            "value": "The Author And The Characters From The ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": " Books Engage In Dialogue Elucidating The Taoist Principle Of Te, The Way Of The Small.",
            "type": "text"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "1.329585075378418"
        },
        "path": "longTitle",
        "texts": [
          {
            "value": "The Te of Piglet (Winnie-the-",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": ")",
            "type": "text"
          }
        ]
      }
    ]
  },
  {
    "title": "Winnie the Pooh and Tigger Too (Disney's Wonderful World of Reading)",
    "score": {
      "$numberDouble": "5.388727188110352"
    },
    "highlights": [
      {
        "score": {
          "$numberDouble": "1.242769718170166"
        },
        "path": "title",
        "texts": [
          {
            "value": "Winnie the ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": " and Tigger Too (Disney's Wonderful World of Reading)",
            "type": "text"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "1.242769718170166"
        },
        "path": "longTitle",
        "texts": [
          {
            "value": "Winnie the ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": " and Tigger Too (Disney's Wonderful World of Reading)",
            "type": "text"
          }
        ]
      }
    ]
  },
  {
    "title": "The Tigger Movie (A golden storybook)",
    "score": {
      "$numberDouble": "5.0963029861450195"
    },
    "highlights": [
      {
        "score": {
          "$numberDouble": "1.1716015338897705"
        },
        "path": "synopsis",
        "texts": [
          {
            "value": "Winnie The ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": " And The Rest Of The Gang From The Hundred-acre Wood Help Tigger Find His Real Family.",
            "type": "text"
          }
        ]
      }
    ]
  },
  {
    "title": "Everyone is Special (Lessons From the Hundred Acre Wood, No. 2 / Disney's Winnie The Pooh)",
    "score": {
      "$numberDouble": "4.5299882888793945"
    },
    "highlights": [
      {
        "score": {
          "$numberDouble": "1.194116234779358"
        },
        "path": "title",
        "texts": [
          {
            "value": "Everyone is Special (Lessons From the Hundred Acre Wood, No. 2 / Disney's Winnie The ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": ")",
            "type": "text"
          }
        ]
      },
      {
        "score": {
          "$numberDouble": "1.194116234779358"
        },
        "path": "longTitle",
        "texts": [
          {
            "value": "Everyone is Special (Lessons From the Hundred Acre Wood, No. 2 / Disney's Winnie The ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": ")",
            "type": "text"
          }
        ]
      }
    ]
  },
  {
    "title": "Winnie-the-Pooh's Teatime Cookbook",
    "score": {
      "$numberDouble": "3.373472213745117"
    },
    "highlights": [
      {
        "score": {
          "$numberDouble": "1.4992142915725708"
        },
        "path": "synopsis",
        "texts": [
          {
            "value": "And the goodies produced will please those who like a Little Smackerel in the afternoon - and the company of ",
            "type": "text"
          },
          {
            "value": "Pooh",
            "type": "hit"
          },
          {
            "value": ", Piglet, Christopher Robin, and the other beloved residents of the Hundred Acre Wood.\n\n\n                        ",
            "type": "text"
          }
        ]
      }
    ]
  }
]
> result (JavaScript): 
EJSON.parse('[{"title":"Winnie the Pooh","score":{"$numberDouble":"16.059213638305664"},"highlights":[{"score":{"$numberDouble":"1.3859853744506836"},"path":"title","texts":[{"value":"Winnie the ","type":"text"},{"value":"Pooh","type":"hit"}]},{"score":{"$numberDouble":"1.205136775970459"},"path":"synopsis","texts":[{"value":"Little Ones Who Love Winnie-the-","type":"text"},{"value":"pooh","type":"hit"},{"value":" And Terrific Textures To Touch And Feel Will Delight In This Year\'s Winnie-the-pooh's Touch And Feel. ","type":"text"}]},{"score":{"$numberDouble":"1.3047959804534912"},"path":"synopsis","texts":[{"value":"The Youngest ","type":"text"},{"value":"Pooh","type":"hit"},{"value":" Admirers Won't Be Able To Keep Their Hands Off This Charming Book, Filled With Ernest H. ","type":"text"}]},{"score":{"$numberDouble":"1.5236109495162964"},"path":"synopsis","texts":[{"value":"Milne's Timeless Volumes-and The Mirth And Merriment Of Winnie-the-","type":"text"},{"value":"pooh","type":"hit"},{"value":".","type":"text"}]},{"score":{"$numberDouble":"1.3859853744506836"},"path":"longTitle","texts":[{"value":"Winnie the ","type":"text"},{"value":"Pooh","type":"hit"}]},{"score":{"$numberDouble":"2.368772268295288"},"path":"genres","texts":[{"value":"Winnie-the-","type":"text"},{"value":"pooh","type":"hit"},{"value":" (fictitious character), fiction","type":"text"}]},{"score":{"$numberDouble":"2.4874861240386963"},"path":"genres","texts":[{"value":"Winnie-the-","type":"text"},{"value":"Pooh","type":"hit"},{"value":" (Fictitious character)","type":"text"}]}]},{"title":"The Te of Piglet (Winnie-the-Pooh)","score":{"$numberDouble":"11.674216270446777"},"highlights":[{"score":{"$numberDouble":"1.329585075378418"},"path":"title","texts":[{"value":"The Te of Piglet (Winnie-the-","type":"text"},{"value":"Pooh","type":"hit"},{"value":")","type":"text"}]},{"score":{"$numberDouble":"1.117163896560669"},"path":"synopsis","texts":[{"value":"The Author And The Characters From The ","type":"text"},{"value":"Pooh","type":"hit"},{"value":" Books Engage In Dialogue Elucidating The Taoist Principle Of Te, The Way Of The Small.","type":"text"}]},{"score":{"$numberDouble":"1.329585075378418"},"path":"longTitle","texts":[{"value":"The Te of Piglet (Winnie-the-","type":"text"},{"value":"Pooh","type":"hit"},{"value":")","type":"text"}]}]},{"title":"Winnie the Pooh and Tigger Too (Disney's Wonderful World of Reading)","score":{"$numberDouble":"5.388727188110352"},"highlights":[{"score":{"$numberDouble":"1.242769718170166"},"path":"title","texts":[{"value":"Winnie the ","type":"text"},{"value":"Pooh","type":"hit"},{"value":" and Tigger Too (Disney's Wonderful World of Reading)","type":"text"}]},{"score":{"$numberDouble":"1.242769718170166"},"path":"longTitle","texts":[{"value":"Winnie the ","type":"text"},{"value":"Pooh","type":"hit"},{"value":" and Tigger Too (Disney's Wonderful World of Reading)","type":"text"}]}]},{"title":"The Tigger Movie (A golden storybook)","score":{"$numberDouble":"5.0963029861450195"},"highlights":[{"score":{"$numberDouble":"1.1716015338897705"},"path":"synopsis","texts":[{"value":"Winnie The ","type":"text"},{"value":"Pooh","type":"hit"},{"value":" And The Rest Of The Gang From The Hundred-acre Wood Help Tigger Find His Real Family.","type":"text"}]}]},{"title":"Everyone is Special (Lessons From the Hundred Acre Wood, No. 2 / Disney's Winnie The Pooh)","score":{"$numberDouble":"4.5299882888793945"},"highlights":[{"score":{"$numberDouble":"1.194116234779358"},"path":"title","texts":[{"value":"Everyone is Special (Lessons From the Hundred Acre Wood, No. 2 / Disney's Winnie The ","type":"text"},{"value":"Pooh","type":"hit"},{"value":")","type":"text"}]},{"score":{"$numberDouble":"1.194116234779358"},"path":"longTitle","texts":[{"value":"Everyone is Special (Lessons From the Hundred Acre Wood, No. 2 / Disney's Winnie The ","type":"text"},{"value":"Pooh","type":"hit"},{"value":")","type":"text"}]}]},{"title":"Winnie-the-Pooh's Teatime Cookbook","score":{"$numberDouble":"3.373472213745117"},"highlights":[{"score":{"$numberDouble":"1.4992142915725708"},"path":"synopsis","texts":[{"value":"And the goodies produced will please those who like a Little Smackerel in the afternoon - and the company of ","type":"text"},{"value":"Pooh","type":"hit"},{"value":", Piglet, Christopher Robin, and the other beloved residents of the Hundred Acre Wood.\n\n\n                        ","type":"text"}]}]}]')

The highlights and scores are included in the output… let’s make sure you’re getting these details from within the UI… then we can debug from there.

Hope this helps.

2 Likes

Hi.
Sorry for the late response.

I think it was permissions issue. Now it’s working.

Thanks for the help.