Skip to content

Fuzzy Search

M ABD AZIZ ALFIAN edited this page Aug 15, 2023 · 16 revisions

Fuzzy search is the process of finding strings that approximately match a given string. A fuzzy matching search algorithm will be able to find relevant strings even if your original string contains typo errors and misspellings.

Usage

Simple Example

const data = [
  {
    name: 'Betania Ivana Besoli Leiten',
    location: 'El Salvador'
  },
  {
    name: 'Alexandría DCastillo Gayubas',
    location: 'Bolivia'
  }
];

// Keys is like indexes and required for best result and performance.
const keys = ['name'];

const nosql = new FlyJson();
const result = nosql
  .set(data)
  // user misspelling by typing "als" but fuzzy search did corrected it.
  .fuzzySearch('als', keys)
  // execute
  .exec();

// output result
[
  {
    name: 'Alexandría DCastillo Gayubas',
    location: 'Bolivia'
  }
]

Back to top


Advanced Example

const data = [
  {
    persons: [
      { firstname: 'Patricia', lastname: 'Millaruelo' },
      { firstname: 'Itziar', lastname: 'Julia' }
    ],
    country: 'us'
  },
  {
    persons: [
      { firstname: 'Alexandría', lastname: 'DCastillo' },
      { firstname: 'Gayubas', lastname: 'Pumarola' }
    ],
    country: 'us'
  },
  {
    persons: [
      { firstname: 'Fuji', lastname: 'Nakata' },
      { firstname: 'Ayumi', lastname: 'Hamasaki' }
    ],
    country: 'jp'
  }
];

// Keys is like indexes and required for best result and performance.
// you are able to set indexes even in child object or array element json
const keys = ['persons.firstname'];

const nosql = new FlyJson();
const result = nosql
  .set(data)
  // you can use other fly-json-odm method to filter the query before use fuzzySearch
  .where('country','=','us')
  // user misspelling by typing "tzia" but fuzzy search did corrected it.
  .fuzzySearch('tzia', keys)
  // execute
  .exec();

// output result
[
  {
    persons: [
      { firstname: 'Patricia', lastname: 'Millaruelo' },
      { firstname: 'Itziar', lastname: 'Julia' }
    ],
    country: 'us'
  }
]

Back to top


API

  1. fuzzySearch(query='',keys=[],caseSensitive=false,sort=false)

Description:
You can use fuzzySearch to chaining with other fly-json-odm method. This function is actualy based on fuzzy function.

  • query is required, this is a query to search. It will show all data if user input as empty string.
  • keys is an Array and required, this is a field name in list.
  • caseSensitive=false is optional, if you set it as true, it will bring your search more strict.
  • sort=false is optional, if you set it as true, it will sort the best match result at first ordered.
  • return this

You can find more example about fuzzySearch() at here


  1. fuzzy(haystack,query='',keys=[],caseSensitive=false,sort=false)

Description:
You can use fuzzy as function helper, but you can't use this function to chaining with other fly-json-odm method.

  • haystack is required, this is the list data which is could be an array string, number or object.
  • query='' is optional, this is a query to search. It will show all data if user input as empty string.
  • keys=[] is an Array, this is optional for data array string or number but can be required for array object json or you will get empty result.
  • caseSensitive=false is optional, if you set it as true, it will bring your search more strict.
  • sort=false is optional, if you set it as true, it will sort the best match result at first ordered.
  • return Array<string|number|object>

You can find more example about fuzzy() at here

Back to top


Clone this wiki locally