Passing parameter into Cypher query node_auto_index - neo4j

I'm trying to pass a parameter into the Cypher query node_auto_index without any luck.
DecisionRepository.class:
#Query("START d=node:node_auto_index(':text') MATCH (d:Decision) RETURN d")
List<Decision> searchDecisions(String text);
Usage:
List<Decision> searchDecisions = decisionRepository.searchDecisions("name:aDbma~ OR name:mosyl~");
Is it possible and if so where I'm wrong ?

Use query parameters
START d=node:node_auto_index({myLuceneQuery}) RETURN d;
Then send "name:aDbma~ OR name:mosyl~" as {myLuceneQuery}

Related

Cypher query with does not return anything if second part does not work

I have a query which supposes to join two results and give the union as output.
MATCH (:Some )-[r*0..1]-> (p:Address) WITH collect(p) AS rows1
MATCH (:Some2)-[r*0..1]-> (pp:Address)
WITH rows1+collect(pp) AS rows2
UNWIND rows2 AS row RETURN row
As you can see the selection has two parts. So it works fine if there is matching data for both queries but if the second part of the match does not return anything then it returns empty. Meaning MATCH (:Some2)-[r*0..1]-> (pp:Address) returns empty then the whole union fails and returns null even if MATCH (:Some )-[r*0..1]-> (p:Address) return values .
How to fix this? Is it a bug in neo4j?
You have not actually asked a question (you have indeed just described the expected behaviour for pattern matching ... namely that if there is no match there is no result) ... but I assume you want a solution ?
MATCH (:Some )-[r*0..1]-> (p:Address)
RETURN p
UNION
MATCH (:Some2 )-[r*0..1]-> (p:Address)
RETURN p
should do the trick. Note that the only thing that matters is that the variables returned are exactly the same (what they contain doesn't actually matter).
Also ... you might want to have a look at what OPTIONAL MATCH does ...
Hope this helps.
Regards,
Tom
Update
CALL apoc.cypher.run("
MATCH (:Some )-[r*0..1]-> (p:Address)
RETURN p
UNION
MATCH (:Some2 )-[r*0..1]-> (p:Address)
RETURN p",{}) YIELD value
RETURN value.p AS result
ORDER BY result;
This simple query should work:
MATCH (s)-[*0..1]->(p:Address)
WHERE s:Some OR s:Some2
RETURN p;
Thanks for your inputs I used following version
MATCH (p:Address)
WHERE exist ((:Some )-[r*0..1]-> (p)) OR ((:Some2 )-[r*0..1]-> (p))
RETURN p;

Neo4j Cypher chain output from match query to APOC

I wanted to make use of the Neo4j Cypher apoc.index.search procedure. I am currently using the Neo4j CE 3.1.0. I have successfully set up and tested the procedure as a stand-alone query such as:
call apoc.index.search("Contact2", "Contact2.FirstName:monica~")
Now, I want to do a MATCH query first to fetch a set of nodes, and spool the FirstNames found into calling this APOC procedure on Contact2 node to see if I can find any similar in the Contact2 node and the corresponding weight.
Is that possible? I have tried several iterations using the WITH keyword but to no avail. Thanks.
You can add each firstName value as separate fuzzy search terms to the query string passed to apoc.index.search. For example:
MATCH (f:Foo)
WITH REDUCE(
s = 'Contact2.FirstName:', n IN COLLECT(DISTINCT f.firstName) |
s + n + '~ '
) AS query
CALL apoc.index.search("Contact2", query)
...
#cybersam thank you for your answer. I did not try yours but I also just solved this on my own (after days of trying). My syntax is as follows:
// Contact1 is my source table. Contact2 is my destination table
MATCH (a:Contact1) WITH a.FirstName AS A
CALL apoc.index.search("Contact2", "Contact2.FirstName:" + A + "~")
YIELD node, weight RETURN *
I am not sure if that is the best way but it worked for me

Is it possible cypher dynamic label at #Query?

I'm using SDN 3.1.0.RELEASE.
And i trying danamic label query like
#Query("MATCH (n:{0}) RETURN n")
public List<SimpleArticle> findAllByDomain(String domain);
and give String parameter like SimpleArticle.class.getSimpleName()
when i launched test code, i met "SyntaxException At {0}"
so i change query to
#Query("MATCH (n:`{0}`) RETURN n")
this code is work but don't find Domain node.
result log is "Executing remote cypher query: MATCH (n:`{0}`) RETURN n params {0=SimpleArticle}"
so i run this cypher query in query broswer
MATCH (n:`SimpleArticle`) RETURN n ;
It works and find node.
Can i use dynamic labels in #Query ?
Labels cannot be parameterized. The rationale for this is that different labels might result in different query plans. A parameterized query is always using the same query plan - therefore it's not possible.
The only way to use "semi"-dynamic labels is by using string concatenation or by Cypher DSL.

Identifying parameters of Cypher query

Is there an API for identifying the named parameters of a given Cypher query? When taking the following query as example:
MATCH (n) WHERE n.firstName = { name } AND n.LastName = { lastName } RETURN n
Then this API should return "name" and "lastName".
Does Neo4j provide such an API or would I have to manually parse the query string to identify any parameters it contains?
There is no API for obtaining the names of the properties used in a Cypher query. There is generally no need for such a utility, since the code making the query should already know that information.
Maybe you can begin your query with parameters in a transaction, get the result/errors and rollback it ?
http://docs.neo4j.org/chunked/stable/rest-api-transactional.html

Running a case-insensitive cypher query

Is it possible to run a case-insensitive cypher query on neo4j?
Try that: http://console.neo4j.org/
When I type into this:
start n=node(*)
match n-[]->m
where (m.name="Neo")
return m
it returns one row. But when I type into this:
start n=node(*)
match n-[]->m
where (m.name="neo")
return m
it does not return anything; because the name is saved as "Neo". Is there a simple way to run case-insensitive queries?
Yes, by using case insensitive regular expressions:
WHERE m.name =~ '(?i)neo'
https://neo4j.com/docs/cypher-manual/current/clauses/where/#case-insensitive-regular-expressions
Another way would be:
WHERE LOWER(m.Name) = LOWER("Neo")
And if you are using the Neo4j Client (.NET):
Client.Cypher.Match("(m:Entity)")
.Where("LOWER(m.Name) = LOWER({name})")
.WithParam("name", inputName)
.Return(m => m.As<Entity>())
.Results
.FirstOrDefault();
If anybody is looking on how to do this with a parameter, I managed to do it like this.
query = "{}{}{}".format('Match (n) WHERE n.pageName =~ "'"(?i)", name, '" RETURN n')
and "name" is the variable or your parameter
You can pass a parameter to the case insensitive regular expression like:
WHERE m.name =~'(?i)({param})

Resources