Aller au contenu

Search

The search functionality allows you to perform full-text search across multiple fields in a single query. This is particularly useful for implementing search bars, autocomplete features, or general content discovery.

When you mark fields as searchable: true in your DTO, a search parameter is automatically added to all query endpoints. The search will be performed across all searchable fields simultaneously.

To enable search on a field, use the searchable option in @EnhancedField:

import { EnhancedField, IDField } from '@lib/type-graphql-query';
import { ObjectType, ID, Field } from '@lib/type-graphql';
@ObjectType('Product')
export class Product {
@IDField(() => ID)
id!: string;
@EnhancedField({ searchable: true })
name!: string;
@EnhancedField({ searchable: true })
description!: string;
@EnhancedField()
price!: number;
}

Once you have searchable fields, you can use the search parameter in your queries:

GraphQL:

{
products(search: "premium eggs") {
edges {
node {
id
name
description
price
}
}
}
}

The search will match records where any searchable field contains the search term.

You can configure advanced search options for each field:

import { EnhancedField, IDField } from '@lib/type-graphql-query';
import { ObjectType, ID, Field } from '@lib/type-graphql';
@ObjectType('Product')
export class Product {
@IDField(() => ID)
id!: string;
@EnhancedField({
searchable: {
unaccent: true, // Remove accents from search text
simplified: true, // Use simplified text matching
escapeHtml: true, // Escape HTML characters
op: 'ILIKE', // Use ILIKE (case-insensitive) or EQUAL
},
})
name!: string;
@EnhancedField({ searchable: true })
description!: string;
}
  • unaccent - When set to true, removes accents from the search text. Useful for languages with accented characters (e.g., “café” matches “cafe”).
  • simplified - When set to true, uses simplified text matching. Useful for handling special characters.
  • escapeHtml - When set to true, escapes HTML characters in the search text. Useful when searching content that may contain HTML.
  • op - The SQL operator to use:
    • 'ILIKE' - Case-insensitive pattern matching (default)
    • 'EQUAL' - Exact match

You can combine search with other filters:

GraphQL:

{
products(search: "premium", filter: { price: { gt: 10 } }) {
edges {
node {
id
name
description
price
}
}
}
}

The search and filter are combined using AND logic - records must match both the search term and the filter criteria.

Search works seamlessly with sorting and paging:

GraphQL:

{
products(search: "eggs", sorting: [{ field: "price", direction: ASC }], paging: { first: 10 }) {
edges {
node {
id
name
price
}
}
pageInfo {
hasNextPage
hasPreviousPage
}
}
}

When you have multiple searchable fields, the search is performed across all of them:

@ObjectType('Article')
export class Article {
@IDField(() => ID)
id!: string;
@EnhancedField({ searchable: true })
title!: string;
@EnhancedField({ searchable: true })
content!: string;
@EnhancedField({ searchable: true })
tags!: string;
}

A search query will match if the search term appears in any of these fields.

  1. Use searchable for text fields - Only mark text fields as searchable, not numeric or date fields
  2. Configure search options appropriately - Use unaccent: true for multilingual content
  3. Combine with filters - Use filters to narrow down results before searching
  4. Consider performance - Search across many fields can be slower; use indexes appropriately
  5. Use ILIKE for flexible matching - The default ILIKE operator provides case-insensitive pattern matching