Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Hello, I'm using the Java API and am having a hard time making a query that aggregates as intended.
- I tried simplifying the problem data.
- - Our root document "authors" have "books" (nested).
- - Books have a format ("ebook" or "hardcover"), and "chapters" (nested).
- - Chapters have a "chapter_type" ("prologue", "regular", "epilogue").
- Here are the mappings for that:
- curl -XPOST localhost:9200/test_index -d '{
- "mappings": {
- "author": {
- "properties": {
- "books": {
- "type": "nested",
- "properties": {
- "format": {
- "type": "string",
- "index": "not_analyzed"
- },
- "chapters": {
- "type": "nested",
- "properties": {
- "chapter_type": {
- "type": "string",
- "index": "not_analyzed"
- }}}}}}}}}'
- Here is test data:
- curl -XPUT 'localhost:9200/test_index/author/Mr_a' -d '{
- "books": [
- { "format": "ebook",
- "chapters": [
- { "chapter_type" : "prologue" },
- { "chapter_type" : "regular" },
- { "chapter_type" : "regular" },
- { "chapter_type" : "epilogue" }
- ]
- },
- { "format": "hardcover",
- "chapters": [
- { "chapter_type" : "prologue" },
- { "chapter_type" : "regular" }
- ]
- }
- ]
- }'
- I try to get the count per format (terms aggregation on books.format) given that I only want books that have an epilogue
- (In practice I have all sorts of other constraints and query parts on both "books" and "chapters".)
- I tried all sorts of combinations on the aggregation filters and nested filters without success. I either get counts for all books in one author, or nothing at all.
- Here's one of the queries I tried, It's not applying the filter constrains as intended, and I'm getting back counts for ebook:1, and hardcover:1, when what i'm expecting is ebook:1 only.
- curl -XGET 'localhost:9200/test_index/author/_search' -d '{
- "query" : {
- "bool" : {
- "should" : {
- "nested" : {
- "path" : "books",
- "query" : {
- "filtered" : {
- "query" : {
- "bool" : { }
- },
- "filter" : {
- "and" : {
- "filters" : [ {
- "nested" : {
- "path" : "books.chapters",
- "filter" : {
- "and" : {
- "filters" : [ {
- "terms" : {
- "books.chapters.chapter_type" : [ "epilogue" ]
- }
- } ]
- }
- }
- }
- } ]
- }
- }
- }
- },
- "inner_hits" : {
- "name" : "books",
- "_source" : false
- }
- }
- }
- }
- },
- "_source" : false,
- "aggregations" : {
- "filtered_aggregation" : {
- "filter" : {
- "nested" : {
- "filter" : {
- "nested" : {
- "filter" : {
- "term" : {
- "books.chapters.chapter_type" : "epilogue"
- }
- },
- "path" : "books.chapters"
- }
- },
- "path" : "books"
- }
- },
- "aggregations" : {
- "sub_aggregation_books" : {
- "nested" : {
- "path" : "books"
- },
- "aggregations" : {
- "count_formats" : {
- "terms" : {
- "field" : "books.format"
- }}}}}}}}
- '
- Here's the response:
- {
- "took": 3,
- "timed_out": false,
- "_shards": {
- "total": 5,
- "successful": 5,
- "failed": 0
- },
- "hits": {
- "total": 1,
- "max_score": 1,
- "hits": [
- {
- "_index": "test_index",
- "_type": "author",
- "_id": "Mr_a",
- "_score": 1,
- "inner_hits": {
- "books": {
- "hits": {
- "total": 1,
- "max_score": 1,
- "hits": [
- {
- "_index": "test_index",
- "_type": "author",
- "_id": "Mr_a",
- "_nested": {
- "field": "books",
- "offset": 0
- },
- "_score": 1
- }
- ]
- }}}}
- ]
- },
- "aggregations": {
- "filtered_aggregation": {
- "doc_count": 1,
- "sub_aggregation_books": {
- "doc_count": 2,
- "count_formats": {
- "doc_count_error_upper_bound": 0,
- "sum_other_doc_count": 0,
- "buckets": [
- {
- "key": "ebook",
- "doc_count": 1
- },
- {
- "key": "hardcover",
- "doc_count": 1
- }
- ]
- }}}}}
- Here's java code for some trials I have made:
- AbstractAggregationBuilder aggregation = AggregationBuilders.filter("filtered_aggregation")
- .filter(FilterBuilders.nestedFilter("books", FilterBuilders.nestedFilter("books.chapters",
- FilterBuilders.termFilter("books.chapters.chapter_type", "epilogue"))))
- .subAggregation(AggregationBuilders.nested("sub_aggregation_books").path("books")
- .subAggregation(AggregationBuilders.terms("count_formats").field("books.format")));
- AbstractAggregationBuilder aggregation2 =
- AggregationBuilders.nested("nested_in_books").path("books")
- .subAggregation(AggregationBuilders.nested("nested_in_chapters").path("books.chapters")
- .subAggregation(AggregationBuilders.filter("filtered_aggregations")
- .filter(FilterBuilders.termFilter("books.chapters.chapter_type", "epilogue"))
- .subAggregation(AggregationBuilders.terms("count_formats").field(
- "books.chapters.chapter_type"))));
- AbstractAggregationBuilder aggregation3 =
- AggregationBuilders.nested("nested_in_books").path("books")
- .subAggregation(AggregationBuilders.filter("filter_on_books")
- .filter(FilterBuilders.nestedFilter("books.chapters",
- FilterBuilders.termFilter("books.chapters.chapter_type","epilogue")))
- .subAggregation(AggregationBuilders.terms("count_formats").field("books.format")));
- Obviously there's something about nested aggregations and filter aggregations that I'm not getting right... can somebody help me?
- Thanks!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement