Best practice to match nodes [closed] - performance

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I'm doing some tests on redisgraph and I'm wondering what is the best practice to match nodes if I have a hierarchy like this:
category => post => comment => reply
I have index on each label _id field and I match according to the _id
First approach: by matching all the way to the target node:
GRAPH.QUERY test "MATCH (:category {_id:1})-[:post]->(:post {_id:1})-[comment_rel_1:comment]->(c1:comment {_id:1}) SET c1.comment = 'changed'"
Second approach: matching the node directly
GRAPH.QUERY test "MATCH (c1:comment {_id:1}) SET c1.comment = 'changed'"
In case of a huge database with a lot of nodes and edges what approach considered to be time efficient?
Thanks

Here are the execution plans for each of the queries
127.0.0.1:6379> graph.explain test "MATCH (:category {_id:1})-[:post]->(:post {_id:1})-[comment_rel_1:comment]->(c1:comment {_id:1}) SET c1.comment = 'changed'"
1) "Update"
2) " Filter"
3) " Conditional Traverse | (anon_2:post)->(c1:comment)"
4) " Filter"
5) " Conditional Traverse | (anon_0:category)->(anon_2:post)"
6) " Index Scan | (anon_0:category)"
127.0.0.1:6379> graph.explain test "MATCH (c1:comment {_id:1}) SET c1.comment = 'changed'"
1) "Update"
2) " Index Scan | (c1:comment)"
In case that you just want to update the comment node, regardless to its state in the graph, the second query is more efficient. In case you want to update only the comment node that is connected in the pattern that you described, the first query is the way to go, between the two.

Related

using for-loop to remove maximum and minimum values from a dictionary [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
how can I use for loop to delete maximum and minimum values from a dictionary. for instance if you have a dictionary grades = {sam: [23,43,55]}, peter: [66,55,44], sarah: [99,55,77]}. how can I remove only the maximum and minimum values?
im new to coding and cannot figure it out. Language is python
Hi you can try something like this:
# Create dictionary
dic = {'sam': [23,43,55], 'peter': [66,55,44], 'sarah': [99,55,77]}
# Use for loop to iterate
for key, value in dic.items():
# Find maximum value and it's index
max_value = max(value)
max_index = value.index(max_value)
# Delete maximum value
del dic[key][max_index]
# Find minimum value and it's index
min_value = min(value)
min_index = value.index(min_value)
# Delete miniumum value
del dic[key][min_index]
# Print output
print(dic)
Let me know if you don't understand anything. Cheers

Elegant/Best solution to get the last element from an iteration [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I'm receiving multiple messages over a channel, and after iterating over them, I would like to keep the last element for further usage. My first (probably bad!) approach was to declare some variable, and then assign it every loop.
let last = 0;
for some in rx_from_channel.iter() {
let last = some;
}
let a = last + 5;
I really don't like this solution - is there a to avoid assigning last in each loop?
Further, I would have expected that after using let last inside the for {} loop for the first time, the variable declared above the loop goes out of scope - and ļast shouldn't be available after the for {} loop at all. The compiler suggests otherwise - why?
You can just do:
let last = rx_from_channel.iter().last().unwrap_or_else(|| &0);
let a = last + 5;
See last()
fn last(self) -> Option<Self::Item>
Consumes the iterator, returning the last element.
Doesn't method last() solve your problem?

Matching partial strings in a phrase [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I put up an intranet site that loops through a .csv dump of our customer database and uses a form to help look up account numbers.
I want to treat all of my keywords as wild card terms, but respect their order. For example, if I have company A: "The Monogramme Shoppe" and company B: "Monograms & More at The Shop", I want to return A and B options if I type "mono shop" in the form field. This code does that:
company_lookup = company_lookup.split(" ")
counter = company_lookup.length
company_lookup.each do |com|
if company.downcase.include? com.downcase
counter = counter - 1
end
end
if counter == 0
match_found = true
account_number = row[2].to_s
matches.push [account_number, company]
end
But if I type "mono the", I also get both results. There, I only want to get the B result.
Is there any way to use regular expressions to, say look for PartialKeyword1 and PartialKeyword2 in a string and return true if matched?
You can use the following code to construct a regular expression to match the company name, and then use this regular expression to find the matched record.
company_lookup = company_lookup.split(" ").map{|r| Regexp.quote(r)}.join('.*?')
if company =~ /#{company_lookup}/i
matches.push [row[2].to_s, company]
end
If performance is a big concern, or the data size is huge, you'd better try some full text search engine, such as Thinking Sphinx

ruby match two strings based 4 different criteria [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I'm creating a quick search rails app feature. Any help with how to structure the following matching specifications would be appreciated.
Basically, when a user enters a name to search, they should get results based on the following matching criteria.
accept match on first 3 chars (e.g. Jon for Jones)
reject match on less than 3 chars (e.g. Jo for Jones)
accept exact match for 2 char author name (e.g. Li for Li)
reject exact match on 1 char author name
reject mismatch on chars beyond 3 (e.g. reject Jonis for Jones)
Can this be done with a regular expression?
matchto = 'Jones'.downcase
input = 'Jon'.downcase
matchto.start_with?(input) && 1 < input.length &&
( input.length == matchto.length || 2 < input.length )

Linq to read text file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
I have a text file with contents as:
...
*..
*..
...
The vehicle used are:
*Car(Marathi, Nissan, Toyota)..
*Bikes(Yamaha, Hero Honda)
*...
*...
*...so on..
The items used are...
*..
*..
Now I need to search for the key word "vehicle" and put the options Car(Marathi, Nissan, Toyota)..), Bikes(Yamaha, hero honda), etc into a list.
i.e. Everything that comes after "*" in a line, must be an item of that list.
Linq must be used or any other means where loops are not allowed.
The desired result is not really clear.
If you need a List<string>, containing
"Car(Marathi, Nissan, Toyota).."
"Bikes(Yamaha, Hero Honda)"
"..."
"..."
"...so on..
you could do
var result = File.ReadAllLines(#"<pathToYourFile>")
//skip lines without "vehicle"
.SkipWhile(m => !m.Contains("vehicle"))
//skip the line with "vehicle"
.Skip(1)
//take the following lines starting with an "*"
.TakeWhile(m => m.StartsWith("*"))
//remove the "*"
.Select(m => m.Replace("*", string.Empty))
//enumerate to get a List<string>
.ToList();

Resources