Given the following Elasticsearch mapping and query, what will be the result count?
{
"mappings": {
"properties": {
"user": {
"type": "object",
"properties": {
"name": { "type": "keyword" },
"age": { "type": "integer" }
}
}
}
}
}
POST /users/_search
{
"query": {
"bool": {
"must": [
{ "term": { "user.name": "alice" } },
{ "range": { "user.age": { "gte": 30 } } }
]
}
}
}Remember that term queries match exact values and range queries filter by numeric ranges.
The term query matches exact values for user.name. The range query filters user.age to be greater than or equal to 30. Both conditions must be true because of the bool must clause.
Choose the best reason to use nested type instead of object type for a field containing arrays of objects.
Think about how Elasticsearch matches fields inside arrays of objects.
The nested type stores each object in the array as a separate hidden document, allowing queries to match fields within the same object. The object type flattens all fields, which can cause incorrect matches across different objects.
Given this mapping and query, why does the query return no results?
{
"mappings": {
"properties": {
"comments": {
"type": "nested",
"properties": {
"author": { "type": "keyword" },
"message": { "type": "text" }
}
}
}
}
}
POST /posts/_search
{
"query": {
"bool": {
"must": [
{ "term": { "comments.author": "john" } },
{ "match": { "comments.message": "great" } }
]
}
}
}Check how nested fields must be queried in Elasticsearch.
When querying nested fields, you must use a nested query to scope the conditions inside the nested documents. Without it, the query treats nested fields as flat, causing no matches.
Which option contains the correct syntax for defining a nested field items with properties product (keyword) and quantity (integer)?
Check the correct keyword to specify nested type in Elasticsearch mappings.
The correct way to define a nested field is to set type to nested and then define properties. Options A, B, and D use invalid or unsupported keywords.
Given this mapping and data, what is the count of unique authors returned by the nested aggregation?
{
"mappings": {
"properties": {
"reviews": {
"type": "nested",
"properties": {
"author": { "type": "keyword" },
"rating": { "type": "integer" }
}
}
}
}
}
Data example:
{
"reviews": [
{"author": "alice", "rating": 5},
{"author": "bob", "rating": 4},
{"author": "alice", "rating": 3}
]
}
Aggregation query:
{
"aggs": {
"nested_reviews": {
"nested": { "path": "reviews" },
"aggs": {
"unique_authors": {
"cardinality": { "field": "reviews.author" }
}
}
}
}
}Count distinct authors inside the nested reviews array.
The nested aggregation counts unique authors inside the nested reviews field. The authors are 'alice' and 'bob', so the count is 2.