ElasticSearch – Structure of a Search Request/Response

Elasticsearch search requests are JSON document-based requests or URL-based requests. The requests are sent to the server with the same format, so we should understand some important components that we can change for each search request and look at a typical response.

Related Posts:
Elasticsearch Overview
ElasticSearch Filter vs Query
ElasticSearch Full Text Queries – Basic

I. Search Scope

Before discussing different components, we talk about the scope of search request.
All REST search requests use the _search endpoint and can be a GET/POST request. We can search entire cluster or limit the scope by specifying the names of indices or types in the request URL.

The following list provides search examples:

II. Basic Components

Once we indicate the indices to search, we need to configure some important components:

query configures the best documents to return based on a score, as well as the documents you don’t want to return (using the query DSL).
For example, all tutorials with the word “elasticsearch” in the title limited to tutorials in recent two years.

size indicates the amount of documents to return. Defaults to 10.

from is index of the documents to return. Defaults to 0.

_source specifies how the _source field is returned.
The default is to return complete _source field.
If our indexed documents are big and we don’t need the full content in your result, we can filter the fields that are returned (using _source_include & _source_exclude).
Set to false to disable retrieval of the _source field.

sort
Default sorting is based on the _score for a document.
If we don’t care about the _score, adding a sort helps us to control which documents get returned.

III. Request body–based

When we execute advanced searches, it’s more flexibility and more options with request body–based searches.

3.1 Pagination and selected Fields

This is an example of searching for the second page (5 items per page) of javasampleapproach‘s tutorials, returning only title and author fields for each item:

3.2 Wildcards in returned fields

We can also specify wildcards for fields.

– If we wanted to return both title and timeline field, we just specify:

– We can also specify multiple wildcards like:

– Not only which fields to include but also which fields we don’t want to return:

3.3 Sort Oder

If no sort order is specified, Elasticsearch returns matching documents sorted by the _score value in descending order, with the most relevant (highest scoring) documents first.

To sort fields (ascending or descending order), we specify an array of maps.
We can sort on any number of fields.

For example, we can return results sorted first by creation date (starting with the latest); then by the title (in alphabetical order); and finally, by the _score of the result:

*Note: Before search statement works well, we have to enable fielddata:

IV. Structure of Response

Now look at an example that have a response like this:



By grokonez | October 24, 2017.


Related Posts


Got Something To Say:

Your email address will not be published. Required fields are marked *

*