Database Reference
In-Depth Information
For almost all cases, it is recommended not to use the graphId because it can be recycled when its node is
deleted. In this case, the productNodeId should be considered safe to use, because products would not be in danger
of being deleted but only removed from a Location relationship.
Listing 9-40.
The
product_search
Route and
product_search
Methods
# return product array as json
@route('/productsearch/<q>')
def product_search (q):
# get matches
productsFound = Product().product_search(graph_db, q + ".*")
# create array
products = Product().product_results_as_json(productsFound)
# set response type
response.content_type = 'application/json'
# return as json
return dumps(products)
# search for products
def product_search(self, graph_db, q):
query = neo4j.CypherQuery(graph_db, "MATCH (p:Product) " +
" WHERE lower(p.title) =~ {q} " +
" RETURN TOSTRING(ID(p)) as id, count(*) as name, " +
" p.title as label " +
" ORDER BY p.title LIMIT 5")
params = {"q": q}
result = query.execute(**params)
return result
# return products as a list
def product_results_as_list (self, productsFound):
products = []
for r in productsFound:
products.append({"id": r.id, "title": r.name, "label": r.label})
return products
Once the product and distance have been set and the search is executed, the Location route tests to see if a
productNodeId
property has been set. If so, the
locations_within_distance_with_product
method is called from
the
Location
class, as shown in Listing 9-41.
Listing 9-41.
The
locations_within_distance_with_product
Method in the
Location
Class
def locations_within_distance_with_product(self, graph_db, lq, productNodeId, mappedUserLocation):
query = neo4j.CypherQuery(graph_db,
" START n = node:geom({lq}), " +
" p=node({productNodeId}) " +
" MATCH n-[:HAS]->p " +