我们使用 neo4j 密码查询在我们的网站中进行搜索。所以,除了查询的优化之外,一切都很顺利。我们正在获得搜索结果,但并不完全符合我们的预期,可能是缺乏有关密码查询的经验和全面知识。
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
查询2: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
问题:- 由于我向您展示的是整个字符串的搜索,而不是通过分隔的字母进行的搜索,因此在图像中仍然可以看到,我们期望作为第一个结果的 vartika jain 移动到 2,但事实并非如此。
因为,当我们为 key up 处理程序工作时,搜索结果 vartika jain 会转到我们不想要的最后一个位置。
问题:- 那么,有什么方法可以优化结果,以便我们在谷歌搜索中获得最佳结果。
最佳答案
看来你应该计算匹配的数量并按其排序。
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
我删除了 [:userinteresttag]
关系和 tag
节点,因为您没有在查询中使用它。
电影图表中的示例:
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 │
└────────────┴───────┘
但实际上,您应该将他们的名字和姓氏存储在单独的属性中,对它们进行索引,并对这些索引属性使用STARTS WITH
。
关于java - NEO4j:搜索密码查询的优化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38498514/