# Multi field aggregation with Elasticsearch-kibana

**URL:** <https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301>\
**Category:** Kibana\
**Created:** [October 25, 2017, 9:29pm UTC](https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301 "2017-10-25T21:29:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![shethsaam](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@shethsaam](https://discuss.elastic.co/u/shethsaam)\
**Post date:** [October 25, 2017, 9:29pm UTC](https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301/1 "2017-10-25T21:29:32Z")

</div>

With kibana devtools, I am trying to create a query to get unique values from multiple fields into a single bucket. I am using cppy\_to to create a new field and plan to find the unique values with aggregation. I need to find a way to put the data from the existing fields into the newly created field.  
Current Code:

```auto
PUT indexme
{
  "mappings": {
    "my_type": {
      "properties": {
        "wlan_da": {
          "type": "keyword",
          "copy_to": "full_namesas" 
        },
        "wlan_sa": {
          "type": "keyword",
          "copy_to": "full_namesas" 
        },
        
        "full_namesas": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT indexme/my_type/1
{
  "wlan_da": { }
  "wlan_sa": { }
}

GET indexme/_search
{
  "aggs": {
    "testprac_wala2": {
      "terms": {
        "field": "full_namesas"
      }
    }
  }
}

```

Need help with `PUT indexme/my_type/1`. Let me know if I am wrong somewhere or missing something. New to elastic community

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [October 26, 2017, 10:58pm UTC](https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301/2 "2017-10-26T22:58:41Z")

</div>

It should look like this:

```auto
POST indexme/my_type
{
  "wlan_da": "foo",
  "wlan_sa": "bar"
}

```

You're indexing the properties `wlan_da` and `wlan_sa` which are available on `full_names`.

---

<div class="post-metadata">

**Author:** ![shethsaam](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@shethsaam](https://discuss.elastic.co/u/shethsaam)\
**Post date:** [October 26, 2017, 11:27pm UTC](https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301/3 "2017-10-26T23:27:58Z")

</div>

Hi tylersmalley,  
Thanks for your suggestions.  
I am unable to understand what you actually mean by "foo" and" bar". More clear explanation of what I am trying: I have an index called abc which has fields wlan\_da and wlan\_sa. Both this fields have some values. I want to aggregate over this values together in a single bucket to find the unique values in wlan\_da and wlan\_sa field. So in POST indexme/my\_type, I want to insert values from wlan\_sa (index=abc) into wlan\_sa (index=indexme) and values from wlan\_da (index=abc) into wlan\_da (index=indexme) so that later by aggregating on full\_names, I get unique values from both the fields together in a single bucket.

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [October 27, 2017, 5:18pm UTC](https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301/4 "2017-10-27T17:18:30Z")

</div>

"foo" and "bar" are just test values. The API call creates a document with those values assigned to `wlan_da` and `wlan_sa`. Then, using your search they are aggregated over full\_names.

Does that help?

```auto
PUT abc
{
  "mappings": {
    "my_type": {
      "properties": {
        "wlan_da": {
          "type": "keyword",
          "copy_to": "full_namesas" 
        },
        "wlan_sa": {
          "type": "keyword",
          "copy_to": "full_namesas" 
        },
        
        "full_namesas": {
          "type": "keyword"
        }
      }
    }
  }
}

POST abc/my_type
{
  "wlan_da": "foo",
  "wlan_sa": "bar"
}

GET abc/_search
{
  "aggs": {
    "testprac_wala2": {
      "terms": {
        "field": "full_namesas"
      }
    }
  }
}

```

 ![52](https://us1.discourse-cdn.com/elastic/original/3X/7/4/747b338fd5b79d9d1ba5ac85c7efbd3ac3210866.png)

---

<div class="post-metadata">

**Author:** ![shethsaam](https://avatars.discourse-cdn.com/v4/letter/s/f1d935/32.png) [@shethsaam](https://discuss.elastic.co/u/shethsaam)\
**Post date:** [October 27, 2017, 6:04pm UTC](https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301/5 "2017-10-27T18:04:31Z")

</div>

Hi tylersmalley,  
It did helped me. I want to replace test values "foo" and "bar" with existing values in the wlan\_da and wlan\_sa type of some other index (say XYZ). So I am trying to pull values from the wlan\_da and wlan\_sa field of some other index ( XYZ) and trying to put them in the full\_namesas. So How to get this values in place of "foo" and "bar" ??

---

<div class="post-metadata">

**Author:** ![tylersmalley](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tylersmalley/32/8833_2.png) [@tylersmalley](https://discuss.elastic.co/u/tylersmalley)\
**Post date:** [October 27, 2017, 6:33pm UTC](https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301/6 "2017-10-27T18:33:23Z")

</div>

If they are already in an index you will need to re-index as copy\_to happens at index time.

There is an API to do this: [https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-reindex.html)

Here I am creating an index containing only `wlan_da` and `wlan_sa` and adding a single document.

```auto
PUT abc
{
  "mappings": {
    "doc": {
      "properties": {
        "wlan_da": {
          "type": "keyword"
        },
        "wlan_sa": {
          "type": "keyword"
        }
      }
    }
  }
}

POST abc/doc
{
  "wlan_da": "foo",
  "wlan_sa": "bar"
}

```

Then, I create a new index with the mapping containing `copy_to` with the desired `full_namesas` and re-index the data into it.

```auto
PUT abc2
{
  "mappings": {
    "doc": {
      "properties": {
        "wlan_da": {
          "type": "keyword",
          "copy_to": "full_namesas" 
        },
        "wlan_sa": {
          "type": "keyword",
          "copy_to": "full_namesas" 
        },
        
        "full_namesas": {
          "type": "keyword"
        }
      }
    }
  }
}

POST _reindex
{
  "source": {
    "index": "abc"
  },
  "dest": {
    "index": "abc2"
  }
}

GET abc2/_search
{
  "aggs": {
    "testprac_wala2": {
      "terms": {
        "field": "full_namesas"
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [November 24, 2017, 6:33pm UTC](https://discuss.elastic.co/t/multi-field-aggregation-with-elasticsearch-kibana/105301/7 "2017-11-24T18:33:26Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
