Database Reference
In-Depth Information
Listing 11-53.
The Search Method to Find Products
public MappedProductSearch[]
search
(String q) {
q = q.trim().toLowerCase() + ".*";
return Iterables.toArray(MappedProductSearch.class,
mappedProductSearchRespository.search
(q));
}
Listing 11-54.
The Search Method in the
MappedProductSearchRespository
@Query(value = "MATCH (p:Product) " +
" WHERE lower(p.title) =~ {q} " +
" RETURN count(*) as count, TOSTRING(ID(p)) as productNodeId, " +
" p.title as name " +
" ORDER BY p.title LIMIT 5")
Iterable<MappedProductSearch>
search
(@Param("q") String q);
@QueryResult
@JsonPropertyOrder(alphabetic = true)
@NodeEntity
public interface
MappedProductSearch
{
@ResultColumn("productNodeId")
String getId();
@ResultColumn("name")
String getLabel();
@ResultColumn("count")
String getName();
}
In many cases, it is recommended not to use the graphId because it can be recycled when its node is deleted.
In this case, the
productNodeId
is 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
LocationController
tests to see if
a
prouctNodeId
property has been set. If so, the
returnLocationsWithinDistanceAndHasProduct
method is called
from LocationImpl, which in turn calls the
returnLocationsWithinDistanceAndHasProduct
method, shown in
Listing 11-55, from the
MappedLocationRepository
interface is called.