CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-fuse-js

Lightweight fuzzy-search library with zero dependencies that implements efficient fuzzy string matching algorithms

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

extended-search.mddocs/

Extended Search

Advanced search syntax with unix-like commands and logical operators. Enables exact matches, prefix/suffix matching, exclusions, and complex query expressions that go beyond basic fuzzy matching.

Capabilities

Extended Search Configuration

Enable extended search functionality with additional configuration options.

interface ExtendedSearchOptions {
  /** Enable unix-like search commands (default: false) */
  useExtendedSearch?: boolean;
}

Usage Examples:

import Fuse from "fuse.js";

// Enable extended search
const fuse = new Fuse(books, {
  keys: ["title", "author"],
  useExtendedSearch: true
});

// Now you can use extended search syntax
const results = fuse.search("'javascript"); // Fuzzy match
const exact = fuse.search('"JavaScript"'); // Exact match
const prefix = fuse.search("^Java"); // Prefix match

Logical Expressions

Complex query expressions using logical operators for advanced search combinations.

type Expression =
  | { [key: string]: string }
  | { $path: ReadonlyArray<string>; $val: string }
  | { $and?: Expression[] }
  | { $or?: Expression[] };

Usage Examples:

// Field-specific search
const fieldResults = fuse.search({
  title: "javascript",
  author: "crockford"
});

// Path-based search for nested fields
const pathResults = fuse.search({
  $path: ["author", "name"],
  $val: "douglas"
});

// AND logic - all conditions must match
const andResults = fuse.search({
  $and: [
    { title: "javascript" },
    { author: "crockford" },
    { year: "2008" }
  ]
});

// OR logic - any condition can match
const orResults = fuse.search({
  $or: [
    { title: "javascript" },
    { title: "python" },
    { title: "ruby" }
  ]
});

// Complex nested logic
const complexResults = fuse.search({
  $and: [
    {
      $or: [
        { title: "javascript" },
        { title: "typescript" }
      ]
    },
    { author: "crockford" }
  ]
});

Extended Search Syntax

Unix-like search commands for different matching behaviors.

String-based Extended Search Patterns:

// Exact match - must match exactly
const exactResults = fuse.search('"javascript"');

// Fuzzy match (default behavior, explicit)
const fuzzyResults = fuse.search("'javascript");

// Prefix match - starts with pattern
const prefixResults = fuse.search("^java");

// Suffix match - ends with pattern  
const suffixResults = fuse.search("script$");

// Inverse match - does NOT contain pattern
const inverseResults = fuse.search("!php");

// Include match - must contain pattern
const includeResults = fuse.search("=javascript");

Usage Examples:

const books = [
  { title: "JavaScript: The Good Parts", author: "Douglas Crockford" },
  { title: "TypeScript Handbook", author: "Microsoft" },
  { title: "Java: The Complete Reference", author: "Herbert Schildt" },
  { title: "Python Programming", author: "Mark Lutz" }
];

const fuse = new Fuse(books, {
  keys: ["title", "author"],
  useExtendedSearch: true
});

// Exact title match
const exact = fuse.search('"JavaScript: The Good Parts"');

// All books starting with "Java"
const javaBooks = fuse.search("^Java");

// All books ending with "Handbook"
const handbooks = fuse.search("Handbook$");

// All books NOT about Python
const nonPython = fuse.search("!Python");

// All books that must contain "Script"
const scriptBooks = fuse.search("=Script");

Field-Specific Extended Search

Combining extended search syntax with field targeting for precise control.

Usage Examples:

// Exact match in specific field
const exactAuthor = fuse.search({
  author: '"Douglas Crockford"'
});

// Prefix match in title field
const titlePrefix = fuse.search({
  title: "^JavaScript"
});

// Multiple fields with different patterns
const mixedSearch = fuse.search({
  $and: [
    { title: "^Java" },        // Title starts with "Java"
    { author: "!Microsoft" }   // Author is not Microsoft
  ]
});

// Complex field patterns
const complexField = fuse.search({
  $or: [
    { title: '"Complete Reference"' },  // Exact title match
    { author: "=Crockford" }           // Author contains "Crockford"
  ]
});

Extended Search with Nested Fields

Using extended search syntax with nested object properties.

Usage Examples:

const products = [
  {
    name: "JavaScript Book",
    details: {
      category: "Programming",
      language: "English",
      tags: ["js", "programming", "web"]
    }
  }
];

const fuse = new Fuse(products, {
  keys: ["name", "details.category", "details.tags"],
  useExtendedSearch: true
});

// Exact category match
const categoryResults = fuse.search({
  "details.category": '"Programming"'
});

// Prefix search in nested field
const nestedPrefix = fuse.search({
  $path: ["details", "language"],
  $val: "^Eng"
});

// Complex nested search
const nestedComplex = fuse.search({
  $and: [
    { name: "=JavaScript" },
    { "details.category": '"Programming"' },
    { "details.tags": "!python" }
  ]
});

Extended Search Error Handling

Extended search features require specific configuration and may throw errors.

Error Conditions:

// These will throw errors if extended search is not enabled:

try {
  const fuse = new Fuse(data, { useExtendedSearch: false });
  fuse.search({ $and: [{ title: "test" }] }); // Throws error
} catch (error) {
  console.error("Extended search not enabled");
}

try {
  fuse.search('"exact match"'); // May work depending on implementation
} catch (error) {
  console.error("Extended search syntax not available");
}

Usage Examples:

// Safe extended search usage
function safeExtendedSearch(fuse, query) {
  try {
    return fuse.search(query);
  } catch (error) {
    if (error.message.includes("EXTENDED_SEARCH_UNAVAILABLE")) {
      console.warn("Extended search not available, falling back to basic search");
      // Convert extended query to basic search if possible
      const basicQuery = typeof query === "string" ? query.replace(/['"^$!=]/g, "") : "";
      return fuse.search(basicQuery);
    }
    throw error;
  }
}

// Check if extended search is enabled
function supportsExtendedSearch(fuse) {
  try {
    fuse.search({ $and: [] });
    return true;
  } catch {
    return false;
  }
}

Performance Considerations

Extended search features have different performance characteristics.

Usage Examples:

// Logical expressions are more expensive than simple searches
const simple = fuse.search("javascript");        // Fast
const logical = fuse.search({                    // Slower
  $and: [
    { title: "javascript" },
    { author: "crockford" }
  ]
});

// Exact matches can be faster than fuzzy matches
const fuzzy = fuse.search("'javascript");        // Normal fuzzy search
const exact = fuse.search('"JavaScript"');       // Potentially faster

// Complex nested logic impacts performance
const complex = fuse.search({                    // Slowest
  $or: [
    {
      $and: [
        { title: "^Java" },
        { author: "!Microsoft" }
      ]
    },
    {
      $and: [
        { title: "=Script" },
        { category: '"Programming"' }
      ]
    }
  ]
});

docs

collection-management.md

core-search.md

extended-search.md

index-management.md

index.md

tile.json