Skip to content

Commit

Permalink
[DOCS] Revises search-operations.asciidoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
szabosteve committed Mar 18, 2020
1 parent e7a4c4e commit fb5c8b2
Showing 1 changed file with 49 additions and 27 deletions.
76 changes: 49 additions & 27 deletions docs/search-operations.asciidoc
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
[[search_operations]]
== Search Operations
== Search operations

Well...it isn't called elasticsearch for nothing! Let's talk about search operations in the client.
Well...it isn't called {es} for nothing! Let's talk about search operations in
the client.

The client gives you full access to every query and parameter exposed by the REST API, following the naming scheme as
much as possible. Let's look at a few examples so you can become familiar with the syntax.
The client gives you full access to every query and parameter exposed by the
REST API, following the naming scheme as much as possible. Let's look at a few
examples so you can become familiar with the syntax.

=== Match Query

Here is a standard curl for a Match query:
=== Match query

Here is a standard curl for a match query:

[source,shell]
----
Expand All @@ -22,6 +25,7 @@ curl -XGET 'localhost:9200/my_index/_search' -d '{
----
{zwsp} +


And here is the same query constructed in the client:

[source,php]
Expand All @@ -41,9 +45,11 @@ $results = $client->search($params);
----
{zwsp} +

Notice how the structure and layout of the PHP array is identical to that of the JSON request body. This makes it very
simple to convert JSON examples into PHP. A quick method to check your PHP array (for more complex examples) is to
encode it back to JSON and check by eye:

Notice how the structure and layout of the PHP array is identical to that of the
JSON request body. This makes it very simple to convert JSON examples into PHP.
A quick method to check your PHP array (for more complex examples) is to encode
it back to JSON and check it:

[source,php]
----
Expand All @@ -68,8 +74,9 @@ print_r(json_encode($params['body']));

.Using Raw JSON
****
Sometimes it is convenient to use raw JSON for testing purposes, or when migrating from a different system. You can
use raw JSON as a string in the body, and the client will detect this automatically:
Sometimes it is convenient to use raw JSON for testing purposes, or when
migrating from a different system. You can use raw JSON as a string in the body,
and the client detects this automatically:
[source,php]
----
Expand All @@ -91,8 +98,10 @@ $results = $client->search($params);
****
{zwsp} +

Search results follow the same format as Elasticsearch search response, the only difference is that the JSON response is
serialized back into PHP arrays. Working with the search results is as simple as iterating over the array values:

Search results follow the same format as {es} search response, the only
difference is that the JSON response is serialized back into PHP arrays. Working
with the search results is as simple as iterating over the array values:

[source,php]
----
Expand All @@ -117,9 +126,12 @@ $doc = $results['hits']['hits'][0]['_source'];
----
{zwsp} +


=== Bool Queries

Bool queries can be easily constructed using the client. For example, this query:
Bool queries can be easily constructed using the client. For example, this
query:

[source,shell]
----
curl -XGET 'localhost:9200/my_index/_search' -d '{
Expand All @@ -139,7 +151,9 @@ curl -XGET 'localhost:9200/my_index/_search' -d '{
----
{zwsp} +

Would be structured like this (Note the position of the square brackets):

Would be structured like this (note the position of the square brackets):

[source,php]
----
$params = [
Expand All @@ -160,14 +174,18 @@ $results = $client->search($params);
----
{zwsp} +

Notice that the `must` clause accepts an array of arrays. This will be serialized into an array of JSON objects internally,
so the final resulting output will be identical to the curl example. For more details about arrays vs objects in PHP,

Notice that the `must` clause accepts an array of arrays. This is serialized
into an array of JSON objects internally, so the final resulting output is
identical to the curl example. For more details about arrays and objects in PHP,
see <<php_json_objects, Dealing with JSON Arrays and Objects in PHP>>.


=== A more complicated example

Let's construct a slightly more complicated example: a boolean query that contains both a filter and a query.
This is a very common activity in elasticsearch queries, so it will be a good demonstration.
Let's construct a slightly more complicated example: a boolean query that
contains both a filter and a query. This is a very common activity in {es}
queries, so it will be a good demonstration.

The curl version of the query:

Expand All @@ -188,6 +206,7 @@ curl -XGET 'localhost:9200/my_index/_search' -d '{
----
{zwsp} +


And in PHP:

[source,php]
Expand Down Expand Up @@ -216,16 +235,19 @@ $results = $client->search($params);

=== Scrolling

The Scrolling functionality of Elasticsearch is used to paginate over many documents in a bulk manner, such as exporting
all the documents belonging to a single user. It is more efficient than regular search because it doesn't need to maintain
an expensive priority queue ordering the documents.
The scrolling functionality of {es} is used to paginate over many documents in a
bulk manner, such as exporting all the documents belonging to a single user. It
is more efficient than regular search because it doesn't need to maintain an
expensive priority queue ordering the documents.

Scrolling works by maintaining a "point in time" snapshot of the index which is then used to page over.
This window allows consistent paging even if there is background indexing/updating/deleting. First, you execute a search
request with `scroll` enabled. This returns a "page" of documents, and a scroll_id which is used to continue
paginating through the hits.
Scrolling works by maintaining a "point in time" snapshot of the index which is
then used to page over. This window allows consistent paging even if there is
background indexing/updating/deleting. First, you execute a search request with
`scroll` enabled. This returns a "page" of documents, and a `scroll_id` which is
used to continue paginating through the hits.

More details about scrolling can be found in the https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-body.html#request-body-search-scroll[Link: reference documentation].
More details about scrolling can be found in the
{ref}/search-request-body.html#request-body-search-scroll[reference documentation].

This is an example which can be used as a template for more advanced operations:

Expand Down

0 comments on commit fb5c8b2

Please sign in to comment.