Skip to content

OpenSearch Sample Application

Build a REST app that performs OpenSearch operations through an OpenSearchDemoController. You use Winter Boot for the application runtime and the Winter OpenSearch module from Winter Modules for search and analytics.

You need PHP 8.5 or later with the swoole and pcntl extensions. You also need access to an OpenSearch cluster. The sample config points at https://localhost:9200 with basic auth — replace the host and credentials with your own cluster.

The sample uses this layout:

opensearch/
├── bin/
│ └── application.php # Application entry point
├── config/
│ ├── application.yml # Winter Boot application config
│ └── opensearch-config.yml # OpenSearch connections config
├── src/
│ ├── OpenSearchSampleApplication.php # Main application class
│ └── controller/
│ └── OpenSearchDemoController.php # OpenSearch operations controller
└── composer.json # Dependencies

Require the framework and the modules package:

Terminal window
composer require suvera/winter-boot suvera/winter-modules

Switch between the source files. Each tab shows the exact file from the sample.

The single entry point. It points Winter Boot at the config directory and at the sample namespace.

<?php
namespace dev\example;
use dev\winterframework\stereotype\WinterBootApplication;
#[WinterBootApplication(
configDirectory: [__DIR__ . "/../config"],
scanNamespaces: [
['dev\\example', __DIR__ . '']
]
)]
class OpenSearchSampleApplication {
public static function main(): void {
$winterApp = new \dev\winterframework\core\app\WinterWebSwooleApplication();
$winterApp->run(self::class);
}
}

Switch between the two config files. application.yml enables the module, and opensearch-config.yml defines the primary connection with basic auth.

The full sample file registers OpenSearchModule and sets the server and app identity:

server:
port: 8080
address: 0.0.0.0
context-path: /
winter:
application:
name: OpenSearch Sample Application
id: opensearch-sample-app
version: 1.0.0
modules:
- module: dev\winterframework\opensearch\OpenSearchModule
enabled: true
configFile: opensearch-config.yml

See Configuration for every application.yml key.

Start the app, then call the endpoints to check connectivity, index a document, and search it back.

1. Start the application:

Terminal window
composer install
php bin/application.php

2. Check cluster connectivity:

Terminal window
curl "http://localhost:8080/opensearchdemo/ping"

3. Create an index:

Terminal window
curl -X PUT "http://localhost:8080/opensearchdemo/index/my-test-index" \
-H "Content-Type: application/json" -d '{}'

4. Index a document:

Terminal window
curl -X POST "http://localhost:8080/opensearchdemo/doc?index=my-test-index&id=doc-1" \
-H "Content-Type: application/json" \
-d '{"title":"Hello OpenSearch","views":10}'

5. Get the document:

Terminal window
curl "http://localhost:8080/opensearchdemo/doc?index=my-test-index&id=doc-1"

6. Search documents:

Terminal window
curl "http://localhost:8080/opensearchdemo/search?index=my-test-index&field=title&value=Hello"

7. Run the complete demo:

Terminal window
curl "http://localhost:8080/opensearchdemo/demo"

The demo creates its own index, runs create, index, bulk, get, update, search, count, and delete in sequence, and returns one status entry per operation.

  • Read the OpenSearch module for the full OpenSearchTemplate API and AWS SigV4 signing.
  • Browse all Libraries when you need Kafka, SQS, Redis, or Doctrine in the same app.