Current Location: Home> Latest Articles> RiSearch + PHP: Implementing Efficient Time-Series Data Search and Aggregation

RiSearch + PHP: Implementing Efficient Time-Series Data Search and Aggregation

M66 2025-10-20

Introduction

As internet applications continue to evolve, systems generate massive amounts of time-series data—such as sensor readings, logs, and stock prices. These datasets are continuous and rapidly growing, making fast and accurate search and aggregation a major challenge. This article demonstrates how to build a high-performance time-series data search and aggregation solution using RiSearch and PHP.

Setting Up RiSearch and PHP

Before implementation, you need to install Redis, RiSearch, and the PHP Redis extension. Below is the setup process:

  • Install Redis: Refer to the official Redis documentation for installation instructions.
  • Install RiSearch: Download and build it from the official GitHub repository.
  • Install PHP Redis extension: Run pecl install redis and enable it in your PHP configuration file.

Creating a Time-Series Index

Indexes are the foundation for performing searches and aggregations in RiSearch. The following example shows how to create a log-style time-series index using PHP:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$redis->rawCommand('FT.CREATE', 'logs', 'SCHEMA', 'timestamp', 'NUMERIC', 'content', 'TEXT');

// Add log data to the index
$logs = array(
    array('timestamp' => 1615516800, 'content' => 'This is the first log'),
    array('timestamp' => 1615603200, 'content' => 'This is the second log'),
    // ...
);

foreach ($logs as $log) {
    $redis->rawCommand('FT.ADD', 'logs', 'doc:'.$log['timestamp'], 1.0, 'FIELDS',
        'timestamp', $log['timestamp'],
        'content', $log['content']);
}
?>

The above script defines a logs index with two fields—timestamp and content—and then inserts multiple log entries into it. This index structure enables fast search and aggregation operations later on.

Searching Time-Series Data

RiSearch provides flexible query capabilities that allow fast filtering based on time ranges. The following PHP code demonstrates how to search for logs within a specific time range:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$query = '@timestamp:[1615516800 1615603200]'; // Search logs from 2021-03-12 to 2021-03-13

$result = $redis->rawCommand('FT.SEARCH', 'logs', $query, 'LIMIT', 0, 10);

// Output search results
foreach ($result as $key => $value) {
    if ($key % 2 === 0) {
        echo 'Log ID: '.$value."\n";
    } else {
        echo 'Content: '.$value."\n";
    }
}
?>

Using the FT.SEARCH command, you can easily retrieve logs within a time range and iterate over the results. This method also supports complex query conditions and multiple fields.

Aggregating Time-Series Data

Aggregation is a common requirement in log analytics and monitoring systems—for example, counting how many times a specific log message appears. The example below uses the RiSearch aggregation command to perform this task:

<?php
$redis = new Redis();
$redis->connect('127.0.0.1', 6379);

$query = '@timestamp:[1615516800 1615603200]'; // Search logs within the time range

$result = $redis->rawCommand('FT.AGGREGATE', 'logs', $query, 'LOAD', 1,
    'GROUPBY', 1, '@content',
    'REDUCE', 'COUNT', 0, 'AS', 'count');

// Output aggregation results
foreach ($result as $item) {
    echo 'Content: '.$item['@content'].' | Count: '.$item['count']."\n";
}
?>

The FT.AGGREGATE command groups query results and counts how many times each unique log content appears, making it ideal for log analysis, event statistics, and report generation.

Conclusion

By combining RiSearch and PHP, developers can easily build high-performance systems for searching and aggregating time-series data. Through the three core steps—index creation, query execution, and aggregation—you can efficiently process large datasets. Depending on your use case, this approach can be extended with features such as data visualization, intelligent alerts, or multidimensional analysis to create a complete data analytics platform.