Dense_vector element-wise operation

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

Ultimately I would like to write a search query like:

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
}

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 through getVectorValue access method and implement any function you want through a painless script.

1 Like

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