Auto suggestion / Auto complete

How to allow searching for ‘Freezer’ even if the input is ‘My Freezer’, and auto suggest the corresponding values based on the search term and match type provided in the data through a function.

Search term and match type
const { term, match } = data

Example screenshot below:

Hey @Jagankumar_T,

If you are talking about an ‘app’ platform bot:

The below ‘autoComplete’ function should solve your issue.

return new Promise(function (resolve) {
    app.log(`*${app.term}*`, 'inside autocomplete()!')
    if (app.term) {
        app.dataStore.search({
            table: "auto_complete",
            body: {
                "query": {
                    "query_string": {
                        "query": `*${app.term}*`,
                        "fields": [`utterances`]
                    }
                }
            }
        }).then((result) => {
            resolve(app._.map(result.hits.hits, function (hit) {
                app.log(hit._source[`utterances`], 'hit._source[`utterances`]')
                return [hit._source[`utterances`], hit._source[`utterances`]];
            }))
        }, () => {
            resolve([])
        })
    } else { resolve([]); }
});

If you are talking about a ‘cloud’ platform bot, you can play around with the following code:

return new Promise(resolve => {
    let dbSearchRecordsArray = data.variables.dbSearch.records;
    const { term } = data;
    //let term = "Ovens";
    let userMessageArray = term.split(" ");
    let suggestions = [];
    let dynamicRegEx, currentFAQ, currentFAQArray;
    for (let eachUserMessageWord of userMessageArray) {
        eachUserMessageWord = eachUserMessageWord.trim();
        //console.log(eachUserMessageWord);
        if (eachUserMessageWord) {
            eachUserMessageWord = eachUserMessageWord.trim();
            eachUserMessageWord = eachUserMessageWord.toLowerCase();
            for (let eachDBSearchResponse of dbSearchRecordsArray) {
                currentFAQ = eachDBSearchResponse.faq;
                currentFAQ = currentFAQ.trim();
                currentFAQ = currentFAQ.toLowerCase();
                currentFAQArray = currentFAQ.split(" ");
                for (let eachFAQWord of currentFAQArray) {
                    eachFAQWord = eachFAQWord.trim();
                    eachFAQWord = eachFAQWord.toLowerCase();
                    dynamicRegEx = new RegExp(eachFAQWord, "g");
                    if (eachUserMessageWord.match(dynamicRegEx)) {
                        //console.log("\n\n eachUserMessageWord in iF condition:"+eachUserMessageWord);
                        //console.log("eachFAQWord in iF condition:"+eachFAQWord);
                        suggestions.push([eachDBSearchResponse.faq, eachDBSearchResponse.faq]);
                    } 
                }
            }
        }
    }
    //console.log(JSON.stringify(suggestions));
    resolve(suggestions);
});

Here are some screenshots for your reference:

Notes:

The above code is just a workaround, as our cloud platform currently doesn’t allow us to write any elastic search queries.

→ If you have more than 200 records on the auto complete DB Table, You can do UI Looping, so that You can store all the DB Search Responses on a Variable.

→ Because of 100 UI Nodes Loop Limitation and also because of Multiple Looping on the above code, If you have more than 1000 (or) 3000 records on the DB Table, This workaround might not work properly.

At Such Cases, You can raise an Internal Ticket to the (Builder) Platform team. They might give you some other alternate solution (or) They might develop a new feature request for this.

Feel Free to Let me know if you have any questions on the same.

1 Like