"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > Fuzzy Matching with Trigram Search: Building Intelligent Search in Node.js and MySQL

Fuzzy Matching with Trigram Search: Building Intelligent Search in Node.js and MySQL

Published on 2024-11-09
Browse:750

Fuzzy Matching with Trigram Search: Building Intelligent Search in Node.js and MySQL

Introduction
In modern applications, efficient text search is crucial, especially when dealing with large databases. While MySQL provides basic full-text search capabilities, it falls short when it comes to fuzzy matching or handling misspellings. This is where trigram-based search comes into play. In this blog, we’ll explore what a trigram is, how it improves search performance, and how you can implement trigram search in MySQL.

What is a Trigram?
A trigram is a sequence of three consecutive characters from a given string. For instance, the word "search" can be broken down into the following trigrams:

  • sea
  • ear
  • arc
  • rch By breaking down words into trigrams, we can perform more flexible and efficient text matching, especially when trying to match incomplete or slightly misspelled terms.

Implementing Trigram Search in MySQL By Creating Temporary Table

1.Create Trigram Function in MySQL database. Trigram function code:

CREATE FUNCTION TRIGRAM_SEARCH(search_string VARCHAR(255), target_string VARCHAR(255)) 
RETURNS FLOAT
DETERMINISTIC
BEGIN
    DECLARE i INT DEFAULT 1;
    DECLARE total_trigrams INT DEFAULT 0;
    DECLARE matched_trigrams INT DEFAULT 0;
    DECLARE search_length INT;
    DECLARE target_length INT;

    SET search_length = CHAR_LENGTH(search_string);
    SET target_length = CHAR_LENGTH(target_string);

    -- Handle edge cases where strings are too short
    IF search_length  0 THEN
        RETURN matched_trigrams / total_trigrams;
    ELSE
        RETURN 0;
    END IF;
END;

2.Now Indexing Desired Column to full-text

@Entity()
@Index(['title'], { fulltext: true })
export class Ebook extends BaseEntity {
  @PrimaryGeneratedColumn()
  ebookId: number;

  @Column({ nullable: true })
  title: string;
}

3.Testing of Trigram Search Function

select *
FROM ebook e 
WHERE TRIGRAM_SEARCH('physis onlu', e.title) > 0.4
ORDER BY TRIGRAM_SEARCH('physis onlu', e.title) desc;

4.Implement trigram search in code

 async find(title?: string) {
        const eBooks = await this.dataSource
      .getRepository(Ebook)
      .createQueryBuilder('eBook');
    if (title) {
      eBooks.where(`TRIGRAM_SEARCH(:title, eBook.title) > 0.4`, { title });
    }
    const result = await eBooks.getMany();
    return result;
  }

Conclusion
Trigram search offers a powerful way to implement fuzzy matching in MySQL databases. By breaking down text into trigrams, we can perform more flexible and forgiving searches, greatly enhancing the user experience in applications where text search is crucial.
While this approach has its strengths, it's important to consider alternatives like Levenshtein distance or soundex algorithms depending on your specific use case and performance requirements.
By implementing trigram search, you can significantly improve the search capabilities of your Node.js and MySQL applications, providing users with more intelligent and forgiving search results.

Release Statement This article is reproduced at: https://dev.to/mukesh_rajbanshi/fuzzy-matching-with-trigram-search-building-intelligent-search-in-nodejs-and-mysql-2839?1 If there is any infringement, please contact study_golang@163 .comdelete
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3