How to order ilike data based on it's ranking?

Hi all, is it possible to fetch some data by using ilike for searching and add something like an ordering ranking without using external rcp function?

Like I have a function like this:

  public async getCompanies(pageIndex: number, isCreatingLicense: boolean, isSegnalator: boolean = false): Promise<any> {
    const PAGE_SIZE = 20;
    const from = pageIndex * PAGE_SIZE;
    const to = (pageIndex + 1) * PAGE_SIZE - 1;

    const request = this.service.supabase.from('companies').select('id, name', { count: 'exact' }).range(from, to);

    const searchTerm = this.#search()?.toLowerCase() ?? '';
    if (searchTerm) {
      request.or(`id.ilike.%${searchTerm}%, name.ilike.%${searchTerm}%`);
    }

    if (isCreatingLicense === true) {
      const currentCompany = this.service.getCompany();

      request.or(`type.eq.CUSTOMER,id.eq.${currentCompany}`);
    }

    if (isSegnalator) {
      request.neq('type', 'CUSTOMER');
    }

    request.order('name');

    return await request.overrideTypes<Company[]>();
  }


If the search term is "GAB" and I have data like:

[{"id":"02682270356","name":"BAR IL VAGABONDO DI FERRARI ANDREA"}, 
 {"id":"00168780351","name":"GAB TAMAGNINI SRL"}, 
 {"id":"51436261096","name":"GAB_Test1"}, 
 {"id":"02124180353","name":"PIZZERIA DA GABRIELE DI CALIFANO GABRIELE E NASTA ANNA"}]


The first result should be GAB Tamagnini SRL, then GAB_Test1, while I get BAR as first result of the query.
Was this page helpful?