Add a WordPress full-text search layer before your RAG

A customer types an internal product code into your new AI assistant and it says the document doesn’t cover it. The code is in there, on one line of page 12. The model wasn’t lying. It just never saw that line, and nothing in the pipeline can prove the term is absent. A WordPress full-text search layer can prove that in one MySQL query.

Kezhan Shi’s post on the ten RAG mistakes they keep seeing in production puts the vector-database reflex and the trusted “not found” answer near the top of its list. My WordPress fix is smaller than the four-brick architecture the series builds. Add the keyword layer before any embedding pipeline, because MySQL does the two things embeddings can’t: match exact tokens and prove a term is absent from the corpus.

Why the keyword layer comes first

Embeddings rank by similarity. That works for paraphrased prose and fails on everything discrete: SKUs, part numbers, acronyms, references like Section 4.2(a)(iii). The source calls the vector-database reflex the biggest mistake it sees, because that choice dictates the whole infrastructure stack. Cosine top-k always returns something too, so the model only ever sees a few chunks. Its “not found” means “not in these chunks”, which the user reads as “not in the corpus”.

MySQL’s FULLTEXT index does the opposite job. MATCH … AGAINST scores rows on token match. It’s deterministic, it runs against an index, and there’s no API call or drift. And MySQL already runs your site.

Building the WordPress full-text search layer

Put the text where MySQL can index it

The text goes in a custom table, prefixed bbioon_docs, holding post_id, title, and doc_text, with a FULLTEXT index over (title, doc_text), created with dbDelta on activation. Don’t put this in postmeta. A LIKE ‘%term%’ query on meta scans every row, and meta wasn’t built to hold indexed text. InnoDB has supported FULLTEXT since MySQL 5.6, so any live host can run it.

Keep it in sync

Hook save_post for your document post type and replace the row on every save. For the backfill, register a WP-CLI command instead of doing it in a request, because reindexing a few thousand long posts will blow past max execution time.

Query it and gate the refusals

<?php
function bbioon_doc_search( string $query, int $limit = 5 ): array {
    global $wpdb;
    $table = $wpdb->prefix . 'bbioon_docs';

    return $wpdb->get_results( $wpdb->prepare(
        "SELECT post_id, title,
                MATCH ( title, doc_text ) AGAINST ( %s IN BOOLEAN MODE ) AS score
         FROM {$table}
         WHERE MATCH ( title, doc_text ) AGAINST ( %s IN BOOLEAN MODE )
         ORDER BY score DESC
         LIMIT %d",
        $query, $query, $limit
    ) ) ?: [];
}

// Absence proof: search the whole corpus, not the retrieved chunks.
function bbioon_corpus_contains( string $term ): bool {
    return bbioon_doc_search( '+' . $term, 1 ) !== [];
}

Boolean mode is the one to use. The plus operator makes the term required, so you get an exact filter when a question carries a code. The second function is the absence proof from the source’s last pitfall. Before the assistant tells a user the document doesn’t specify something, run the term across the whole corpus. If the search comes back empty, the refusal is defensible. If the term turns up on a page that retrieval never pulled, retrieval missed, and that’s the trigger for a second pass.

When this is the wrong tool

Keyword matching has a blind spot. “Do I have to pay this back?” won’t hit a clause titled “reimbursement obligation”, and that gap is where embeddings and a reranker earn their keep. It’s the mirror image of how vector search misses SKUs and internal product codes. Plan for hybrid search with re-ranking from the start, words first and embeddings as fallback.

The MySQL defaults can bite. innodb_ft_min_token_size is 3 by default, so a two-character code like the insurance acronym the source mentions won’t index until you change that server variable and rebuild. Non-Latin scripts need the ngram parser. And on shared hosting you often can’t change server variables at all, so check before promising exact matching.

If you’re wiring an assistant over product docs or manuals, this is the kind of plugin work I take on: the table, the sync hook, the CLI reindex, and the check that gates refusals.

Test before building any of it. Pull the last twenty real queries from the site’s search log and run them against the keyword layer alone. If most resolve, the vector pipeline can wait (I’ve been surprised how often this is enough), and the customer with the product code stops hearing that the answer isn’t in the document.

author avatar
Ahmad Wael
I'm a WordPress and WooCommerce developer with 15+ years of experience building custom e-commerce solutions and plugins. I specialize in PHP development, following WordPress coding standards to deliver clean, maintainable code. Currently, I'm exploring AI and e-commerce by building multi-agent systems and SaaS products that integrate technologies like Google Gemini API with WordPress platforms, approaching every project with a commitment to performance, security, and exceptional user experience.

Leave a Comment