Combining scores from multiple Neo4j queries - database

We have a movie graph. There are actors, directors, and movies. It's possible that a director has also played in another movie, or even the same movie. We give an actor a score for each movie he plays in. We also give a director a score for each of his movies. Now, we are looking to get a list of individuals and their scores, sorted from highest to lowest score. So these are the queries we currently have:
MATCH (p:Person)-[idr:IsDirectorOf]->(m:Movie)
RETURN p.name AS name, COUNT(idr) AS numberOfMoviesDirected
ORDER BY numberOfMoviesDirected DESC;
and
MATCH (p:Person)-[iai:IsActorIn]->(m:Movie)
RETURN p.name AS name, COUNT(iai) AS numberOfMoviesPlayed
ORDER BY numberOfMoviesPlayed DESC;
I want to combine these queries, to get the person who has the highest score from both queries together at the top. Also, please note that we may later need to add another query to the mix, so solutions that work for only two queries may not be the best.

You should try this query :
MATCH (p:Person)
RETURN p, size((p:Person)-[:IsDirectorOf]->(:Movie)) + size((p:Person)-[:IsActorIn]->(:Movie)) AS score
ORDER BY score DESC
And if you want to add additional score, just continue your query with a WITH.
Cheers
UPDATE
To respond to your comment, this is the query :
MATCH (p:Person)
WITH
max(size((p)-[:IsActorIn]->())) As maxActed,
max(size((p)-[:IsDirectorOf]->())) AS maxDirected
MATCH (p:Person)
RETURN
p,
size((p)-[:IsActorIn]->(:Movie)) / toFloat(maxActed) + size((p)-[:IsDirectorOf]->(:Movie)) / toFloat(maxDirected) AS score
ORDER BY score DESC

Related

Will duplicated documents impact search results?

Will duplicated documents impact search results?
For example, we have an index that we can have the same documents repeated and different by only one field.
Index: ChannelID, ProductID, ProductName and ProductDescription
We may have the same product on different ChannelIDs. So, if we have 100 ChannelIDs, we will have 100 times the same product (document) if this product is available is on all channels.
When doing a search, because of these repetition of documents (same product name, description), will it impact the results quality?
Thanks.
Depending on your search, similar documents would all show up in search results. For example, in your ‘100 different channel ids but same product’ example, if one searches by product description (assuming the same product gets the same description), all of the 100 documents of that product would either be returned if the search matched or none of them will.

SalesForce SOQL highest number from size column

i'm new to SOQL and SF, so bear with me :)
I have Sales_Manager__c object tied to Rent__c via Master-Detail relationship. What i need is to get Manager with highest number of Rent deals for a current year. I figured out that the Rents__r.size column stores that info. The question is how can i gain access to the size column and retrieve highest number out of it?
Here is the picture of query i have
My SOQL code
SELECT (SELECT Id FROM Rents__r WHERE StartDate__c=THIS_YEAR) FROM Sales_Manager__c
Try other way around, by starting the query from Rents and then going "up". This will let you sort by the count and stop after say top 5 men (your way would give you list of all managers, some with rents, some without and then what, you'd have to manually go through the list and find the max).
SELECT Manager__c, Manager__r.Name, COUNT(Id)
FROM Rent__c
WHERE StartDate__c=THIS_YEAR
GROUP BY Manager__c, Manager__r.Name
ORDER BY COUNT(Id) DESC
LIMIT 10
(I'm not sure if Manager__c is the right name for your field, experiment a bit)

Neo4j/Cypher | Not getting unique results from the database

I'm trying to get completely unique results from my Neo4j database.
I have a database with all kinds of movies, users can watch movies, and movies can be similar to each other. I'm trying to get a completely unique list of movies that are related to the movies that the user already watched, and filter out the movies that are already watched.
As far as I know this should work by using RETURN DISTINCT m, but that doesn't work if you have watched multiple movies that are similar to one movie.
So to make it simple:
User watched movie A, B and C. All of those movies are similar to movie D
Right now, it returns: D, D, D.
I tried both DISTINCT movie and collect(DISTINCT m) without success
The complete query I'm using is:
MATCH (u:user {name:'" + user + "'})-[:watched]->()-[r:is_similar_to]-(m)
WHERE NOT (u)-[:watched]-(m)
RETURN collect(DISTINCT m), r ORDER BY r.rated
I hope you guys can help me out,
Thanks!
The reason for the "non unique" movies is because you have returned "r".
There is an implicit group by r here. When you switch to the Rows view you can see that you will have 3 rows with unique relationships (r) and within the row unique movies.
Maybe this is what you want:
MATCH (u:user {name:'" + user + "'})-[:watched]->()-[r:is_similar_to]-(m)
WHERE NOT (u)-[:watched]-(m)
RETURN m, sum(r.rated) as score
ORDER by score DESC
You will than have unique Movies and the sum of all the related scores.

Solr sort within group by proximity

I've got a Solr instance containing variable product prices that powers a site search. To search around a date and show results even if the exact requested date is not available, we use a query like this:
product_id: 759 AND arrival_date:[2016-05-20T00:00:00Z-3DAYS TO 2016-05-20T00:00:00Z+3DAYS]
The results are grouped by product_id. The grouping inherits a price asc sorting order from the main query.
This, unfortunately, has the negative side effect of always returning documents with the lowest price, regardless of the date, even if the requested date is available.
How can I sort within a group by proximity to a date?
Use the group.sort=arrival_date desc
Alternatively if you want to sort all results based on the date first then the price us &sort=date desc,price asc

Solr: Fetching results with a minimum from each category

I am using solr 4.4.0. The search is performed on products, each of which has a category field. I want to retrieve top n products. But, if some category has less than m products among the top n, then I want to retrieve more products only for those categories.
Eg. I have 4 categories a, b, c, d. n=20 and m=5. Now lets say the top 20(=n) have following category distribution (a:6, b:4, c:6, d:4). Categories b and d have less than m(=5) products. So I would like to fetch one more product(with the next highest score) for both these categories.
Is there a way I can do this using solr
Did you try to solve this with FieldCollapsing?
You use group.field=category, and group.limit lets you set the size of each group. Then you need to be a bit careful on how the groups are sorted, I think it was by the first doc in the group...
But I guess you can achieve what you are looking for fairly easy.

Resources