# Watcher's transform adds smaller number of the values

**URL:** <https://discuss.elastic.co/t/watchers-transform-adds-smaller-number-of-the-values/329497>\
**Category:** Elasticsearch\
**Created:** [April 6, 2023, 9:07am UTC](https://discuss.elastic.co/t/watchers-transform-adds-smaller-number-of-the-values/329497 "2023-04-06T09:07:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ddoroshenko](https://avatars.discourse-cdn.com/v4/letter/d/b9e5f3/32.png) [@ddoroshenko](https://discuss.elastic.co/u/ddoroshenko)\
**Post date:** [April 6, 2023, 9:07am UTC](https://discuss.elastic.co/t/watchers-transform-adds-smaller-number-of-the-values/329497/1 "2023-04-06T09:07:48Z")

</div>

Hi,

I'd like to make watcher which will write to a certain index unique values of the particular field.

A part of my query is

```auto
{
  "query" : {
    "bool": {
      "must": [
         { "wildcard": { "field": { "value": "foo-bar-*" }}}
       ]
    }
  }
}

```

Then I have the following transform in `action` section

```auto
List unique = new ArrayList();
for (hit in ctx['payload']['hits']['hits']) {
  if (!unique.contains(hit['_source']['field'])) {
    unique.add(hit['_source']['field']);
  }
}
return ['unique':unique];

```

and then

```auto
"index": {
  "index": "unique-values",
  "doc_id": "0001"
}

```

This configuration will write to the index some unique values but less than 10.  
Aggregation `cardinality` shows 58 (looks like true).

The question is why it adds to the list smaller number of the values? How can I fix it?

---

<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:** [May 4, 2023, 9:07am UTC](https://discuss.elastic.co/t/watchers-transform-adds-smaller-number-of-the-values/329497/2 "2023-05-04T09:07:55Z")

</div>

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