user3280193
user3280193

Reputation: 460

Neo4j - How to use createRelationshipIndex / createNodeIndex in full-text search

So I understand that Neo4j 3.5 and above implements full-text search in cypher query via createNodeIndex(), e.g.:

CALL db.index.fulltext.createNodeIndex("myIndex", ["PersonNode"], ["name"])

where myIndex is an arbitrary variable I make up to store the index, PersonNode is the name of my Node label, and name is one of the attributes of PersonNode where I want the full-text search performed.

And to actually perform the search by name, I can do something like the following:

CALL db.index.fulltext.queryNodes("myIndex", "Charlie")

But now assume that PersonNode has a relationship of type PURCHASED_ITEM, which is connected to another node label ProductNode as follows:

PersonNode-[:PURCHASED_ITEM]->ProductNode

And assume further that ProductNode has an attribute called productTitle indicating the display title name for each product.

My question is, I would like to set up an index for this relationship (using, presumably, createRelationshipIndex()), and perform a full-text search by productTitle and return a list of all PersonNode that purchased the given product. How can I do this?

Addendum: I understand that the above could be done by first getting a list of all ProductNode instances matching the given title, then performing a normal cypher query to extract all related PersonNode instances. I also understand that for the above example, a normal cypher query would be all that I need. But the reason I'm asking this question is that I eventually need to implement a single search bar that would allow the user to input any text, including possible misspellings and all, and have it perform a search through multiple attributes and/or relationships of PersonNode, and the results need to be sorted by some kind of relevance score. And in order to do this, I feel I need to first grasp exactly how the relationship queries work in neo4j.

Upvotes: 1

Views: 1062

Answers (2)

deepika
deepika

Reputation: 1

product title is the property of product node not the purchased item

Upvotes: 0

cybersam
cybersam

Reputation: 67044

Here is an example of how to create a full-text index for the productTitle property of PURCHASED_ITEM relationships:

CALL db.index.fulltext.createRelationshipIndex("myRelIndex", ["PURCHASED_ITEM"], ["productTitle"])

And here is a snippet showing the use of that index:

CALL db.index.fulltext.queryRelationships("myRelIndex", "Hula Hoop") YIELD relationship, score
...

Upvotes: 1

Related Questions