Skip to main content

Controlling Query Results

Specifying the result format

GET /recipes/_search?format=yaml
{
"query": {
"match": { "title": "pasta" }
}
}
  • Make the response pretty
GET /recipes/_search?pretty
{
"query": {
"match": { "title": "pasta" }
}
}

Source Filtering

GET /recipes/_search
{
"_source": "created",
"query": {
"match": { "title": "pasta" }
}
}
  • Example returns match all properties within the objects stored within the ingredients array
GET /recipes/_search
{
"_source": "ingredients.*",
"query": {
"match": { "title": "pasta" }
}
}
  • Example that name property is no longer returned for each object within the ingredients field
GET /recipes/_search
{
"_source": {
"includes": "ingredients.*",
"excludes": "ingredients.name"
}
"query": {
"match": { "title": "pasta" }
}
}

Specifying the result size

  • Two Examples below output same thing where 2 matches are shown
GET /recipes/_search?size=2
{
"_source": false,
"query": {
"match": {
"title": "pasta"
}
}
}

GET /recipes/_search
{
"size": 2,
"_source": false,
"query": {
"match": {
"title": "pasta"
}
}
}

Specifying an offset

GET /recipes/_search
{
"size": 2,
"_source": false,
"from": 4,
"query": {
"match": {
"title": "pasta"
}
}
}
note

The from paramter is like the offset clause in SQL

Pagination

  • Pagination Docs
  • total_pages = ceil(total_hits / page_size)
  • from = (page_size * (page_number - 1))

Sorting Results

GET /recipes/_search
{
"_source": false,
"query": {
"match_all": {}
},
"sort": [
"preparation_time_minutes"
]
}

GET /recipes/_search
{
"_source": "created",
"query": {
"match_all": {}
},
"sort": [
{ "created": "desc" }
]
}

Sorting by multi-value fields

GET /recipes/_search
{
"_source": "ratings",
"query": {
"match_all": {}
},
"sort": [
{
"ratings": {
"order": "desc",
"mode": "avg"
}
}
]
}