sorry if this question is already answered but I didn't anything which matches my case.
Currently I am trying to build a search suggestion function with Azure Cognitive Search. This is working pretty fine. But now I try to build something like this:
The suggestion should show the matching pattern, which is entered in the searchbar with a couple of words before and after. Also shown in the picture.
I tried to build a array which splits the complete content in single words and searches for the pattern. But this seems very ugly to me because I don't know how to reverse the array and get the words before and after. And compile this to a fitting string.
$.ajax({
url: "https://" + azSearchInstance + ".search.windows.net/indexes/" + azSearchIndex + "/docs?api-version=" + azApiVersion + "&search=" + text + '&$top=' + azSearchResults + '&api-key=' + azApiKey,
method: 'GET'
}).done(function (data) {
// display results
currentSuggestion2 = data[0];
add(data);
for(let i in data.value) {
var content = data.value[i].content;
var contentArray = content.split(' ');
for(let word in contentArray) {
if(contentArray[word] === text) {
console.log(contentArray[word]);
}
}
}
var render = Mustache.render(template, data);
$(".search-suggest").html(render)
});
My second try was to use the indexOf() function but it results in the same problem because it only returns a number on which position the matching pattern is located.
$.ajax({
url: "https://" + azSearchInstance + ".search.windows.net/indexes/" + azSearchIndex + "/docs?api-version=" + azApiVersion + "&search=" + text + '&$top=' + azSearchResults + '&api-key=' + azApiKey,
method: 'GET'
}).done(function (data) {
// display results
currentSuggestion2 = data[0];
add(data);
for(let i in data.value) {
var content = data.value[i].content;
console.log(content.indexOf(text));
}
var render = Mustache.render(template, data);
$(".search-suggest").html(render)
});
I am searching for a regular expression which searches for the pattern and prints like 4-5 words before and after the pattern. Does anyone of you has an idea?
Thank you in advance.
Regards
OjunbamO