# Searching for Duplicates with CYPER match on properties

**URL:** <https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463>\
**Category:** Cypher\
**Created:** [August 14, 2020, 12:55pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463 "2020-08-14T12:55:39Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![mkretsch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mkretsch/32/5860_2.png) [@mkretsch](https://community.neo4j.com/u/mkretsch)\
**Post date:** [August 14, 2020, 12:55pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/1 "2020-08-14T12:55:39Z")

</div>

I am new to Cypher and work for a non-profit looking into financial crime. Most of our graph contains persons and entities and I want to check for duplicates. I tried the following simple query, but it returned everything

MATCH (a), (b)  
WHERE a.name = b.name

How do I match for nodes with the exact same name property? How do I match for nodes where one name is contained in the other? For example one node is Mike Green and the other Mike Green Smith (Mike Green is contained completely in Mike Green Smith).

I really appreciate any advise you have! Just getting started and learning my way around.

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [August 14, 2020, 7:32pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/2 "2020-08-14T19:32:47Z")

</div>

Hello @mkretsch and welcome to the Neo4j community 🙂

We will need an [index](https://neo4j.com/docs/cypher-manual/current/administration/indexes-for-full-text-search/) on `name` for a quick search, I suppose you are using `Person` and `Entity` as node labels:

```auto
CALL db.index.fulltext.createNodeIndex("node_name", ["Person", "Entity"], ["name"])

```

This request will collect duplicates, [thanks to a subquery](https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/index.html#subquery-correlated-aggregation), nodes for each name, the nodes which have the same name or the nodes which have a similar name:

```auto
MATCH (a)
CALL {
  WITH a.name AS name
  MATCH (b)
  WHERE name =~ '(?i)' + b.name
  WITH collect(b) AS nodes
  CALL db.index.fulltext.queryNodes("node_name", name) YIELD node
  RETURN name, collect(node) + nodes AS nodes
}
RETURN DISTINCT name, nodes

```

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![mkretsch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mkretsch/32/5860_2.png) [@mkretsch](https://community.neo4j.com/u/mkretsch)\
**Post date:** [August 17, 2020, 1:25pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/3 "2020-08-17T13:25:56Z")

</div>

Thank you Cobra for your help. Unfortunately I am getting an error when I run this in Neo4j. It appears unhappy with the curly brackets and how the "name" was defined. Any thoughts on how to avoid these errors?

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [August 17, 2020, 1:27pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/4 "2020-08-17T13:27:15Z")

</div>

Can you show the error?

---

<div class="post-metadata">

**Author:** ![mkretsch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mkretsch/32/5860_2.png) [@mkretsch](https://community.neo4j.com/u/mkretsch)\
**Post date:** [August 17, 2020, 1:39pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/5 "2020-08-17T13:39:49Z")

</div>

```auto
Invalid input '{': expected whitespace, comment, namespace of a procedure or a procedure name (line 3, column 6 (offset: 99))
"CALL {"
      ^

```

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [August 17, 2020, 1:50pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/6 "2020-08-17T13:50:15Z")

</div>

```auto
MATCH (a)
CALL {
  WITH a
  MATCH (b)
  WHERE a.name =~ '(?i)' + b.name
  WITH collect(b) AS nodes
  CALL db.index.fulltext.queryNodes("node_name", a.name) YIELD node
  RETURN a.name, collect(node) + nodes AS nodes
}
RETURN DISTINCT name, nodes

```

---

<div class="post-metadata">

**Author:** ![mkretsch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mkretsch/32/5860_2.png) [@mkretsch](https://community.neo4j.com/u/mkretsch)\
**Post date:** [August 17, 2020, 2:23pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/7 "2020-08-17T14:23:19Z")

</div>

Still getting an error, any suggestions?

```auto
Invalid input '{': expected whitespace, comment, namespace of a procedure or a procedure name (line 2, column 6 (offset: 15))
"CALL {WITH a"
      ^

```

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [August 17, 2020, 2:24pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/8 "2020-08-17T14:24:45Z")

</div>

Which version of Neo4j are you using?

---

<div class="post-metadata">

**Author:** ![mkretsch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mkretsch/32/5860_2.png) [@mkretsch](https://community.neo4j.com/u/mkretsch)\
**Post date:** [August 17, 2020, 4:03pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/9 "2020-08-17T16:03:25Z")

</div>

Neo4j Browser version: 4.0.8

Neo4j Server version: [3.5.18](https://github.com/neo4j/neo4j/wiki/Neo4j-3.5-changelog#3518) (community)

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [August 17, 2020, 4:05pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/10 "2020-08-17T16:05:25Z")

</div>

That's why it's not working, this query only works on versions of Neo4j \> 4.1 🙂

Can you upgrade or do you want another query for your current version?

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [August 17, 2020, 4:18pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/11 "2020-08-17T16:18:44Z")

</div>

This query work with all Neo4j version but it requires APOC:

```auto
MATCH (a)
WITH a.name AS name
CALL apoc.cypher.run('
    MATCH (b)
    WHERE name =~ "(?i)" + b.name
    WITH collect(b) AS nodes
    CALL db.index.fulltext.queryNodes("node_name", name) YIELD node
    RETURN name, collect(node) + nodes AS nodes
', {name:name})
YIELD value
RETURN

```

Regards,  
Cobra

---

<div class="post-metadata">

**Author:** ![mkretsch](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/mkretsch/32/5860_2.png) [@mkretsch](https://community.neo4j.com/u/mkretsch)\
**Post date:** [August 17, 2020, 6:29pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/12 "2020-08-17T18:29:41Z")

</div>

Thank you, that seems to work now! I really appreciate you reaching out to help.

---

<div class="post-metadata">

**Author:** ![Cobra](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/cobra/32/35243_2.png) [@Cobra](https://community.neo4j.com/u/Cobra)\
**Post date:** [August 17, 2020, 7:19pm UTC](https://community.neo4j.com/t/searching-for-duplicates-with-cyper-match-on-properties/23463/13 "2020-08-17T19:19:53Z")

</div>

No problem, I'm happy to hear it 🙂
