Skip to content

Instantly share code, notes, and snippets.

@oliverfoggin
Last active August 29, 2015 13:57
Show Gist options
  • Save oliverfoggin/9743873 to your computer and use it in GitHub Desktop.
Save oliverfoggin/9743873 to your computer and use it in GitHub Desktop.
Search for a book by its cover
== Search for a book by its cover
Playing on the saying "Don't judge a book by its cover" I created this so that if you see a book online or in a shop but you forget what it's called you can find it by recalling details about the cover.
//hide
//setup
[source, cypher]
----
//publishers
create (apress:Publisher {name:"Apress"}),
//index on :Publisher(name),
//colors
(black:Color {name:"black"}),
(yellow:Color {name:"yellow"}),
(red:Color {name:"red"}),
(white:Color {name:"white"}),
(purple:Color {name:"purple"}),
(orange:Color {name:"orange"}),
(pink:Color {name:"pink"}),
(blue:Color {name:"blue"}),
(green:Color {name:"green"}),
//index on :Color(name),
//pictures
(rose:Picture {name:"rose"}),
(globe:Picture {name:"globe"}),
(sphere:Picture {name:"sphere"}),
(daisy:Picture {name:"daisy"}),
(fruit:Picture {name:"fruit"}),
(melon:Picture {name:"melon"}),
(lime:Picture {name:"lime"}),
(jellybean:Picture {name:"jellybean"}),
(sweet:Picture {name:"sweet"}),
(pansy:Picture {name:"pansy"}),
(flower:Picture {name:"flower"}),
(orangeFruit:Picture {name:"orange"}),
(orchid:Picture {name:"orchid"}),
(strawberry:Picture {name:"strawberry"}),
(pineapple:Picture {name:"pineapple"}),
(lemon:Picture {name:"lemon"}),
(cocoa:Picture {name:"cocoa"}),
(blocks:Picture {name:"blocks"}),
(dragonfruit:Picture {name:"dragonfruit"}),
(android:Picture {name:"android"}),
//index on :Picture(name),
//category
(iPhone:Topic {name:"iPhone"}),
//index on :Topic(name),
//books and relationships
(book1:Book {title:"Learn iOS7 Development"}),
(book1)-[:HAS_COLOR]->(black),
(book1)-[:HAS_COLOR]->(yellow),
(book1)-[:HAS_COLOR]->(red),
(book1)-[:HAS_COLOR]->(white),
(book1)-[:HAS_PICTURE]->(rose),
(book1)-[:HAS_PICTURE]->(flower),
(book1)-[:HAS_PUBLISHER]->(apress),
(book1)-[:HAS_TOPIC]->(iPhone),
(book2:Book {title:"Android Game Recipes"}),
(book2)-[:HAS_COLOR]->(black),
(book2)-[:HAS_COLOR]->(yellow),
(book2)-[:HAS_COLOR]->(red),
(book2)-[:HAS_COLOR]->(white),
(book2)-[:HAS_PICTURE]->(globe),
(book2)-[:HAS_PICTURE]->(sphere),
(book2)-[:HAS_PICTURE]->(android),
(book2)-[:HAS_PUBLISHER]->(apress),
(book3:Book {title:"iOS 7 Development Recipes"}),
(book3)-[:HAS_COLOR]->(black),
(book3)-[:HAS_COLOR]->(yellow),
(book3)-[:HAS_COLOR]->(white),
(book3)-[:HAS_PICTURE]->(flower),
(book3)-[:HAS_PICTURE]->(daisy),
(book3)-[:HAS_PUBLISHER]->(apress),
(book3)-[:HAS_TOPIC]->(iPhone),
(book4:Book {title:"Learn Java for Android Development"}),
(book4)-[:HAS_COLOR]->(black),
(book4)-[:HAS_COLOR]->(yellow),
(book4)-[:HAS_COLOR]->(white),
(book4)-[:HAS_PICTURE]->(globe),
(book4)-[:HAS_PICTURE]->(sphere),
(book4)-[:HAS_PICTURE]->(android),
(book4)-[:HAS_PUBLISHER]->(apress),
(book5:Book {title:"iPhone and iPad Apps for Absolute Beginners"}),
(book5)-[:HAS_COLOR]->(black),
(book5)-[:HAS_COLOR]->(yellow),
(book5)-[:HAS_COLOR]->(white),
(book5)-[:HAS_COLOR]->(green),
(book5)-[:HAS_PICTURE]->(fruit),
(book5)-[:HAS_PICTURE]->(melon),
(book5)-[:HAS_PUBLISHER]->(apress),
(book6:Book {title:"Learn Objective-C on the Mac: For OS X and iOS"}),
(book6)-[:HAS_COLOR]->(black),
(book6)-[:HAS_COLOR]->(yellow),
(book6)-[:HAS_COLOR]->(white),
(book6)-[:HAS_COLOR]->(green),
(book6)-[:HAS_PICTURE]->(fruit),
(book6)-[:HAS_PICTURE]->(lime),
(book6)-[:HAS_PUBLISHER]->(apress),
(book7:Book {title:"Learn Android App Development"}),
(book7)-[:HAS_COLOR]->(black),
(book7)-[:HAS_COLOR]->(yellow),
(book7)-[:HAS_COLOR]->(white),
(book7)-[:HAS_COLOR]->(pink),
(book7)-[:HAS_PICTURE]->(sweet),
(book7)-[:HAS_PICTURE]->(jellybean),
(book7)-[:HAS_PICTURE]->(android),
(book7)-[:HAS_PUBLISHER]->(apress),
(book8:Book {title:"Expert Android"}),
(book8)-[:HAS_COLOR]->(black),
(book8)-[:HAS_COLOR]->(yellow),
(book8)-[:HAS_COLOR]->(white),
(book8)-[:HAS_COLOR]->(purple),
(book8)-[:HAS_PICTURE]->(sweet),
(book8)-[:HAS_PICTURE]->(jellybean),
(book8)-[:HAS_PICTURE]->(android),
(book8)-[:HAS_PUBLISHER]->(apress),
(book9:Book {title:"Pro Android Graphics"}),
(book9)-[:HAS_COLOR]->(black),
(book9)-[:HAS_COLOR]->(yellow),
(book9)-[:HAS_COLOR]->(white),
(book9)-[:HAS_COLOR]->(red),
(book9)-[:HAS_PICTURE]->(sweet),
(book9)-[:HAS_PICTURE]->(jellybean),
(book9)-[:HAS_PICTURE]->(android),
(book9)-[:HAS_PUBLISHER]->(apress),
(book10:Book {title:"Learn Cocoa on the Mac"}),
(book10)-[:HAS_COLOR]->(black),
(book10)-[:HAS_COLOR]->(yellow),
(book10)-[:HAS_COLOR]->(white),
(book10)-[:HAS_COLOR]->(orange),
(book10)-[:HAS_PICTURE]->(fruit),
(book10)-[:HAS_PICTURE]->(orangeFruit),
(book10)-[:HAS_PUBLISHER]->(apress),
(book11:Book {title:"Learn Corona SDK Game Development"}),
(book11)-[:HAS_COLOR]->(black),
(book11)-[:HAS_COLOR]->(yellow),
(book11)-[:HAS_COLOR]->(white),
(book11)-[:HAS_COLOR]->(blue),
(book11)-[:HAS_PICTURE]->(flower),
(book11)-[:HAS_PUBLISHER]->(apress),
(book12:Book {title:"Learn Lua for iOS Game Development"}),
(book12)-[:HAS_COLOR]->(black),
(book12)-[:HAS_COLOR]->(yellow),
(book12)-[:HAS_COLOR]->(white),
(book12)-[:HAS_PICTURE]->(flower),
(book12)-[:HAS_PICTURE]->(orchid),
(book12)-[:HAS_PUBLISHER]->(apress),
(book13:Book {title:"Beginning Android 4"}),
(book13)-[:HAS_COLOR]->(black),
(book13)-[:HAS_COLOR]->(yellow),
(book13)-[:HAS_COLOR]->(white),
(book13)-[:HAS_COLOR]->(blue),
(book13)-[:HAS_PICTURE]->(globe),
(book13)-[:HAS_PICTURE]->(sphere),
(book13)-[:HAS_PICTURE]->(android),
(book13)-[:HAS_PUBLISHER]->(apress),
(book14:Book {title:"Learn GameSalad for iOS: Game Development for iPhone, iPad, and HTML5"}),
(book14)-[:HAS_COLOR]->(black),
(book14)-[:HAS_COLOR]->(yellow),
(book14)-[:HAS_COLOR]->(white),
(book14)-[:HAS_COLOR]->(red),
(book14)-[:HAS_PICTURE]->(fruit),
(book14)-[:HAS_PICTURE]->(strawberry),
(book14)-[:HAS_PUBLISHER]->(apress),
(book15:Book {title:"Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X"}),
(book15)-[:HAS_COLOR]->(black),
(book15)-[:HAS_COLOR]->(yellow),
(book15)-[:HAS_COLOR]->(white),
(book15)-[:HAS_PICTURE]->(fruit),
(book15)-[:HAS_PICTURE]->(pineapple),
(book15)-[:HAS_PUBLISHER]->(apress),
(book16:Book {title:"Learn C on the Mac: For OS X and iOS"}),
(book16)-[:HAS_COLOR]->(black),
(book16)-[:HAS_COLOR]->(yellow),
(book16)-[:HAS_COLOR]->(white),
(book16)-[:HAS_PICTURE]->(fruit),
(book16)-[:HAS_PICTURE]->(lemon),
(book16)-[:HAS_PUBLISHER]->(apress),
(book17:Book {title:"Beginning Android 4 Games Development"}),
(book17)-[:HAS_COLOR]->(black),
(book17)-[:HAS_COLOR]->(yellow),
(book17)-[:HAS_COLOR]->(white),
(book17)-[:HAS_COLOR]->(pink),
(book17)-[:HAS_PICTURE]->(globe),
(book17)-[:HAS_PICTURE]->(sphere),
(book17)-[:HAS_PICTURE]->(android),
(book17)-[:HAS_PUBLISHER]->(apress),
(book18:Book {title:"Learn Cocoa Touch for iOS"}),
(book18)-[:HAS_COLOR]->(black),
(book18)-[:HAS_COLOR]->(yellow),
(book18)-[:HAS_COLOR]->(white),
(book18)-[:HAS_COLOR]->(red),
(book18)-[:HAS_PICTURE]->(cocoa),
(book18)-[:HAS_PICTURE]->(fruit),
(book18)-[:HAS_PUBLISHER]->(apress),
(book19:Book {title:"Android Recipes: A Problem-Solution Approach"}),
(book19)-[:HAS_COLOR]->(black),
(book19)-[:HAS_COLOR]->(yellow),
(book19)-[:HAS_COLOR]->(white),
(book19)-[:HAS_COLOR]->(red),
(book19)-[:HAS_PICTURE]->(globe),
(book19)-[:HAS_PICTURE]->(sphere),
(book19)-[:HAS_PICTURE]->(android),
(book19)-[:HAS_PUBLISHER]->(apress),
(book20:Book {title:"Learn OpenGL ES: For Mobile Game and Graphics Development"}),
(book20)-[:HAS_COLOR]->(black),
(book20)-[:HAS_COLOR]->(yellow),
(book20)-[:HAS_COLOR]->(white),
(book20)-[:HAS_PICTURE]->(globe),
(book20)-[:HAS_PICTURE]->(sphere),
(book20)-[:HAS_PICTURE]->(android),
(book20)-[:HAS_PUBLISHER]->(apress),
(book21:Book {title:"Pro Windows Phone App Development"}),
(book21)-[:HAS_COLOR]->(black),
(book21)-[:HAS_COLOR]->(yellow),
(book21)-[:HAS_COLOR]->(white),
(book21)-[:HAS_COLOR]->(purple),
(book21)-[:HAS_PICTURE]->(blocks),
(book21)-[:HAS_PUBLISHER]->(apress)
----
//graph
== All the books
Lets see what books we have
[source,cypher]
----
MATCH (book:Book)
RETURN book.title AS Title
----
//table
== Filter by a picture on the cover
I know there was one with a picture of a fruit...?
[source,cypher]
----
MATCH (book:Book)
WHERE (book)-[:HAS_PICTURE]->(:Picture { name:"fruit" })
RETURN book.title AS Title
----
//table
== Compound query
Maybe it was a green fruit???
[source,cypher]
----
MATCH (book:Book)
WHERE (book)-[:HAS_PICTURE]->(:Picture { name:"fruit" }) AND (book)-[:HAS_COLOR]->(:Color { name:"green" })
RETURN book.title AS Title
----
//table
== How about searching for a book about iPhone stuff...
Text/topic search for iPhone
[source,cypher]
----
MATCH (book:Book)
WHERE (book)-[:HAS_TOPIC]->(:Topic {name:"iPhone"}) OR book.title =~ ".*iPhone.*"
RETURN book.title AS Title
----
//table
== Suggested search/auto complete...
Based on what is being typed suggest filters that the user might be looking for.
User starts typing... "g" so we can give them suggestions...
[source,cypher]
----
MATCH (node)<--(book:Book)
WHERE node.name =~ "g.*"
RETURN DISTINCT labels(node)[0] AS Type, node.name AS `Suggested Search`, count(book) AS `Number of books`
ORDER BY `Number of books` DESC ;
----
//table
== Whats the next best filter to use?
Based on my current filter what is the next best filter to use to clear the list?
[source,cypher]
----
MATCH (book)-[:HAS_PICTURE]->(picture:Picture { name:"android" })
OPTIONAL
MATCH (book)-->(attribute)
WHERE attribute <> picture
RETURN DISTINCT labels(attribute)[0] AS `Suggested Type`, attribute.name AS `Suggested Text`, count(attribute) AS `Number of books`
ORDER BY `Number of books` DESC
LIMIT 6
----
//table
//console
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment