alexklibisz / elastiknn

Elasticsearch plugin for nearest neighbor search. Store vectors and run similarity search using exact and approximate algorithms.

Home Page:https://alexklibisz.github.io/elastiknn

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for elastiknn_dense_float_vector in script_score

venkatana-kore opened this issue · comments

I'm using elastiknn_dense_float_vector as a vector for a field to achieve ANN search. But I want to use the elastiknn_dense_float_vector in script_score to modify the score which is not working as I'm getting "Fielddata is not supported on field [knn_vectors] of type [elastiknn_dense_float_vector]" as an error.

Any possible way to convert elastiknn_dense_float_vector to dense_vector. If not how can we use elastiknn_dense_float_vector in script_score query.

This is the query I'm using right now and Im using ElaskiKNN 7.9.2 version

{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "multi_match": {
                                "query": "statement",
                                "fields": [ ]
                            }
                        },
                        {
                            "elastiknn_nearest_neighbors": {
                                "field": "page_title_vectors_knn",
                                "vec": {
                                    "values": []
                                },
                                "model": "lsh",
                                "similarity": "l2",
                                "candidates": 10
                            }
                        }
                    ]
                }
            },
            "functions": [
                {
                    "script_score": {
                        "script": {
                            "source": "double cosVal = Math.acos(cosineSimilarity(params.query_vector, doc["page_title_vectors_knn"])); return 1-(cosVal/3.14);",
                            "params": {
                                "query_vector": []
                            }
                        }
                    }
                }
            ],
            "boost_mode": "multiply",
            "score_mode": "multiply",
            "boost": 1.0
        }
    }
}

Hi, thanks for the question. This is currently not supported. I know that it's possible to implement, and I actually did it on a branch at some point in the distant past, but it was never merged. I'm not sure when I'd be able to look into it. If you would be interested in trying it out, I would suggest looking at how the dense_vector type in Elasticsearch is integrated to work with scripts. It would probably work very similarly in Elastiknn.

Thanks @alexklibisz for your reply. It would be great if you can give me some inputs in this direction or any resources or pointers on how can I use knn vectors in scripts.

They can't be used at all right now. There's no support for it. If you want to try to implement it yourself, see the developer-guide.md and the dense_vector type implementation in Elasticsearch. Otherwise, I'll try to get to it at some point, but can't prioritize it right now.