简体   繁体   English

通过密码查询在neo4j数据库中搜索关键字

[英]Search keywords in neo4j database by cypher query

在此处输入图片说明 I'm working on a website and implementing search filter functionality.I am fetching search result from data base which is neo4j. 我正在网站上并实现搜索过滤器功能。我正在从neo4j数据库中获取搜索结果。 I'm facing an problem in applying filters.The problem is when add for search let say Bangalore I get 10 result then I add another keyword let developer I get the result from the whole database but what i want it should be come from only previous selected result or Keyword that is Bangalore 我在应用过滤器时遇到问题。问题是当添加搜索时,例如说班加罗尔,我得到10个结果,然后我添加另一个关键字,让开发人员我从整个数据库中得到结果,但是我想要的结果应该仅来自先前选择的结果或关键字是班加罗尔

My query is: 我的查询是:

        $data = array (
           "query" => "MATCH (x :Job)-[r :POSTED_JOB]-(m) where ( x.city IN {data1} or x.categories or x.sectors IN {data4} or x.role IN {data5} or x.requirement IN {data6} 
            or x.title IN {data7} or x.description IN {data8})
            RETURN m.companyName,x.city",
       "params" => array(
                "data1" => $city,
                "data4" => $sector,
                "data5" => $skills,
                "data6" => $search,
                "data7" => $search,
                "data8" => $search

) ); ));

How can I write this query to refine the result. 如何编写此查询以优化结果。

Your query is full of OR s which means that a Job will match when any of the (that array contains two data5 properties). 您的查询充满了OR ,这意味着当任何一个(该数组包含两个data5属性)时, Job都会匹配。 If you want to match all of the provided criteria then swap the OR s for AND s. 如果要匹配所有提供的条件,则将OR替换为AND Depending on the data you are providing you may need to be careful of NULL values and empty lists. 根据您提供的数据,您可能需要注意NULL值和空列表。

EDIT - example code 编辑-示例代码

This is the small section of your query that I think probably matches the fields you are talking about. 我认为这是您查询的一小部分,可能与您所讨论的字段匹配。

WHERE x.city IN {data1} OR x.role IN {data5}

It says match me any Node (x) WHERE x is in Bangalore OR WHERE x is for a developer role. 它说可以与我在班加罗尔(X)位于班加罗尔(X)的任何节点(x) 为开发人员角色(X)匹配。 If you want your query to match only records which are both in Bangalore and for a Developer then that section of query should be: 如果您希望查询仅匹配班加罗尔和开发人员中的记录,则查询的该部分应为:

WHERE x.city IN {data1} AND x.role IN {data5}

x.categories seems a little stranded too (you're not querying anything but its existence). x.categories似乎也有些搁浅(除了其存在之外,您没有查询任何东西)。

EDIT - Post Chat 编辑-发布聊天

The requirement is to perform successive queries where each query acts as a filter on the previous result. 要求是执行连续查询,其中每个查询都充当先前结果的过滤器。 All fields are re-matched with each call. 所有字段均与每个呼叫重新匹配。 To achieve this you need to have an external node identifier that you can use to chain queries and then pass the set of identifiers into each successive query. 为此,您需要具有一个外部节点标识符,该标识符可用于链接查询,然后将标识符集传递到每个后续查询中。 A reduced example in Cypher: Cypher中的简化示例:

Initial Query: 初始查询:

MATCH (j:Job)<-[:POSTED_JOB]-(m)
WHERE j.title=~{searchString} OR j.description=~{searchString}
RETURN j.jobId, j.title, j.description, j.city, m.companyName

Subsequent Queries: 后续查询:

MATCH (j:Job)<-[:POSTED_JOB]-(m)
WHERE j.jobId IN {collectionOfJobIds}
AND (j.title=~{searchString} OR j.description=~{searchString})
RETURN j.jobId, j.title, j.description, j.city, m.companyName

EDIT - Because it is bugging me 编辑-因为它困扰着我

You might want to rematch your data on each query, so you can employ a filter: 您可能希望重新匹配每个查询的数据,因此可以使用过滤器:

MATCH (j:Job)<-[:POSTED_JOB]-(m)
WHERE j.title=~({searchStrings}[0]) OR j.description=~({searchStrings}[0])
WITH m, COLLECT(j) as jobs
WITH m, FILTER (j IN jobs WHERE j.title=~({searchStrings}[1]) OR j.description=~({searchStrings}[1]) as filteredJobs
RETURN m, jobs

This requires passing in the searchStrings as an array, and you can add as many WITHs as you like (just remember to increment your array index). 这要求将searchStrings作为数组传入,并且您可以根据需要添加任意数量的WITH(只记得增加数组索引)。 It is important to realise that with all of these queries, if you have very many Jobs, they are going to be slow as you are begin by matching all the Jobs in your system. 重要的是要意识到,对于所有这些查询,如果您有很多Job,那么在匹配系统中的所有Job时,它们将变得很慢。 If you can put some restriction on the initial Match your life will be better! 如果您可以对初始比赛设置一些限制,那么您的生活会更好!

ASIDE: Your model is not really a graph though where you would possibly model City, Sector, Skills, etc as Nodes rather than properties. 旁白:虽然您可以将“城市”,“部门”,“技能”等建模为节点而不是属性,但您的模型并不是真正的图形。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM