Hi, I have an index where I need to store medical test results. A test result can talk about many conditions and their results: for example, Tuberculosis => positive, Flu => negative. So I modeled my index like this:
curl -XPUT "http://localhost:9200/test_results/" -d' { "mappings": { "result": { "properties": { "data": { "type": "nested", "properties": { "condition": {"type": "string"}, "result": {"type": "string"} } } } } } }' I insert one test result with Tuberculosis => positive, Flu => negative: curl -XPOST "http://localhost:9200/test_results/_bulk" -d' {"index":{"_index":"test_results","_type":"result"}} {"data": [{"condition": "Tuberculosis", "result": "positive"}, {"condition": "FLU", "result": "negative"}]} ' Then, one of the queries I need to do is this one: for Tuberculosis, give me how many positives you have and how many negatives you have (basically: filter by data.condition and group by data.result). So I tried this query: curl -XPOST "http://localhost:9200/test_results/_search?pretty=true" -d'{ "size": 0, "query": { "nested": { "path": "data", "query": { "match": { "data.condition": "Tuberculosis" } } } }, "aggregations": { "data": { "nested": { "path": "data" }, "aggregations": { "result": { "terms": { "field": "data.result" } } } } } } ' However, the above gives me this result: "aggregations" : { "data" : { "doc_count" : 2, "result" : { "buckets" : [ { "key" : "negative", "doc_count" : 1 }, { "key" : "positive", "doc_count" : 1 } ] } } } That is, it gives me one negative result and one positive result. That's because the document has one positive and negative, and it's not discarding the one that has "Flu". I see in the documentation there's a "filter" aggregate. I tried using it in many ways: 1. With term on "data.condition": curl -XPOST "http://localhost:9200/test_results/_search?pretty=true" -d'{ "size": 0, "query": { "nested": { "path": "data", "query": { "match": { "data.condition": "Tuberculosis" } } } }, "aggregations": { "data": { "nested": { "path": "data" }, "aggregations": { "filtered_result": { "filter": { "term": { "data.condition" : "Tuberculosis" } }, "aggregations" : { "result": { "terms": { "field": "data.result" } } } } } } } } ' 2. With term on "condition": curl -XPOST "http://localhost:9200/test_results/_search?pretty=true" -d'{ "size": 0, "query": { "nested": { "path": "data", "query": { "match": { "data.condition": "Tuberculosis" } } } }, "aggregations": { "data": { "nested": { "path": "data" }, "aggregations": { "filtered_result": { "filter": { "term": { "condition" : "Tuberculosis" } }, "aggregations" : { "result": { "terms": { "field": "data.result" } } } } } } } } ' 3. With nested: curl -XPOST "http://localhost:9200/test_results/_search?pretty=true" -d'{ "size": 0, "query": { "nested": { "path": "data", "query": { "match": { "data.condition": "Tuberculosis" } } } }, "aggregations": { "data": { "nested": { "path": "data" }, "aggregations": { "filtered_result": { "filter": { "nested": { "path": "data", "filter": { "term": { "data.condition": "Tuberculosis" } } } }, "aggregations" : { "result": { "terms": { "field": "data.result" } } } } } } } } ' but no luck: all of the above queries just give me: "aggregations" : { "data" : { "doc_count" : 2, "filtered_result" : { "doc_count" : 0, "result" : { "buckets" : [ ] } } } } Is there a way to do what I want? Thanks, Ary -- 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 [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/f5094888-6654-4d40-bf9b-d81ec1e5add4%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
