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.
Overview
Section intitulée « Overview »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.
Configuring Searchable Fields
Section intitulée « Configuring Searchable Fields »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;}Basic Search
Section intitulée « Basic Search »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.
Search Options
Section intitulée « Search Options »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;}Search Options Explained
Section intitulée « Search Options Explained »unaccent- When set totrue, removes accents from the search text. Useful for languages with accented characters (e.g., “café” matches “cafe”).simplified- When set totrue, uses simplified text matching. Useful for handling special characters.escapeHtml- When set totrue, 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
Combining Search with Filtering
Section intitulée « Combining Search with Filtering »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.
Combining Search with Sorting and Paging
Section intitulée « Combining Search with Sorting and Paging »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 } }}Multiple Searchable Fields
Section intitulée « Multiple Searchable Fields »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.
Best Practices
Section intitulée « Best Practices »- Use searchable for text fields - Only mark text fields as searchable, not numeric or date fields
- Configure search options appropriately - Use
unaccent: truefor multilingual content - Combine with filters - Use filters to narrow down results before searching
- Consider performance - Search across many fields can be slower; use indexes appropriately
- Use ILIKE for flexible matching - The default
ILIKEoperator provides case-insensitive pattern matching