question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suppress searchEngine with async data function is exhaustive

See original GitHub issue

Is your feature request related to a problem? Please describe. When I have a src: async ... that makes a request to my backend, my backend will provide search results that are exhaustive. By exhaustive, I mean that they by definition should show in the results list, even though they may not contain a substring of the query string. Sometimes, the search results look nothing like the query string.

Because those search results look nothing like the query string, the searchEngine will reject it as a candidate, which means it doesn’t show up in the results.

Thoroughly Describe the solution you’d like

I think the most conclusive way to answer this is that the backend should be able to also give the highlighted parts to return, and then the searchEngine is suppressed.

For example, let’s say I search for “Mesa, AZ”. The backend returns “Mesa, Arizona” and it also indicates that the highlighted substring is "Mesa, " (it shouldn’t be as integer offsets, because then there’s an ambiguity if it’s unicode codepoints, bytes, etc).

A clear and concise description of what you want to happen.

Please provide a few use cases for this feature

  1. Let the backend be canonical
  2. Search for things that don’t actually match by string, for example, you search for “Elevator” and you get “Lift”

Please Describe alternatives you’ve considered Make searchEngine just return the shortest length string, but that would look kinda weird and I don’t really understand the searchEngine API as documented well enough.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:2
  • Comments:5

github_iconTop GitHub Comments

2reactions
folknorcommented, Sep 21, 2021

It’s kind of hard to parse what you want to accomplish, but I’m 90% certain that it’s entirely possible already.

.searchEngine can be set to your own function, the signature is (query: string, record: any), so for example:

new autoComplete({
    searchEngine: (q, r) => return r.propertythatcontainsmatchingtext,
})

Whatever you return from the search engine is what will be displayed in the dropdown by default. You can of course change this with the resultItem.element setting, etc.

searchEngine is invoked either (a) once per result returned from data.src if data.keys is not set, or (b) once per key per result if data.keys is set.

The record argument is either (a) each array index returned from data.src if data.keys is not set, or (b) data.src[...][key] for each data.keys entry.

Returning null or false or undefined from searchEngine makes autoComplete.js ignore that data entry.

0reactions
jules-w2commented, Nov 13, 2022

I propose this solution.

It works well for me with multiple keywords and the highlight feature. I wrap with <mark></mark> outside my loop to avoid problems if I have a string with ‘mark’ in my keywords.

searchEngine: function (query, record){
 query.replace(/\s+/g,' ').trim().split(" ").forEach(function (substring){
    var regex1 = RegExp(substring, "i")
      if(regex1.test(record)){
        record = record.replace(regex1 ,'!*$&*!');
      }
    })
    return record.replaceAll("!*", "<mark>").replaceAll("*!", "</mark>")
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to safely call an async method in C# without await
I'm sure I don't want to wait for the call to complete; I don't need to or have the time to. But the...
Read more >
Async search | Elasticsearch Guide [8.5] | Elastic
The async search API let you asynchronously execute a search request, monitor its progress, and retrieve partial results as they become available.
Read more >
Asynchronous I/O (asyncio) - SQLAlchemy 1.4 Documentation
dispose() method using await when using the AsyncEngine object in a scope that will go out of context and be garbage collected, as...
Read more >
API Reference: ApolloServer - Apollo GraphQL Docs
A map of functions that populate data for individual schema fields. ... ApolloServer.stop() is an async method that tells all of Apollo Server's...
Read more >
Synchronous vs Asynchronous JavaScript – Call Stack ...
The JavaScript engine uses the stack data structure to keep track of currently executed functions. The stack is called the function execution ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found