Playing with elasticsearch for a project.
This is sample data, every document will have a list of ingredients, some
of the ingredients may be used in different documents and in different
quantities. I would like an aggregation that lists all ingredients for a
search, and the stats of each ingredient.
I'm looking to this group for suggestions, can I structure my query
different to get the results I would like (or should I structure my
document different?).
Basically I have two constraints: I don't know what the ingredients are
when the query is written. I would like to list all ingredients, and the
average amounts of each.
Sample data:
{
"ingredients": [
{
"name": "Rock",
"quantity": 6,
"unit": "lb"
},
{
"name": "Dirt",
"quantity": 6,
"unit": "lb"
},
{
"name": "Mortar",
"quantity": 3,
"unit": "lb"
}
]
}
This query looks like it works, but doesn't. I'm quite confused as to
where these numbers are coming from .
Query:
POST /concrete/recipe/_search
{
"query" : {"match_all" : {}},
"aggs" : {
"ingredients" : {
"terms" : {
"field" : "ingredients.name"
},
"aggs" : {
"pounds" : { "stats" : { "field" : "ingredients.quantity" }
}
}
}
}
}
Results:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "concrete",
"_type": "recipe",
"_id": "1",
"_score": 1,
"_source": {
"ingredients": [
{
"name": "Rock",
"quantity": 6,
"unit": "lb"
},
{
"name": "Dirt",
"quantity": 6,
"unit": "lb"
},
{
"name": "Mortar",
"quantity": 3,
"unit": "lb"
}
]
}
},
{
"_index": "concrete",
"_type": "recipe",
"_id": "2",
"_score": 1,
"_source": {
"ingredients": [
{
"name": "Rock",
"quantity": 8,
"unit": "lb"
},
{
"name": "Quartz",
"quantity": 0.5,
"unit": "lb"
},
{
"name": "Mortar",
"quantity": 4.5,
"unit": "lb"
}
]
}
}
]
},
"aggregations": {
"ingredients": {
"buckets": [
{
"key": "mortar",
"doc_count": 2,
"pounds": {
"count": 5,
"min": 0,
"max": 8,
"avg": 4.2,
"sum": 21
}
},
{
"key": "rock",
"doc_count": 2,
"pounds": {
"count": 5,
"min": 0,
"max": 8,
"avg": 4.2,
"sum": 21
}
},
{
"key": "dirt",
"doc_count": 1,
"pounds": {
"count": 2,
"min": 3,
"max": 6,
"avg": 4.5,
"sum": 9
}
},
{
"key": "quartz",
"doc_count": 1,
"pounds": {
"count": 3,
"min": 0,
"max": 8,
"avg": 4,
"sum": 12
}
}
]
}
}
}
Thanks for reading,
Chris
--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/e7ae4039-19f7-4e45-9b72-0465833fbdfa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.