Skip to content

using doc_count from one aggregation into another aggregation to calculate additional percentage #8211

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
rajeshetty opened this issue Oct 23, 2014 · 5 comments

Comments

@rajeshetty
Copy link

Lets say I have doc like this

      {
       user_id : "12312321321"
       location_shared_with { [ ]},
       location {location_name:.... 
                      location_verification[{} ]
                } 
       }

In here location_verification is an array of all the friends who have verified the location and it will always be equal or less than location_shared_with array.

mapping

    { 
        "user" : 
            {"properties" : 
                 { 
                    "locations":
                    {
                        "type" : "nested",
                        "properties" : 
                        {
                            "location_name": { "type": "string", "index": "not_analyzed"},
                            "location_verification":
                            {
                                "type" : "nested",
                                "properties" : 
                                {
                                    "rating": { "type": "short", "store":"true" }
                                }
                            }
                        }
                    },
                    "location_shared_with":
                    {
                        "type" : "nested",
                        "properties" : 
                        {
                            "status": { "type": "string", "store":"true", "index": "analyzed"},
                            "friend_id": { "type": "string", "store" : "true"}
                        }
                    }
                 }
            }
    }'

So I need to calculate the % verified per location array . I have created right "nested" mapping between main doc (user) and location , location and location_verification. I have terms aggregation that tells me

when I execute

<pre>
GET _search?search_type=count
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "user_id": "1"
          }
        }
      ]
    }
  },  
  "aggs": {
    "location_shared_with_count":{
      "nested": {
        "path": "location_shared_with"
      }
     },
    "locations_count": {
      "nested": {
        "path": "locations"
      },
      "aggs": {
        "locations_aggs": {
          "terms": {
            "field": "locations.location_name"
          },
          "aggs": {
              "location_verification_count": {
                "nested": {
                  "path": "locations.location_verification"
                }

              }
            }          
        }
      }
    }
  },   
    "size": 10
} 

i do get following

    {
       "took": 2,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 1,
          "max_score": 0,
          "hits": []
       },
       "aggregations": {
          "location_shared_with_count": {
             "doc_count": 6
          },
          "locations_count": {
             "doc_count": 2,
             "locations_aggs": {
                "buckets": [
                   {
                      "key": "Boston",
                      "doc_count": 1,
                      "location_verification_count": {
                         "doc_count": 2
                      }
                   },
                   {
                      "key": "Miami",
                      "doc_count": 1,
                      "location_verification_count": {
                         "doc_count": 0
                      }
                   }
                ]
             }
          }
       }
    }

  • Location verification count (doc_count) per location - perfect . So I do get e.g. "Boston" location was verified by 2 friends (doc_count) .

What I want to do is calculate % verified per location .
Formula = [ (location_verification_count -> doc_count / location_shared_with_count->doc_count) x 100 ].

Question is

  • How can I get or use existing doc_count in another sub-aggregation to calculate above %
  • What kind of aggregation type I would use to perform above ?
@clintongormley
Copy link
Contributor

Hi @rajeshetty

Please ask questions like these in the mailing list, rather than on the github issues. That said, you would have to do this calculation application side today. However when #8110 is merged, you should be able to do things like this.

@rajeshetty
Copy link
Author

when is 8110 going into main release ?. what release would that be ?

@jpountz
Copy link
Contributor

jpountz commented Oct 24, 2014

@rajeshetty It is scheduled for elaticsearch 2.0 (see tags of the issue), which I don't see released before at least several months.

@rajeshetty
Copy link
Author

Appreciate your quick response on this. Which mailing list you recommend asking these kind of questions.? - Stackoverflow or somewhere else?.

You see your answer about new reducers was very helpful even though i can't use it anywhere in coming months till 2.0 is out, but still knowing that ES is working on it is useful , So which forum would give me such a quick turn around on my queries around questions I might have ?

@clintongormley
Copy link
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants