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.

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:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

GET /_search

--> entire cluster

GET /javasampleapproach/_search

--> 'javasampleapproach' index

GET /javasampleapproach/tutorial/_search

--> 'tutorial' type in 'javasampleapproach' index

GET /_all/tutorial/_search

--> all 'tutorial' types in all indices

GET /*/tutorial/_search

--> all 'tutorial' types in all indices

GET /javasampleapproach,angularsampleapproach/tutorial,sourcecode/_search

--> 'tutorial' and 'sourcecode' types in 'javasampleapproach' and 'angularsampleapproach' indices

GET /*sample*,-angularsample*/_search

--> all indices that contain 'sample' but no start with 'angularsample'

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:

1

2

3

4

5

6

7

8

9

GET /javasampleapproach/tutorial/_search

{

"query": {

"match_all": {}

},

"from": 5,

"size": 5,

"_source": ["title", "author"]

}

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:

1

"_source": "ti*"

– We can also specify multiple wildcards like:

1

"_source": ["category.*", "author.*"]

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

1

2

3

4

"_source": {

"include": ["author.*", "title"],

"exclude": ["author.birthday"]

}

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:

1

2

3

4

5

"sort": [

{"created_on": "desc"},

{"title": "asc"},

"_score"

]

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

grokonez

ABOUT US
We are passionate engineers in software development by Java Technology & Spring Framework. We believe that creating little good thing with specific orientation everyday can make great influence on the world someday.