Fulltext query in MySQL with TypeORM

See original GitHub issue

Issue type:

[x] question [ ] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [x] mysql / mariadb [ ] oracle [ ] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

How can I do a fulltext query in MySQL?

I want to search all the people that has a determinate word or phrase. For example, in the table “People” has:

@Column("varchar")
name: string;

@Column("varchar")
lastname: string;

@Column("text")
personalDescription: string;

So with the word “Programmer” or “I am a programmer in javascript” I want to find who have that info through the functionality “Full-text” of MySQL among the name, lastname, and personalDescription of an user.

Thanks.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:11 (5 by maintainers)

github_iconTop GitHub Comments

62reactions
vlapocommented, Dec 11, 2018

I do not have much experience with mysql fulltext. But my simple test is running.

Entity:

@Entity()
export class User {

    @PrimaryGeneratedColumn()
    id: number;

    @Index({ fulltext: true })
    @Column("varchar")
    name: string;

    @Index({ fulltext: true })
    @Column("varchar")
    lastname: string;

    @Index({ fulltext: true })
    @Column("text")
    personalDescription: string;
}

Select using query builder:

const searchTerm = "programmer";

const result = await connection.manager.getRepository(User)
            .createQueryBuilder()
            .select()
            .where(`MATCH(lastname) AGAINST ('${searchTerm}' IN BOOLEAN MODE)`)
            .orWhere(`MATCH(name) AGAINST ('${searchTerm}' IN BOOLEAN MODE)`)
            .orWhere(`MATCH(personalDescription) AGAINST ('${searchTerm}' IN BOOLEAN MODE)`)
            .getMany();

If you need more advanced fulltext search you have to check mysql documentation.

7reactions
vlapocommented, Dec 11, 2018

You should write find like this:

const result = await connection.manager.getRepository(User).find({
   where: [{
        lastname: Like("%programmer%")
   }, {
        name: Like("%programmer%")
   }, {
        personalDescription: Like("%programmer%")
   }]
});

This is just simple example with LIKE.

I do not have experience with mysql fulltext feature, but if you want to use it you have to define fulltext index with @Index({ fulltext: true }) and use query builder to write own custom query http://typeorm.io/#/select-query-builder/adding-where-expression and use special sql syntax.

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeORM FullText Search - Techiediaries
In this short article, we'll see how to use the builtin full-text search in MySQL and PostgreSQL databases with TypeORM.
Read more >
Postgresql full text search with TypeOrm - Stack Overflow
There is some way to handle full text search with Postgres and TypeOrm. I've seen some examples but they only work with Mysql....
Read more >
Postgresql full text search with TypeOrm - iTecNote
There is some way to handle full text search with Postgres and TypeOrm. I've seen some examples but they only work with Mysql....
Read more >
12.10 Full-Text Search Functions - MySQL :: Developer Zone
A full-text index in MySQL is an index of type FULLTEXT . Full-text indexes can be used only with InnoDB or MyISAM tables,...
Read more >
Perform Full-text Searches in MySQL (Part 2) - Navicat
Leave the Index method blank as it is not required for a FULLTEXT index. Finally, click the Save button to create the index....
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