0

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:

search suggestion

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

OjunbamO
  • 29
  • 1
  • 3
  • 1
    You could create a regular expression, that contains groups for the 4-5 words before/after. Then you could search with that regex and finally manipulate the string to show the result you want. But pay attention, that could quickly become slow with complex regular expressions. – casenonsensitive Jul 13 '20 at 13:49

1 Answers1

0

/(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}test(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}/i

The first {0,4} means up to 4 words before the search term ('test'). The second means up to 4 words after the search term.

It could have been written {4} (exactly 4) but then that wouldn't work when the search term is the first word. Since there'd be 0 words before it.

Most of the complication owes to the fact that your test case needs to match the test inside {{testHelper}}, along with the preceding and following words.

const searchTerm = 'test';
const strings = [
  'Hello World! And so, whenever you test-drive a new theme, make sure that you are buckled in.',
  'It is going to be a bumpy ride. In the below example there is a {{testHelper}} defined as an illustration.  This is for illustrative purposes.',
  'Some users have encountered the issue of duplicating the URL as TestName.github.io/Test for illegitamate uses.',
  'You do not have to, but if you want, you can click the Test connection button to make sure everything works as intended.',
];
const regExpString = String.raw`(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}${searchTerm}(([a-z]+[^a-z]+)|([^a-z]+[a-z]+)){0,4}`;
const regExp = new RegExp(regExpString, 'i');

strings.forEach(string => {
  const match = string.match(regExp)?.[0];
  console.log(match);
}); 
GirkovArpa
  • 4,427
  • 4
  • 14
  • 43