Database Reference
In-Depth Information
Listing 8-42.
The
product_search
Route and
product_search
Methods
// return product array as json
$app->get('/productsearch/:q', function($q) use ($app){
# get matches
$productsFound = Product::productSearch($q);
$app->response->headers->set('Content-Type', 'application/json');
echo json_encode($productsFound);
})->name('productsearch');
// product_search method - located in the Product service class.
public static function productSearch($q){
$q = trim($q) . ".*";
$queryString = " MATCH (p:Product) WHERE lower(p.title) =~ {q} ".
" RETURN count(*) as name, TOSTRING(ID(p)) as id, p.title as label " .
" ORDER BY p.title " .
" LIMIT 5 ";
$query = new Everyman\Neo4j\Cypher\Query(Neo4Client::client(), $queryString, array(
'q' => $q
));
$result = $query->getResultSet();
return self::returnMappedProductSearch($result);
}
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 consider safe to use, because products would not be in danger of
being deleted but only removed from a Location relationship.
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
locationsWithinDistanceWithProduct
method is called from the
Location class, which is shown in Listing 8-43.
Listing 8-43.
The
locationsWithinDistanceWithProduct
Method in the
Location
Class
public static function locationsWithinDistanceWithProduct($lq,$mappedUserLocation,$productNodeId){
$queryString = " START n = node:geom({lq}), p=node({productNodeId}) " .
" MATCH n-[:HAS]->p " .
" RETURN n.locationId as locationId, n.address as address, " .
" n.city as city, n.state as state, n.zip as zip, n.name as name, " .
" n.lat as lat, n.lon as lon";
$query = new Everyman\Neo4j\Cypher\Query(Neo4Client::client(), $queryString, array(
'lq' => $lq,
'productNodeId' => intval($productNodeId)
));