-4

Is there a way in javascript to search against an array and return results based on relevancy, similar to Apache Solr full-text search capabilities?

Example:

var testData = [
  '101',
  '102C',
  '103B',
  'A',
  'BD',
  'AB',
  'A102B',
  'A1',
];
//if search Text is 102
var searchText = "102";
// results would be 102C, A102B

//if search Text is 102B
var searchText = "102B";
// result would be A102B, 102C

//if search Text is A
var searchText = "A";
// result would be A, A1, AB, A102

Related question : stackoverflow.com/questions/5324798/

What is the best way to approach this using JavaScript?

Steve Mulvihill
  • 688
  • 6
  • 13
Dreamer
  • 53
  • 1
  • 8
  • 3
    so why doesn't 102 return A102B? yet 102B returns 102C - seems like arbitrary logic, which is harder to code than actual logic – Jaromanda X Jul 17 '18 at 02:38
  • 1
    what do you mean by most similar? Is it the set of strings that has substring of a given string? – D. Seah Jul 17 '18 at 02:40
  • @user3808826 Thank you for comment, most simillar means most cotain same text in array. I get data from server but the data is Not standardized so i need to find most same keyword out of the data. related question : https://stackoverflow.com/questions/5324798/how-to-search-an-array-in-jquery-like-sql-like-value-statement – Dreamer Jul 17 '18 at 02:43
  • 1
    Hey there, and welcome to SO! Your question has a couple of issues. Firstly, as previously mentioned, your logic is a bit unclear - you'll need to provide some more detail about what you mean by "most similar". Second, your question doesn't show that you've made any attempts or done any research of your own. As StackOverflow is a resource to help you out with *specific* problems, it relies on the question-asker showing that they've made an effort of their own before coming here. Please edit your question to show any attempts and/or research you've done to solve this on your own. – Tyler Roper Jul 17 '18 at 02:44
  • 1
    Are we looking at indexof here? I guess we kind of are but still need to return values in each one. – Luay Jul 17 '18 at 02:47
  • The question is bit unclear. May be you can explain it a bit more. Also there were spelling errors in the questions. – Parth Manaktala Jul 17 '18 at 02:57
  • @JaromandaX fixed; number order is primary than alphabet? – Dreamer Jul 17 '18 at 03:00

1 Answers1

2

It appears you are looking for fuzzy search functionality that returns results based on relevancy. Take a look at Fuse.js, it provides Solr-like searching against arrays and objects in JavaScript.

http://fusejs.io

Steve Mulvihill
  • 688
  • 6
  • 13