07-FindBySimilarFavorites

command
v0.0.0-...-a96d7aa Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 21, 2014 License: MIT Imports: 3 Imported by: 0

README

DM.07 Find By Similar Favorites

List a user's favorite things.
func getFavoriteList(user string) {
	stmt := `
		MATCH (user:User {name: {userSub}})-[:favorite]->(thing:Thing)
		RETURN thing.name
		ORDER BY thing.name
	`
	params := neoism.Props{"userSub": user}

	res := []struct {
		Favorite string `json:"thing.name"`
	}{}

	// construct query
	cq := neoism.CypherQuery{
		Statement:  stmt,
		Parameters: params,
		Result:     &res,
	}

	// execute query
	err := db.Cypher(&cq)
	panicErr(err)

	fmt.Println(user, "favorites:")
	if len(res) > 0 {
		for _, n := range res {
			fmt.Printf("  %-24s\n", n.Favorite)
		}
	} else {
		fmt.Println("  No results found")
	}
}
List the users and number of similar favorites to a user
func findSameFavoriteUsersOfUser(user string) {
	stmt := `
		MATCH (user:User)-[:favorite]->(thing)<-[:favorite]-(other:User)
		WHERE user.name = {userSub}
		RETURN other.name, count(thing) AS ocount
		ORDER BY ocount DESC, other.name
	`
	params := neoism.Props{"userSub": user}

	res := []struct {
		Other string `json:"other.name"`
		Count int    `json:"ocount"`
	}{}

	// construct query
	cq := neoism.CypherQuery{
		Statement:  stmt,
		Parameters: params,
		Result:     &res,
	}

	// execute query
	err := db.Cypher(&cq)
	panicErr(err)

	fmt.Println(user, "friends    #cofavorites:")
	if len(res) > 0 {
		for _, n := range res {
			fmt.Printf("  %-24s  %4d\n", n.Other, n.Count)
		}
	} else {
		fmt.Println("  No results found")
	}
}
List the friends and number of similar favorites to a user

Same as last, but restricted to friends

func findSameFavoriteUsersOfUser(user string) {
	stmt := `
		MATCH (user:User)-[:favorite]->(thing)<-[:favorite]-(other:User)
		WHERE user.name = {userSub}
		RETURN other.name, count(thing) AS ocount
		ORDER BY ocount DESC, other.name
	`
	params := neoism.Props{"userSub": user}

	res := []struct {
		Other string `json:"other.name"`
		Count int    `json:"ocount"`
	}{}

	// construct query
	cq := neoism.CypherQuery{
		Statement:  stmt,
		Parameters: params,
		Result:     &res,
	}

	// execute query
	err := db.Cypher(&cq)
	panicErr(err)

	fmt.Println(user, "friends    #cofavorites:")
	if len(res) > 0 {
		for _, n := range res {
			fmt.Printf("  %-24s  %4d\n", n.Other, n.Count)
		}
	} else {
		fmt.Println("  No results found")
	}
}
List of users who favorited a thing
func findThingFavoriteList(thing string) {
	stmt := `
		MATCH (user:User)-[:favorite]->(thing:Thing {name: {thingSub}})
		RETURN user.name
		ORDER BY user.name
	`
	params := neoism.Props{"thingSub": thing}

	res := []struct {
		Name string `json:"user.name"`
	}{}

	// construct query
	cq := neoism.CypherQuery{
		Statement:  stmt,
		Parameters: params,
		Result:     &res,
	}

	// execute query
	err := db.Cypher(&cq)
	panicErr(err)

	fmt.Println(thing, "favorited by:", len(res))
	if len(res) > 0 {
		for _, n := range res {
			fmt.Printf("  %-24s\n", n.Name)
		}
	} else {
		fmt.Println("  No results found")
	}

}

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL