Skip to content

Latest commit

 

History

History
123 lines (97 loc) · 3.01 KB

File metadata and controls

123 lines (97 loc) · 3.01 KB

graphile-pg-textsearch-plugin

graphile-pg-textsearch-plugin enables auto-discovered BM25 ranked full-text search for PostGraphile v5 schemas using pg_textsearch.

Installation

npm install graphile-pg-textsearch-plugin

Features

  • Auto-discovery: Finds all text columns with BM25 indexes automatically — zero configuration
  • Condition fields: bm25<Column> condition inputs accepting { query, threshold? } for BM25 ranked search
  • Score fields: bm25<Column>Score computed fields returning BM25 relevance scores (negative values, more negative = more relevant)
  • OrderBy: BM25_<COLUMN>_SCORE_ASC/DESC enum values for sorting by relevance
  • Works with PostGraphile v5 preset/plugin pipeline

Usage

With Preset (Recommended)

import { Bm25SearchPreset } from 'graphile-pg-textsearch-plugin';

const preset = {
  extends: [
    // ... your other presets
    Bm25SearchPreset(),
  ],
};

With Plugin Directly

import { Bm25CodecPlugin, Bm25SearchPlugin } from 'graphile-pg-textsearch-plugin';

const preset = {
  plugins: [
    Bm25CodecPlugin,
    Bm25SearchPlugin(),
  ],
};

GraphQL Query

query SearchArticles($search: Bm25SearchInput!) {
  articles(condition: { bm25Body: $search }) {
    nodes {
      id
      title
      body
      bm25BodyScore
    }
  }
}

Variables:

{
  "search": {
    "query": "postgres full text search",
    "threshold": -0.5
  }
}

OrderBy

query SearchArticlesSorted($search: Bm25SearchInput!) {
  articles(
    condition: { bm25Body: $search }
    orderBy: BM25_BODY_SCORE_ASC
  ) {
    nodes {
      id
      title
      bm25BodyScore
    }
  }
}

Requirements

  • PostgreSQL with pg_textsearch extension installed
  • PostGraphile v5 (rc.5+)
  • A BM25 index on the text column(s) you want to search:
CREATE INDEX articles_body_idx ON articles USING bm25(body)
  WITH (text_config='english');

Testing

# requires constructiveio/postgres-plus:18 Docker image with pg_textsearch pre-installed
pnpm --filter graphile-pg-textsearch-plugin test