Skip to content
Advertisement

How to use select on azure search suggesters

I’m using Azure search on my project, and I want to do an autocomplete text field, it works as expected. here’s the code :

const suggestItems = async (req, res) => {

try {

    // Reading inputs from HTTP Request

    const q = (req.query.q || (req.body && req.body.q));

    const top = (req.query.top || (req.body && req.body.top));

    const suggester = (req.query.suggester || (req.body && req.body.suggester));

    // Let's get the top 5 suggestions for that search term

    const suggestions = await client.suggest(q, suggester, {  top: parseInt(top) });

    //const suggestions = await client.autocomplete(q, suggester, {top: parseInt(top)});

    console.log(suggestions.results)

    return res.status(status.OK)

    .json({ suggestions: suggestions.results})

   

} catch (error) {

    handleError(res, error)

}

 }

her’s the result :

[

{ text: 'Alpha Aromatics (MA)', document: { id: '4' } },

{ text: 'Alpha Aromatics (USA)', document: { id: '5' } },

{ text: 'Art Land - Winter Palace', document: { id: '6' } },

 { text: 'Alpha Aromatics (USA)', document: { id: '3' } }

]

here’s the quesry passed by postman :

{

"q":"ar","top":5,"suggester":"sg"

 }

but the problem is , on the result I have just the text and the id of the document , I’m looking for other fields like status for example, how can get that please ?

Advertisement

Answer

I am guessing “Status” is one of your index fields, from the question. You need to make sure you mark the fields you need to be returned in the results as retrievable in your index definition. It looks you only have text and id fields as retrievable. For more information: https://learn.microsoft.com/en-us/azure/search/search-what-is-an-index.

Example:

enter image description here

Advertisement