# Dense\_vector element-wise operation

**URL:** <https://discuss.elastic.co/t/dense-vector-element-wise-operation/270181>\
**Category:** Elasticsearch\
**Created:** [April 15, 2021, 5:18am UTC](https://discuss.elastic.co/t/dense-vector-element-wise-operation/270181 "2021-04-15T05:18:42Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruodingt\_trendii](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ruodingt_trendii/32/77793_2.png) [@ruodingt\_trendii](https://discuss.elastic.co/u/ruodingt_trendii)\
**Post date:** [April 15, 2021, 5:18am UTC](https://discuss.elastic.co/t/dense-vector-element-wise-operation/270181/1 "2021-04-15T05:18:42Z")

</div>

Hi mates,

I just encountered an issue with `dense_vector`

I am trying to implement an additive\_chi2\_kernel as my score function.

additive\_chi2\_kernel is a way to compare similarity of two histogram

The function is as simple as `k(x, y) = -Sum [(x - y)^2 / (x + y)]`

However to achieve this it involves some element-wise operation like (x-y) and (x+y)

[https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.additive\_chi2\_kernel.html](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.additive_chi2_kernel.html)

Ultimately I would like to write a search query like:

```auto
POST /axi-dev-3/_search

{
"query": {
"script_score": {
"query": {"match_all":{}},
"script": {
"source": "addtiveChiSquare(params.hist,doc['hist']) + 1.0",
"params": {"hist":[
0.00042724609375,
0,
0,
0,
0.000274658203125,
0.0050048828125,
0.03057861328125,
0,
0,
0,
0.0067596435546875,
0.0162506103515625,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0.0398406982421875,
0.0000152587890625,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0.000091552734375,
0,
0.4103546142578125,
0.4169921875,
0.002655029296875,
0.0059967041015625,
0.0460052490234375,
0.0187530517578125
]}
}
}
},
"size": 20
}

```

---

<div class="post-metadata">

**Author:** ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)\
**Post date:** [April 22, 2021, 7:40pm UTC](https://discuss.elastic.co/t/dense-vector-element-wise-operation/270181/2 "2021-04-22T19:40:17Z")

</div>

Up to v7.12, dense\_vector fields can be accessed only through dedicated functions (which `additive_chi2_kernel` is not one of them), so you can't do this . But from v. 7.13, you will be able to access vector values [directly](https://github.com/elastic/elasticsearch/pull/71313) through `getVectorValue ` access method and implement any function you want through a painless script.

---

<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 20, 2021, 7:40pm UTC](https://discuss.elastic.co/t/dense-vector-element-wise-operation/270181/3 "2021-05-20T19:40:40Z")

</div>

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