Reputation:
We are using neo4j cypher queries for searching in our website. So, long everythng is going fine except the optimization of queries. We are getting search results but not exactly what we are expecting may be lack of experience and full knowledge about cypher queries.
In a textbox the search string is been send to the query on key up handler means on entering each letter its going to execute query. Like for eg. v then a like this and untill we enter space it will treat it as one string and the result will be shown accordingly, but the issue is as we enter space and start writing letters and then again will form a string the result fluctates badly.
EXAMPLE:
QUERY 1 :
MATCH (n:user)<-[:userinteresttag]-(tag) where ANY(m in split(n.username," ") where m STARTS WITH 'vartika' ) RETURN distinct n.username
QUERY2: MATCH (n:user)<-[:userinteresttag]-(tag) where
ANY(m in split(n.username," ") where m STARTS WITH 'vartika' or m STARTS WITH 'jain')
RETURN distinct n.username order by n.username
ISSUE:- Since I am showing you the search through full string not by separated letters, still as it can be visible in the images that vartika jain which we expect to come as first result move to 2 which should not be the case.
As, when we work for key up handler then search result vartika jain goes to last position which is not we wanted.
QUESTION:- SO, is there any way to optimize the result, so that we can get the best results as we get in google search.
Upvotes: 0
Views: 118
Reputation: 7790
It seems like you should count the number of matches and order by that.
MATCH (n:user)
WITH n, size([m in split(n.username, ' ') WHERE m STARTS WITH 'vartika' OR m STARTS WITH 'jain']) AS matches
RETURN n.username
ORDER BY matches DESC
I removed the [:userinteresttag]
relationship and tag
node since you aren't using it in your query.
Example from the movie graph:
MATCH (p:Person)
WITH p, size([x IN split(p.name, ' ') WHERE x STARTS WITH 'Tom' OR x STARTS WITH 'Hanks']) AS matches
RETURN p.name, matches
ORDER BY matches DESC
LIMIT 5
╒════════════╤═══════╕
│p.name │matches│
╞════════════╪═══════╡
│Tom Hanks │2 │
├────────────┼───────┤
│Tom Cruise │1 │
├────────────┼───────┤
│Tom Skerritt│1 │
├────────────┼───────┤
│Tom Tykwer │1 │
├────────────┼───────┤
│Keanu Reeves│0 │
└────────────┴───────┘
But really you should store their first and last name in separate properties, index them, and use STARTS WITH
on those indexed properties.
Upvotes: 3