# Use my own global ordinals with a painless script?

**URL:** <https://discuss.elastic.co/t/use-my-own-global-ordinals-with-a-painless-script/192366>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [July 26, 2019, 6:31am UTC](https://discuss.elastic.co/t/use-my-own-global-ordinals-with-a-painless-script/192366 "2019-07-26T06:31:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kazim\_Zaidi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kazim_zaidi/32/47149_2.png) [@Kazim\_Zaidi](https://discuss.elastic.co/u/Kazim_Zaidi)\
**Post date:** [July 26, 2019, 6:31am UTC](https://discuss.elastic.co/t/use-my-own-global-ordinals-with-a-painless-script/192366/1 "2019-07-26T06:31:08Z")

</div>

Here's what one of my document might look like

```
{
  "CC":{"colors":["Blue","Green","Yellow"]},
  "CN":{"colors":["White","Green","Blue"]},
  "WA":{"colors":["Orange","Green","Blue"]}
}

```

I want a terms aggregation, on the intersection of two fields `CC.colors` and `CN.colors`. That is, for this document, that field will have `["Green", "Blue"]` in the intersection, and I want a term aggregation on this intersection.

As far as I understand, there are two ways to do it.

1. A painless script in terms aggregation, which returns the intersection of these two arrays for each document.
2. A new field created during index time, maybe called `CC_CN.colors`, which holds intersection for all docs.

I can't go ahead with 2 because my combinations will be too many. I can have any need during search time, like CC\_CN, or CC\_WA, or WA\_CN\_CC etc.

For 1), it works, but gets painfully slow. One reason is that 1) cannot use global ordinals.

Is there any trick, that I can ask elastic to build a custom global ordinal for my painless terms aggregation? I know there are just 25 colors in my system, so can give all colors to elastic somewhere, and "assure" them that I'll not return anything else but these colors from my aggregation?

Or, if I encode and store numbers instead of strings in index, would this be faster for elastic? e.g. 0 instead of "Black", 1 instead of "Green" etc.?

Other than intersection, my other use cases involve union etc. as well. Thanks for reading!

---

<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:** [August 23, 2019, 6:31am UTC](https://discuss.elastic.co/t/use-my-own-global-ordinals-with-a-painless-script/192366/2 "2019-08-23T06:31:08Z")

</div>

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