# Algo cosine similarity error

**URL:** <https://community.neo4j.com/t/algo-cosine-similarity-error/25127>\
**Category:** Cypher\
**Tags:** apoc, cypher, operations, knowledge-base\
**Created:** [September 11, 2020, 9:12am UTC](https://community.neo4j.com/t/algo-cosine-similarity-error/25127 "2020-09-11T09:12:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jai](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@Jai](https://community.neo4j.com/u/Jai)\
**Post date:** [September 11, 2020, 9:12am UTC](https://community.neo4j.com/t/algo-cosine-similarity-error/25127/1 "2020-09-11T09:12:52Z")

</div>

Hi all,

I am new to cypher and neo4j. We have a neo4j version 3.5.14 . We have articles as node and it's meta data as properties. I am trying to apply some graph algorithms on the nodes to create similar link between articles based on cosine similarity between articles embeddings property. I am able to do it on browser but through cypher query i am having issues. Here is my query -:

```auto
           session.run(
                    """
                    MATCH (a:article)-[r_0:has]-(k:keyword)-[q:has]-(b:article)
                    WHERE a._id = {article_id}
                    AND b.published > {window_left}
                    AND b.published < {window_right}
                    AND b.embeddings IS NOT NULL"
                    WITH a,b 
                    algo.similarity.cosine(a.embeddings,b.embeddings) as similarity
                    WHERE similarity > 0.8
                    MERGE (a)-[r:similar]-(b)
                     """ % (article['_id'],similarity_threshold,window_left,window_right)
)

```

We have keywords as nodes as well, since data volume is above 100k, I have the first article id and I am trying to first match articles on common keyword and then compare their embeddings. If the embeddings are above 0.8, i create a similar link between those and store their weights.  
And is there is any better way to do it? I am able to run this in browser client but not through neo bolt from python

Versions -:  
Neo4j -3.5.14-enterprise  
neobolt - 1.7.4

---

<div class="post-metadata">

**Author:** ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)\
**Post date:** [September 11, 2020, 4:59pm UTC](https://community.neo4j.com/t/algo-cosine-similarity-error/25127/2 "2020-09-11T16:59:07Z")

</div>

But, what is the error ?

---

<div class="post-metadata">

**Author:** ![Jai](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@Jai](https://community.neo4j.com/u/Jai)\
**Post date:** [September 14, 2020, 5:07am UTC](https://community.neo4j.com/t/algo-cosine-similarity-error/25127/3 "2020-09-14T05:07:00Z")

</div>

Hi gabriel,

this is the error

 ![Screenshot 2020-09-14 at 10.34.58 AM](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/2X/4/4876d0e5d16b6b52acf1a1fb2dd6bdf936397839.png)  
Any help to resolve this would be appreciated. Thanks

---

<div class="post-metadata">

**Author:** ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)\
**Post date:** [September 14, 2020, 7:35am UTC](https://community.neo4j.com/t/algo-cosine-similarity-error/25127/4 "2020-09-14T07:35:15Z")

</div>

> [@Jai](#):
>
> ```auto
> WITH a,b 
> algo
> 
> ```

I think you are missing a comma (",") after "WITH a,b", meaning "WITH a,b,algo..."

---

<div class="post-metadata">

**Author:** ![Jai](https://avatars.discourse-cdn.com/v4/letter/j/5f9b8f/32.png) [@Jai](https://community.neo4j.com/u/Jai)\
**Post date:** [September 14, 2020, 10:53am UTC](https://community.neo4j.com/t/algo-cosine-similarity-error/25127/5 "2020-09-14T10:53:48Z")

</div>

Hi gabriel, thank you. Kinda embarrassed to be honest. 🙂  
Is there any way to optimize the query? I see, as my data increases, it is getting slower. Would using cosine.stream would make a difference in speed?  
Thank you

---

<div class="post-metadata">

**Author:** ![gabriel\_toma](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/gabriel_toma/32/722_2.png) [@gabriel\_toma](https://community.neo4j.com/u/gabriel_toma)\
**Post date:** [September 14, 2020, 11:49am UTC](https://community.neo4j.com/t/algo-cosine-similarity-error/25127/6 "2020-09-14T11:49:23Z")

</div>

It's always better to think about ways to optimise your graph, as to re-use already done calculatios, but, have you tried runnig your query in parallel ?

Do take a look here, it's an interesting discussion: [How best to do parallel processing](https://community.neo4j.com/t/how-best-to-do-parallel-processing/13342)
