11# OpenSearch Adapter
22
3- A PHP adapter for the official [ OpenSearch PHP client] ( https://github.com/opensearch-project/opensearch-php ) .
3+ A PHP adapter for the [ OpenSearch PHP client] ( https://github.com/opensearch-project/opensearch-php ) .
44
55## Installation
66
@@ -10,9 +10,9 @@ Install the package with Composer:
1010composer require directorytree/opensearch-adapter
1111```
1212
13- ## Usage
13+ ## Creating Managers
1414
15- Create the adapter managers with an ` OpenSearch\Client ` instance:
15+ Create the adapter managers from an ` OpenSearch\Client ` instance:
1616
1717``` php
1818use DirectoryTree\OpenSearchAdapter\Documents\DocumentManager;
@@ -23,4 +23,131 @@ $documents = new DocumentManager($client);
2323$indices = new IndexManager($client);
2424```
2525
26- The adapter provides value objects for documents, index mappings, settings, aliases, search requests, and search responses.
26+ ## Managing Indices
27+
28+ Use an index blueprint when you want fluent mapping and settings builders:
29+
30+ ``` php
31+ use DirectoryTree\OpenSearchAdapter\Indices\IndexBlueprint;
32+ use DirectoryTree\OpenSearchAdapter\Indices\Mapping;
33+ use DirectoryTree\OpenSearchAdapter\Indices\Settings;
34+
35+ $mapping = (new Mapping)
36+ ->keyword('id')
37+ ->text('title')
38+ ->object('author', [
39+ 'properties' => [
40+ 'name' => ['type' => 'text'],
41+ ],
42+ ]);
43+
44+ $settings = (new Settings)->index([
45+ 'number_of_shards' => 1,
46+ 'number_of_replicas' => 0,
47+ ]);
48+
49+ $indices->create(new IndexBlueprint('books', $mapping, $settings));
50+ ```
51+
52+ Raw OpenSearch arrays are supported when you already have a mapping or settings payload:
53+
54+ ``` php
55+ $indices->createRaw(
56+ 'books',
57+ mapping: [
58+ 'properties' => [
59+ 'title' => ['type' => 'text'],
60+ ],
61+ ],
62+ settings: [
63+ 'index' => [
64+ 'number_of_shards' => 1,
65+ ],
66+ ],
67+ );
68+ ```
69+
70+ ## Indexing Documents
71+
72+ Documents contain the OpenSearch document ID and source payload:
73+
74+ ``` php
75+ use DirectoryTree\OpenSearchAdapter\Documents\Document;
76+
77+ $documents->index('books', collect([
78+ new Document('1', [
79+ 'title' => 'The Hobbit',
80+ ]),
81+ ]));
82+ ```
83+
84+ Routing values can be attached by document ID:
85+
86+ ``` php
87+ use DirectoryTree\OpenSearchAdapter\Documents\Routing;
88+
89+ $routing = (new Routing)->add('1', 'tenant-1');
90+
91+ $documents->index('books', collect([
92+ new Document('1', [
93+ 'title' => 'The Hobbit',
94+ ]),
95+ ]), routing: $routing);
96+ ```
97+
98+ ## Searching Documents
99+
100+ Build a search request with raw OpenSearch query fragments:
101+
102+ ``` php
103+ use DirectoryTree\OpenSearchAdapter\Search\SearchRequest;
104+
105+ $request = (new SearchRequest([
106+ 'match' => [
107+ 'title' => 'hobbit',
108+ ],
109+ ]))
110+ ->size(10)
111+ ->highlight([
112+ 'fields' => [
113+ 'title' => new stdClass,
114+ ],
115+ ]);
116+
117+ $response = $documents->search('books', $request);
118+
119+ $total = $response->total();
120+
121+ $hits = $response->hits()->map(function ($hit) {
122+ return $hit->document()->content();
123+ });
124+ ```
125+
126+ ## Aliases
127+
128+ Aliases can include optional filters and routing values:
129+
130+ ``` php
131+ use DirectoryTree\OpenSearchAdapter\Indices\Alias;
132+
133+ $indices->putAlias('books', new Alias(
134+ 'tenant-books',
135+ filter: [
136+ 'term' => [
137+ 'tenant_id' => 1,
138+ ],
139+ ],
140+ routing: 'tenant-1',
141+ ));
142+
143+ $aliases = $indices->getAliases('books');
144+ ```
145+
146+ ## Raw Responses
147+
148+ Search response objects expose the original OpenSearch payload through ` raw() ` :
149+
150+ ``` php
151+ $rawHit = $response->hits()->first()->raw();
152+ $rawResponse = $response->raw();
153+ ```
0 commit comments