# Cosine-Distance of reduced dense vectors

**URL:** <https://discuss.elastic.co/t/cosine-distance-of-reduced-dense-vectors/255114>\
**Category:** Elasticsearch\
**Created:** [November 11, 2020, 6:40pm UTC](https://discuss.elastic.co/t/cosine-distance-of-reduced-dense-vectors/255114 "2020-11-11T18:40:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![christiana](https://avatars.discourse-cdn.com/v4/letter/c/d9b06d/32.png) [@christiana](https://discuss.elastic.co/u/christiana)\
**Post date:** [November 11, 2020, 6:40pm UTC](https://discuss.elastic.co/t/cosine-distance-of-reduced-dense-vectors/255114/1 "2020-11-11T18:40:40Z")

</div>

Hi,

i have set up a database containing documents which are having dense vectors for the features and for a binary [1,0] mask.  
I would like to use the script query to the database with a feature vector at runtime. For this i'm using cosine-similarity, but till now only between the entire feature vectors. But when querying i want to filter the features with a mask vector to get only a subset of entries. Then I want to calculate the cosine-similarity.

My db mapping looks like:

```auto
mapping = {
        "mappings": {
            "properties": {
                   "imageid": {
                            "type": "text"
                    },
                     "score": {
                            "type": "float"
                     },
                      'feature': {
                              "type": "dense_vector",
                              "dims": dim
                       }
                      'maskvec': {
                              "type": "dense_vector",
                              "dims": dim
                       }
            }               
        }
    }

```

The script score query till now looks like this:

```auto
"script_score": {
                            "query": {
                                "match_all": {}
                            },
                            "script": {
                                "lang":"painless",
                                "source": """
                                return cosineSimilarity(params.queryVector, feature) + 1.0
                                """,
                                "params": {
                                    "queryVector": list(featurevector)
                                }    
                            }
                        }

```

I would like to do sth like this:  
mask = params.querymask and maskvec  
queryvec = params.queryVector[mask]  
featurevec = feature[mask]  
return cosineSimilarity(queryvec, featurevec) + 1.0

Is it possible to implement this with elastic search?  
Cheers,  
Christian

---

<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:** [December 9, 2020, 6:40pm UTC](https://discuss.elastic.co/t/cosine-distance-of-reduced-dense-vectors/255114/2 "2020-12-09T18:40:46Z")

</div>

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