Term aggregation field mapping or join


(Anurag Setia) #1

I am fairly new to Elasticsearch and have been able to grasp the basics so far. I have an index of documents of the following structure -

{ team1: Australia, team2: South Africa, winner: team1 score: 3-2 }

My wanna be able to fetch each team name with the number of wins against their names, how do I do that?
I am able to get # of wins by team1 and team2 however, thats clearly not enough. For reasons beyond the topic, I cannot store team name itself in the winner field.


(Colin Goodheart-Smithe) #2

You could possibly use a script in the terms aggregation for this. The following is untested but should give you the idea of what I'm talking about:

{
    "winners": {
        "terms": {
            "script": "var winner = doc['winner']; return doc[winner];"
        }
    }
}

It basically takes the value of the winner field and stores it in a variable winner, and then returns the value of the field with the name of the winner variables value. It comes with the usual caveats of using scripting which you can read about here: https://www.elastic.co/guide/en/elasticsearch/reference/2.1/modules-scripting.html


(system) #3