While some may argue that this question belongs on programmers.stackexchange.com, after reviewing the Help Center of Stack Overflow, I believe it is a specific programming issue and will likely receive a better response here.
I have developed a webapp using ExpressJS with a Neo4j database backend. On the search screen, I want to leverage Neo4j's relationships. The screen allows users to input values such as manufacturing year, fuel type, gearbox, etc., which are then sent via POST request to ExpressJS where I construct a Cypher query based on these parameters. An example of the query is shown below:
MATCH
(v:VEHICLE),(v)-[:VGEARBOX_IS]->(:VGBOX{type:'MANUAL'}),
(v)-[:VCONDITION_IS]->(:VCONDITION{condition:'USED'})
WITH DISTINCT v
WHERE v.manufacture_year = 1990
MATCH (v)-[r]->(info)
RETURN v AS vehicle, COLLECT({type:type(r), data:info}) AS details
Running the above query returns information about three vehicles along with their properties.
https://i.stack.imgur.com/qBdvP.jpg
If there are more than 20 vehicles in the result, I would like to paginate the results using SKIP and LIMIT as shown below:
MATCH
(v:VEHICLE)
OPTIONAL MATCH (v)-[r:VFUEL_TYPE|:VGEARBOX_IS|:VHAVING_COLOR|...]->(info)
RETURN
v.vehicle_id AS vehicle_id,
v.engine_size AS engine_size,
v.published_date AS published_date,
COUNT(v) AS count,
COLLECT({type:type(r), data:info}) as data
ORDER BY v.published_date DESC
SKIP 20
LIMIT 16
The workflow for this process is as follows:
- User navigates to the search screen form with various input fields using POST method.
- User selects options to search based on their preferences.
- User submits the form triggering a post request to the server.
- This request is handled by a ROUTE which constructs a Cypher query using the request parameters and runs it against the Neo4j database to retrieve the results.
- If there are 200 matching vehicles, only 20 are displayed, along with next/previous buttons for pagination.
- To show additional results, the same query is rerun with an updated SKIP value.
My question is, what is the most optimal way to save the original search request or generated Cypher query so that clicking next/previous pages triggers a rerun using different SKIP values? I aim to avoid making fresh POST requests every time the user changes pages. There are several potential solutions, but I am unsure which is the most performance-friendly:
- Create a new POST request with preserved values each time the user clicks next/previous pages (I am concerned about the costliness of multiple POSTs).
- Store the original Cypher query in Redis and retrieve it when needed for pagination updates (handling deletion when necessary).
- Save the query in a session or temporary storage for quick access (security and efficiency concerns).
If you have encountered this issue before and found an efficient solution, please share your insights on how best to address this problem.