When creating a map from collections via apoc's apoc.map.fromLists (or plain cypher for all I care), is there any way to respect/keep the sorting? Or is it just "It's a map, sorting doesn't make sense anyway".
specifically, when executing
with [{name: "z", a: 1}, {name: "b", b: 1}, {name: "b2", b: 2}] as qs
return apoc.map.fromLists([q in qs | q.name], qs)
I get
{
"b2": {
"name": "b2",
"b": 2
},
"z": {
"name": "z",
"a": 1
},
"b": {
"name": "b",
"b": 1
}
}
where indeed I would like to have
{
"z": {
"name": "z",
"a": 1
},
"b": {
"name": "b",
"b": 1
},
"b2": {
"name": "b2",
"b": 2
}
}
Sorry, maps in Cypher are unsorted and unsortable structures, with regard to map keys.
There is apoc.maps.sortedProperties(), however this doesn't return a map, but a list of key/value pairs, where those pairs are sorted alphabetically.
Related
Does anyone have any idea how I can filter the elements of a specific list of dictionaries to add a prepended name to the keys of all elements? The field name that is the list of dictionaries is static and known ahead of time. However, the elements inside each dictionary are not. I have tried to use fluent-plugin-rewrite and fluent-plugin-record-transformer without success.
I have a record like the following. I know 'g' ahead of time, but the fact there are two entries in the dictionary and that the specific elements are 'h, i, k, and l' are dynamic.
{
"a": "d1",
"b": "d2",
"c": {
"e": "d3",
"f": "d4"
},
"g": [
{
"h": "d5",
"i": "d6",
},
{
"h": "d7",
"k": "d8",
"l": "d9",
}
]
}
}
That I would like to change to:
{
"a": "d1",
"b": "d2",
"c": {
"e": "d3",
"f": "d4"
},
"g": [
{
"prepend_h": "d5",
"prepend_i": "d6",
},
{
"prepend_h": "d7",
"prepend_k": "d8",
"prepend_l": "d9",
}
]
}
}
I want to sort hash by position, I am using sort_by but it is not sorting out, as it should
hash = {
"a": {"name": "a", "type": "text", "position": 1, "required": "false"},
"b": {"name": "b", "type": "text", "position": 4, "required": "false"},
"c": {"name": "c", "type": "text", "position": 2, "required": "false"},
"d": {"name": "d", "type": "text", "position": 3, "required": "false"}
}
to sort this I am using following command
temp = hash.sort_by { |k,v| k[0]['position'] }
There is no error but I am getting save above hash without any sorting. Even I am using temp to create new hash and but it is same. where I want to it should be sorted by position 1,2,3,4. It is part of Ruby on Rails where I am creating these fields.
sort_by is called with two arguments, k and v which refer to the entry's key and value.
Since you want to sort by position, you have to use v[:position]:
hash.sort_by { |k, v| v[:position] }
#=> [[:a, {:name=>"a", :type=>"text", :position=>1, :required=>"false"}],
# [:c, {:name=>"c", :type=>"text", :position=>2, :required=>"false"}],
# [:d, {:name=>"d", :type=>"text", :position=>3, :required=>"false"}],
# [:b, {:name=>"b", :type=>"text", :position=>4, :required=>"false"}]]
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 am still trying to create an app where the user could transform his name or a word with chemical elements (like breaking bad logo).
The user will enter a word in a text field and when he'll submit it will return him the word with the corresponding chemical symbols if they match, or it will display the "raw" letters if they don't match.
ex: If no symbol matches I am keepking the initial entry so it could be: hello => He ll O (bold char represent the existing chemical symbols)
I know this could be done in js, but the challenge is ROR (btw I don't know any js...)
In an earlier question I had just a hash like:
symbols =
{"cr" => "Cr",
"sb" => "Sb",
"ag" => "Ag",
"ar" => "Ar",
"as" => "As",
"at" => "At",
"n" => "N",
"ba" => "Ba",
"bk" => "Bk"}
and I was using name.downcase.gsub!(Regexp.union(symbols.keys), symbols)to transform the user entry. Actually I need more datas... that's why I chosen the json file.
Like on this picture i will need to use:
"number"
"small"
"molar"
(and the "name" will appear in a caption below)
I have organized a .json file with all the symbols I may need in the app and stored it in my config/periodic_table.json (pasted just a sample cause it's very long).
1°) If a user enter "hello" how do I loop to search for the "he" hash and print the "name","number", "small" and the "molar"
2°) I will use the json as a database (I will use heroku to deploy) so do I have anything to transform for using json and pg together?
[ "symbols"
{
"h": {
"name": "Hydrogen",
"number": 1,
"small": "H",
"molar": 1.00794
},
"he": {
"name": "Helium",
"number": 2,
"small": "He",
"molar": 4.002602
},
"b": {
"name": "Boron",
"number": 5,
"small": "B",
"molar": 10.811
},
"c": {
"name": "Carbon",
"number": 6,
"small": "C",
"molar": 12.0107
},
"n": {
"name": "Nitrogen",
"number": 7,
"small": "N",
"molar": 14.0067
}
}
]
I will need to loop first with the symbols that contain 3 chars, then 2 then 1... shall i change anythin in the json, like an harray for the hashes that contains 3 chars, another for 2 chars , and for 1char?
Correct Json Format
[{
"symbols":{
"h":{
"name": "Hydrogen",
"number": 1,
"small": "H",
"molar": 1.0079
},
"he":{
"name": "Helium",
"number": 2,
"small": "He",
"molar": 4.002602
},
"b": {
"name": "Boron",
"number": 5,
"small": "B",
"molar": 10.811
},
"c": {
"name": "Carbon",
"number": 6,
"small": "C",
"molar": 12.0107
},
"n": {
"name": "Nitrogen",
"number": 7,
"small": "N",
"molar": 14.0067
}
}
}]
I am trying to get the relationship type of a very simple Cypher query, like the following
MATCH (n)-[r]-(m) RETURN n, r, m;
Unfortunately this return an empty object for r. This is troublesome since I can't distinguish between the different types of relationships. I can monkey patch this by adding a property like [r:KNOWS {type:'KNOWS'}] but I am wondering if there isn't a direct way to get the relationship type.
I even followed the official Neo4J tutorial (as described below), demonstrating the problem.
Graph Setup:
create (_0 {`age`:55, `happy`:"Yes!", `name`:"A"})
create (_1 {`name`:"B"})
create _0-[:`KNOWS`]->_1
create _0-[:`BLOCKS`]->_1
Query:
MATCH p=(a { name: "A" })-[r]->(b)
RETURN *
JSON RESPONSE BODY:
{
"results": [
{
"columns": [
"a",
"b",
"p",
"r"
],
"data": [
{
"row": [
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{
"name": "B"
},
[
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{},
{
"name": "B"
}
],
{}
]
},
{
"row": [
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{
"name": "B"
},
[
{
"name": "A",
"age": 55,
"happy": "Yes!"
},
{},
{
"name": "B"
}
],
{}
]
}
]
}
],
"errors": []
}
As you can see, I get an empty object for r, which makes it impossible to distinguish between the relationships.
NOTE: I am running Neo4J v.2.2.2
Use the type() function.
MATCH (n)-[r]-(m) RETURN type(r);
Added distinct.
MATCH (n)-[r]-(m) RETURN distinct type(r);