I have neo4j data as like as given below:
Here I have COUNTEY_PROVINCE relationship between country and province, PROVINCE_CITY relationship between province and city and COUNTRY_CITY relationship between country and city. When user call an api with country name, I want to return all province with city. To do this, I have run the following query:
MATCH path=(cn:Country { name: "Bangladesh" })-[:COUNTRY_PROVINCE]->(pv:Province)-[:PROVINCE_CITY]->(ct:City)
RETURN { x: nodes(path) }
And I have got the following result ( country, province, city ) :
{ x: nodes(path) }
{ "x": [ { "name": "Bangladesh"}, { "name": "Dhaka" }, { "name": "Dhaka" } ] }
{ "x": [ { "name": "Bangladesh"}, { "name": "Dhaka" }, { "name": "Narayanganj" } ] }
{ "x": [ { "name": "Bangladesh"}, { "name": "Sylhet" }, { "name": "Sylhet" } ] }
{ "x": [ { "name": "Bangladesh"}, { "name": "Khulna" }, { "name": "Khulna" } ] }
{ "x": [ { "name": "Bangladesh"}, { "name": "Khulna" }, { "name": "Jessore" } ] }
{ "x": [ { "name": "Bangladesh"}, { "name": "Chittagong" }, { "name": "Chittagong" } ] }
{ "x": [ { "name": "Bangladesh"}, { "name": "Chittagong" }, { "name": "Comilla" } ] }
Now my question is, how can I get country list with province and associate cities like :
[
{
country: {
name: "Bangladesh",
province: [
{
name: "Dhaka",
city: [
{ name: "Dhaka" },
{ name: "Narayanganj" }
]
},
{
name: "Sylhet",
city: [
{ name: "Sylhet" }
]
},
{
name: "Chittagong",
city: [
{ name: "Chittagong" },
{ name: "Comilla" }
]
},
{
name: "Khulna",
city: [
{ name: "Khulna" },
{ name: "Jessore" }
]
}
]
}
}
]
I simulated your scenario here.
Load the initial data set (Similar to the data set described in the question):
CREATE (c:Country {name:"Country A"})
CREATE (p1:Province {name:"Province A"})
CREATE (p2:Province {name:"Province B"})
CREATE (c1:City {name:"City A"})
CREATE (c2:City {name:"City B"})
CREATE (c3:City {name:"City C"})
CREATE (c4:City {name:"City D"})
CREATE (c)-[:COUNTRY_PROVINCE]->(p1)
CREATE (p1)-[:PROVINCE_CITY]->(c1)
CREATE (p1)-[:PROVINCE_CITY]->(c2)
CREATE (c)-[:COUNTRY_PROVINCE]->(p2)
CREATE (p2)-[:PROVINCE_CITY]->(c3)
CREATE (p2)-[:PROVINCE_CITY]->(c4)
The query:
MATCH (cn:Country { name: "Country A" })-[:COUNTRY_PROVINCE]->(pv:Province)-[:PROVINCE_CITY]->(ct:City)
WITH cn, pv, collect({name : ct.name}) as cities
RETURN {coutry : {name : cn.name, province : collect( distinct { name:pv.name, city :cities }) } }
The result:
{
"coutry":{
"name":"Country A",
"province":[
{
"name":"Province A",
"city":[
{
"name":"City B"
},
{
"name":"City A"
}
]
},
{
"name":"Province B",
"city":[
{
"name":"City D"
},
{
"name":"City C"
}
]
}
]
}
}
The above query uses the collect() function and the DISTINCT operator to achieve the desired format.
Well, if you're going to return the nodes of a path, that's obviously what you are going to get. You've got several options to do it differently :
Check out https://neo4j-contrib.github.io/neo4j-apoc-procedures/index32.html#_from_tojson, for example apoc.convert.toTree may get you very close to what you want.
Do it yourself. Send the query from any client application and process (+ format) the results yourself (in Java, Python, C#, ...)
...
Hope this helps.
Regards,
Tom
Related
I'm new to API Connect, and I haven't been able to find the correct mapping to pass from an array of objects to an object, evaluating its content.
I explain:
I have as input a json like this:
{
"methodCall": {
"methodName": {
"$": "ThisIsTheMethodName"
},
"params": {
"param": {
"value": {
"array": {
"data": {
"value": {
"struct": {
"member": [
{
"name": {
"$": "message"
},
"value": {
"string": {
"$": "Some text to send to client"
}
}
},
{
"name": {
"$": "phone"
},
"value": {
"string": {
"$": "9876543120124"
}
}
},
{
"name": {
"$": "date"
},
"value": {
"string": {}
}
},
{
"name": {
"$": "appid"
},
"value": {
"string": {
"$": "Application Identificator"
}
}
},
{
"name": {
"$": "costCenter"
},
"value": {
"string": {
"$": "102030"
}
}
},
{
"name": {
"$": "filled"
},
"value": {
"string": {
"$": "filledString"
}
}
}
]
}
}
}
}
}
}
}
}
}
and I need to generate this json output from the mapping:
{
"phoneNumberSMS":"983849780",
"message":"Some text to send to client",
"date": "2022-10-04T15:30:00",
"appId":"Application Identificator",
"costCenter":"102030",
"filled":"filledString" }
I have tried with the following configuration, but without success:
On the YAML
actions:
- set: output.phoneNumberSMS
foreach: input.methodCall.params.param.value.array.data.value.struct.member.value.string
from:
- input.methodCall.params.param.value.array.data.value.struct.member.name.$
- input.methodCall.params.param.value.array.data.value.struct.member.value.string.$
values: |-
var retValue1 = '';
if($(input.methodCall.params.param.value.array.data.value.struct.member.name.$) == 'phone'){
retValue1=input.methodCall.params.param.value.array.data.value.struct.member.value.string.$;
}
retValue1;
I appreciate your help !!
I solve this in two phases of mapping:
Create an array called members, where each node is of type member, which has name and value properties.
This 'members' array is the receiver of the data coming from the request.
In the second phase of the mapping, I took the output variable from the previous mapping (of type members) and assigned it to message.body.
This with the aim of getting rid of the field names with a dollar symbol ($), so the mapping will not give any error for not recognizing it.
I have a set of Author nodes. An Author node is the single parent of multiple Book nodes.
My goal: To print all Author nodes with no order and no limit, with each authors' first three books in alphabetical order.
Desired output: (let's pretend book names are a single letter)
[
{
"name" : "Leo Tolstoy",
"books": [
{ "name": "A" },
{ "name": "B" },
{ "name": "D" }
]
},
{
"name": "Charles Dickens",
"books": [
{ "name": "C" },
{ "name": "E" },
{ "name": "F" }
]
},
{
"name": "Oscar Wilde
...
]
My Problem:
I tried this:
MATCH(author:Author)
WITH author
OPTIONAL MATCH(author)-[:WROTE]->(book:Book)
WITH author, book
ORDER BY book.name
LIMIT 3
WITH author, collect(book) AS books
RETURN collect (
{
name: author.name,
books: books
}
);
But this gives:
[
{
"name" : "Leo Tolstoy",
"books": [
{ "name": "A" },
{ "name": "B" },
]
},
{
"name": "Charles Dickens",
"books": [
{ "name": "C" }
]
}
]
How could I achieve my desired output in Neo4j v3.5?
[EDITED]
This should work:
MATCH(author:Author)
OPTIONAL MATCH(author)-[:WROTE]->(book:Book)
WITH author, book.name AS bookName
ORDER BY bookName
WITH author, COLLECT({name: bookName})[..3] AS bookNames
RETURN COLLECT({name: author.name, books: bookNames}) AS result
I'm just getting started with Neo4j.
I'm trying to do a complex LOAD FROM CSV but I don't seem to be getting the relations part right.
LOAD CSV WITH HEADERS FROM "file:/Users/brestrepo/Krossover/neo4j/access_control_games.csv" AS csvLine
MERGE (game:Game
{
id: toInt(csvLine.id),
datePlayed: csvLine.date_played,
type: csvLine.type,
createdAt: csvLine.created_at,
updatedAt: csvLine.updated_at
}
)
FOREACH(ignoreMe IN CASE WHEN trim(csvLine.deleted_at) <> "" THEN [1] ELSE [] END | SET game.deletedAt = csvLine.deleted_at)
WITH csvLine, game
MATCH (team:TEAM { id: toInt(csvLine.created_by_team_id)}),(game:Game { id: toInt(csvLine.id)})
CREATE (team)-[:OWNER_TEAM { }]->(game)
WITH csvLine, game
MATCH (user:User { id: toInt(csvLine.created_by_user_id)}),(game:Game { id: toInt(csvLine.id)})
CREATE (user)-[:OWNER_USER { }]->(game)
WITH csvLine, game
MATCH (team_away:TEAM { id: toInt(csvLine.team_away_id)}),(game:Game { id: toInt(csvLine.id)})
CREATE (team_away)-[:AWAY_TEAM { }]->(game)
WITH csvLine, game
MATCH (team_home:TEAM { id: toInt(csvLine.team_home_id)}),(game:Game { id: toInt(csvLine.id)})
CREATE (team_home)-[:HOME_TEAM { }]->(game)
RETURN game
I think that the MATCH is that's causing this problem. The Match is unable to find a game.
I do get games when I do:
MATCH (game:Game) return game LIMIT 1
{
"records": [
{
"keys": [
"game"
],
"length": 1,
"_fields": [
{
"identity": {
"low": 31891,
"high": 0
},
"labels": [
"Game"
],
"properties": {
"createdAt": "2014-07-18 19:38:32",
"deletedAt": "NULL",
"id": {
"low": 285,
"high": 0
},
"type": "2",
"datePlayed": "2013-10-12 04:00:00",
"updatedAt": "2016-03-25 15:28:41"
},
"id": "31891"
}
],
"_fieldLookup": {
"game": 0
}
}
],
}
But then, when I do
MATCH (game:Game {id: 31891}) return game;
(no changes, no records)
Can anyone point me in the right direction?
Im trying to search only on the following fields:
name (product name)
vendor.username
vendor.name
categories_name
But the results is to wide, I want the results to be exactly what user is typed.
Example:
I type Cloth A I want the result to be exactly Cloth A not something else contain Cloth or A
Here is my attempt:
```
GET /products/_search
{
"query": {
"filtered": {
"query": {
"multi_match": {
"query": "cloth A",
"fields": [
"name",
"vendor.name",
"vendor.username",
"categories_name"
]
}
},
"filter": [
{
"term": {
"is_available": true
}
},
{
"term": {
"is_ready": true
}
},
{
"missing": {
"field": "deleted_at"
}
}
]
}
}
}
```
How do I do that? Thanks in advance
Put this in your multi_match
"multi_match": {
"type": "best_fields"
}
This one works:
"multi_match": {
"type": "phrase"
}
I'm migrating my elasticsearch from using facets to using aggregations, and I want to create a query where the aggregations represent all the creator names that begin with a certain letter.
I've created a nested index like so:
indexes creators, type: 'nested' do
indexes :name, type: 'string', analyzer: 'caseinsensitive', index: 'not_analyzed'
end
The following query will return all the items where a creator's name begins with a "b". Great working so far.
{
"query": {
"filtered": {
"query": {"match_all": {}},
"filter": {
"nested": {
"path": "creators",
"query": {
"prefix": {
"creators.name": {
"value": "b"
}
}
}
}
}
}
},
"aggregations": {
"creators": {
"nested": {
"path": "creators"
},
"aggs": {
"name": {
"terms": {
"field": "creators.name",
"size": 100
}
}
}
}
}
}
However, the aggregations part of the query returns ALL of the aggregations for the results, including instances creator names that do not begin with a "b." For instance, if I had an item with two creators:
"creators": [
{
"name": "Beyonce"
},
{
"name": "JayZ"
}
],
The aggregation results would include both JayZ and Beyonce. Like most people, I only want Beyonce.
Try this query and see how it goes:
{
"query": {
"filtered": {
"query": {
"match_all": {}
},
"filter": {
"nested": {
"path": "creators",
"query": {
"prefix": {
"creators.name": {
"value": "b"
}
}
}
}
}
}
},
"aggregations": {
"creators": {
"nested": {
"path": "creators"
},
"aggs": {
"NAME": {
"filter": {
"prefix": {
"creators.name": "b"
}
},
"aggs": {
"name": {
"terms": {
"field": "creators.name",
"size": 100
}
}
}
}
}
}
}
}