How FacetWP’s Indexer Works

Indexing is the process of storing facet data into a custom database table. It allows FacetWP to retrieve data very quickly and efficiently.

How is the indexer triggered?

If facets are added, we ask that users press the Re-index button within FacetWP’s admin UI. This completely rebuilds the index table.

FacetWP automatically indexes changes when posts or taxonomy terms are updated.

The facetwp_index table

FacetWP adds one database table: facetwp_index. This table contains all the information needed to generate the facets.

facetwp-index-table

How does the indexer work?

When the “Re-index” button is clicked, FacetWP first retrieves all post IDs that should be indexed. It uses WP_Query, and the query arguments are customizable.

FacetWP then loops through the array of post IDs. For each, it loops through all available facets and saves applicable values. Below is a pseudocode example:

foreach ( $post_ids as $post_id ) {
    foreach ( $facets as $facet ) {
        index_values( $post_id, $facet );
    }
}

The values for each row can be modified using the facetwp_index_row filter.

Built-in resuming

Indexing should “just work” on every site, regardless of the amount of content or the server’s hardware specs. For this reason, we’ve designed the indexer to be extremely resilient.

The indexer can stall for various reasons: the server ran out of memory, power outage, etc. The important part is that the indexer picks up where it left off, which is exactly what happens.

When indexing, you’ll see a progress bar. This serves two purposes: it keeps you informed, but it also pings the server to check for a stalled indexer.

The indexer frequently updates information about itself:

{"num_indexed":980,"num_total":2650,"retries":0,"touch":1452541131}

If the “touch” date (see above) is more than 60 seconds old, FacetWP assumes the indexer has stalled, and starts a new process. Since the “retries” value will have increased, any old processes will automatically die if they haven’t already.