Skip to content

Commit 3193c51

Browse files
authored
Merge pull request #1007 from szabosteve/7.x.json.index
[DOCS] Fine-tunes JSON arrays and index operations pages in PHP client book
2 parents 8af5775 + 2ffcae7 commit 3193c51

File tree

2 files changed

+84
-50
lines changed

2 files changed

+84
-50
lines changed

docs/index-operations.asciidoc

Lines changed: 52 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
[[index_management]]
2-
== Index Management Operations
2+
== Index management operations
3+
4+
Index management operations allow you to manage the indices in your {es}
5+
cluster, such as creating, deleting and updating indices and their
6+
mappings/settings.
37

4-
Index management operations allow you to manage the indices in your Elasticsearch cluster, such as creating, deleting and
5-
updating indices and their mappings/settings.
68

79
=== Create an index
810

9-
The index operations are all contained under a distinct namespace, separated from other methods that are on the root
10-
client object. As an example, let's create a new index:
11+
The index operations are all contained under a distinct namespace, separated
12+
from other methods that are on the root client object. As an example, let's
13+
create a new index:
1114

1215
[source,php]
1316
----
@@ -21,8 +24,9 @@ $response = $client->indices()->create($params);
2124
----
2225
{zwsp} +
2326

24-
You can specify any parameters that would normally be included in a new index creation API. All parameters that
25-
would normally go in the request body are located in the 'body' parameter:
27+
You can specify any parameters that would normally be included in a new index
28+
creation API. All parameters that would normally go in the request body are
29+
located in the 'body' parameter:
2630

2731
[source,php]
2832
----
@@ -56,11 +60,13 @@ $response = $client->indices()->create($params);
5660
----
5761
{zwsp} +
5862

63+
5964
=== Create an index (advanced example)
6065

61-
This is a more complicated example of creating an index, showing how to define analyzers, tokenizers, filters and
62-
index settings. Although essentially the same as the previous example, the more complicated example can be helpful
63-
for "real world" usage of the client, since this particular syntax is easy to mess up.
66+
This is a more complicated example of creating an index, showing how to define
67+
analyzers, tokenizers, filters and index settings. Although essentially the same
68+
as the previous example, the more complicated example can be helpful for "real
69+
world" usage of the client since this particular syntax is easy to mess up.
6470

6571
[source,php]
6672
----
@@ -125,9 +131,12 @@ $params = [
125131
];
126132
$client->indices()->create($params);
127133
----
128-
<1> The top level `settings` contains config about the index (# of shards, etc) as well as analyzers
129-
<2> `analysis` is nested inside of `settings`, and contains tokenizers, filters, char filters and analyzers
130-
<3> `mappings` is another element nested inside of `settings`, and contains the mappings for various types
134+
<1> The top level `settings` contains config about the index (# of shards, etc)
135+
as well as analyzers.
136+
<2> `analysis` is nested inside of `settings`, and contains tokenizers, filters,
137+
char filters and analyzers.
138+
<3> `mappings` is another element nested inside of `settings`, and contains the
139+
mappings for various types.
131140

132141

133142
=== Delete an index
@@ -141,8 +150,10 @@ $response = $client->indices()->delete($params);
141150
----
142151
{zwsp} +
143152

144-
=== Put Settings API
145-
The Put Settings API allows you to modify any index setting that is dynamic:
153+
154+
=== PUT Settings API
155+
156+
The PUT Settings API allows you to modify any index setting that is dynamic:
146157

147158
[source,php]
148159
----
@@ -160,9 +171,11 @@ $response = $client->indices()->putSettings($params);
160171
----
161172
{zwsp} +
162173

163-
=== Get Settings API
164174

165-
Get Settings API will show you the currently configured settings for one or more indexes:
175+
=== GET Settings API
176+
177+
The GET Settings API shows you the currently configured settings for one or more
178+
indices:
166179

167180
[source,php]
168181
----
@@ -178,9 +191,10 @@ $response = $client->indices()->getSettings($params);
178191
----
179192
{zwsp} +
180193

181-
=== Put Mappings API
182194

183-
The Put Mappings API allows you to modify or add to an existing index's mapping.
195+
=== PUT Mappings API
196+
197+
The PUT Mappings API allows you to modify or add to an existing index's mapping.
184198

185199
[source,php]
186200
----
@@ -208,34 +222,45 @@ $client->indices()->putMapping($params);
208222
----
209223
{zwsp} +
210224

211-
=== Get Mappings API
212225

213-
The Get Mappings API will return the mapping details about your indexes. Depending on the mappings that you wish to retrieve, you can specify one of more indexes:
226+
=== GET Mappings API
227+
228+
The GET Mappings API returns the mapping details about your indices. Depending
229+
on the mappings that you wish to retrieve, you can specify one of more indices:
214230

215231
[source,php]
216232
----
217-
// Get mappings for all indexes
233+
// Get mappings for all indices
218234
$response = $client->indices()->getMapping();
219235
220236
// Get mappings in 'my_index'
221237
$params = ['index' => 'my_index'];
222238
$response = $client->indices()->getMapping($params);
223239
224-
// Get mappings for two indexes
240+
// Get mappings for two indices
225241
$params = [
226242
'index' => [ 'my_index', 'my_index2' ]
227243
];
228244
$response = $client->indices()->getMapping($params);
229245
----
230246
{zwsp} +
231247

232-
=== Other APIs in the Indices Namespace
233-
There are a number of other APIs in the indices namespace that allow you to manage your elasticsearch indexes (add/remove templates, flush segments, close indexes, etc).
234248

235-
If you use an IDE with autocompletion, you should be able to easily explore the indices namespace by typing:
249+
=== Other APIs in the indices namespace
250+
251+
There are a number of other APIs in the indices namespace that allow you to
252+
manage your {es} indices (add/remove templates, flush segments, close indices,
253+
etc).
254+
255+
If you use an IDE with autocompletion, you should be able to easily explore the
256+
indices namespace by typing:
236257

237258
[source,php]
238259
----
239260
$client->indices()->
240261
----
241-
And perusing the list of available methods. Alternatively, browsing the `\Elasticsearch\Namespaces\Indices.php` file will show you the full list of available method calls (as well as parameter lists in the comments for each method).
262+
263+
And perusing the list of available methods. Alternatively, browsing the
264+
`\Elasticsearch\Namespaces\Indices.php` file shows you the full list of
265+
available method calls (as well as parameter lists in the comments for each
266+
method).

docs/php_json_objects.asciidoc

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
[[php_json_objects]]
2-
== Dealing with JSON Arrays and Objects in PHP
2+
== Dealing with JSON arrays and objects in PHP
33

4-
A common source of confusion with the client revolves around JSON arrays and objects, and how to specify them in PHP.
5-
In particular, problems are caused by empty objects and arrays of objects. This page will show you some common patterns
6-
used in Elasticsearch JSON API, and how to convert that to a PHP representation
4+
A common source of confusion with the client revolves around JSON arrays and
5+
objects, and how to specify them in PHP. In particular, problems are caused by
6+
empty objects and arrays of objects. This page shows you some common patterns
7+
used in {es} JSON API and how to convert that to a PHP representation.
78

89
=== Empty Objects
910

10-
The Elasticsearch API uses empty JSON objects in several locations, and this can cause problems for PHP. Unlike other
11-
languages, PHP does not have a "short" notation for empty objects and so many developers are unaware how to specify
12-
an empty object.
11+
The {es} API uses empty JSON objects in several locations which can cause
12+
problems for PHP. Unlike other languages, PHP does not have a "short" notation
13+
for empty objects and many developers are unaware how to specify an empty
14+
object.
1315

14-
Consider adding a Highlight to a query:
16+
Consider adding a highlight to a query:
1517

1618
[source,json]
1719
----
@@ -30,9 +32,10 @@ Consider adding a Highlight to a query:
3032
----
3133
<1> This empty JSON object is what causes problems.
3234

33-
The problem is that PHP will automatically convert `"content" : {}` into `"content" : []`, which is no longer valid
34-
Elasticsearch DSL. We need to tell PHP that the empty object is explicitly an object, not an array. To define this
35-
query in PHP, you would do:
35+
The problem is that PHP will automatically convert `"content" : {}` into
36+
`"content" : []`, which is no longer valid {es} DSL. We need to tell PHP that
37+
the empty object is explicitly an object, not an array. To define this query in
38+
PHP, you would do:
3639

3740
[source,json]
3841
----
@@ -50,15 +53,18 @@ $params['body'] = array(
5053
);
5154
$results = $client->search($params);
5255
----
53-
<1> We use the generic PHP stdClass object to represent an empty object. The JSON will now encode correctly.
56+
<1> We use the generic PHP stdClass object to represent an empty object. The
57+
JSON now encodes correctly.
5458

55-
By using an explicit stdClass object, we can force the `json_encode` parser to correctly output an empty object, instead
56-
of an empty array. Sadly, this verbose solution is the only way to acomplish the goal in PHP...there is no "short"
59+
By using an explicit stdClass object, we can force the `json_encode` parser to
60+
correctly output an empty object, instead of an empty array. This verbose
61+
solution is the only way to acomplish the goal in PHP... there is no "short"
5762
version of an empty object.
5863

5964
=== Arrays of Objects
6065

61-
Another common pattern in Elasticsearch DSL is an array of objects. For example, consider adding a sort to your query:
66+
Another common pattern in {es} DSL is an array of objects. For example, consider
67+
adding a sort to your query:
6268

6369
[source,json]
6470
----
@@ -72,11 +78,12 @@ Another common pattern in Elasticsearch DSL is an array of objects. For example
7278
]
7379
}
7480
----
75-
<1> "sort" contains an array of JSON objects
81+
<1> "sort" contains an array of JSON objects.
7682

77-
This arrangement is *very* common, but the construction in PHP can be tricky since it requires nesting arrays. The
78-
verbosity of PHP tends to obscure what is actually going on. To construct an array of objects, you actually need
79-
an array of arrays:
83+
This arrangement is very common, but the construction in PHP can be tricky since
84+
it requires nesting arrays. The verbosity of PHP tends to obscure what is
85+
actually going on. To construct an array of objects, you actually need an array
86+
of arrays:
8087

8188
[source,json]
8289
----
@@ -97,8 +104,8 @@ $results = $client->search($params);
97104
<2> This array encodes the `{"time" : {"order" : "desc"}}` object
98105
<3> This array encodes the `{"popularity" : {"order" : "desc"}}` object
99106

100-
If you are on PHP 5.4+, I would strongly encourage you to use the short array syntax. It makes these nested arrays
101-
much simpler to read:
107+
If you are on PHP 5.4+, we strongly encourage you to use the short array syntax.
108+
It makes these nested arrays much simpler to read:
102109

103110
[source,json]
104111
----
@@ -118,10 +125,12 @@ $results = $client->search($params);
118125

119126
=== Arrays of empty objects
120127

121-
Occasionally, you'll encounter DSL that requires both of the previous patterns. The function score query is a good
122-
example, it sometimes requires an array of objects, and some of those objects might be empty JSON objects.
128+
Occasionally, you'll encounter DSL that requires both of the previous patterns.
129+
The function score query is a good example, it sometimes requires an array of
130+
objects, and some of those objects might be empty JSON objects.
123131

124132
Given this query:
133+
125134
[source,json]
126135
----
127136
{

0 commit comments

Comments
 (0)