Welcome to 16892 Developer Community-Open, Learning,Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

{"name": "John", "district": 1, "light": 2},
{"name": "John", "district": 5, "light": 2},
{"name": "John", "district": 5, "light": 27},
{"name": "John", "district": 11, "light": 1},
{"name": "John", "district": 11, "light": 0},
{"name": "John", "district": 11, "light": 10},
{"name": "John", "district": 8, "light": 6},
{"name": "Mary", "district": 2, "light": 10},
{"name": "Nick", "district": 1, "light": 0},
{"name": "Bob", "district": 3, "light": 10},
{"name": "Kenny", "district": 1, "light": 8}

When searching by name field, how can i get a summary of the remaining fields by group count?

Example:

When searching by

  "query": {
    "term": {
      "name": {
        "value": "john"
      }
    }
  }

Expected output:

{
  "district": {
    11: 3, // value : the amount of matches
    5: 2,
    1: 1,
  },
  "light": {
    2: 2, // value : the amount of matches
    27: 1,
    10: 1,
    // etc..
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
160 views
Welcome To Ask or Share your Answers For Others

1 Answer

Try a pair of terms aggregations:

{
  "size": 0, 
  "query": {
    "term": {
      "name": {
        "value": "john"
      }
    }
  },
  "aggs": {
    "by_district": {
      "terms": {
        "field": "district"
      }
    },
    "by_light": {
      "terms": {
        "field": "light"
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to 16892 Developer Community-Open, Learning and Share
...