0

I'm implementing infinite scroll and trying to get new items every time the user reach the bottom of the page.

problem is, neo4j return nodes that were already returned.

I read that ORDER BY is must for this case, but it didn't help.

I tried follow the solution here: How to paginate query results with cypher?, and put the pagination clauses (LIMIT, SKIP...) before the WITH clause, but it didn't work. It also throw an error

the query:

MATCH(shops: Shop)
WHERE shops.title = ~ '(?i).*${search}.*'
WITH shops
ORDER BY shops.title asc
SKIP $skip
LIMIT $limit
MATCH(products: Product) < -[relation: SELL] - (shops)
RETURN shops, products, relation

how the query can guarantee new items on each request ?

*note - the limit is always 12, and skip is the items array length, the client app contain on the request.

EDIT 1 - IMAGES FROM THE DB OF THE DUPLICATION

it can be shown that a node with the same id, is returned from the two queries.

1ST page - skip 0, limit 12

1st page - skip 0, limit 12

2ND page - skip 12, limit 12

2ND page - skip 12, limit 12

Itay Tur
  • 683
  • 1
  • 15
  • 40
  • What do you want paginate? Shop titles or products? – fbiville Jan 25 '21 at 10:48
  • @fbiville shops – Itay Tur Jan 25 '21 at 10:55
  • What's an item? A shop? A product? Something else? – fbiville Jan 25 '21 at 14:04
  • @fbiville i try to paginate Shop. i want to get 12 new Shops every request. but, i get in 2nd request a shop that was returned on the first request. – Itay Tur Jan 25 '21 at 14:20
  • First page: `skip = 0`, `limit 12`. Second page: `skip = 12`, `limit 12`. Third page: `skip = 24`, `limit 12`. etc Those values should do what you want, i.e. `limit = 12`, `skip = (pageNumber - 1) * limit` (assuming first page number is 1). – fbiville Jan 25 '21 at 16:03
  • @fbiville thats the issue. 1st page: skip 0, limit 12. 2nd: skip 12 limit 12. 3rd: skip 24 limit 12. still, on the 2nd page one of the shops, already returned in the 1st page. – Itay Tur Jan 25 '21 at 16:33
  • Do you reproduce this issue by running the queries directly against Neo4j? How many shops do you have in total in the DB `MATCH(shops: Shop) RETURN COUNT(shops)`? – fbiville Jan 25 '21 at 16:56
  • @fbiville yes I run it against the DB and see that I get a shop in the 2nd page, that was on the 1st. I have 69 shops – Itay Tur Jan 25 '21 at 18:20
  • @fbiville if it helps, I added images of the issue live in EDIT 1. – Itay Tur Jan 25 '21 at 18:40
  • If you truncate the query to just: `MATCH(shops: Shop) WHERE shops.title = ~ '(?i).*${search}.*' RETURN shops ORDER BY shops.title asc SKIP $skip LIMIT $limit`, does the same problem happen? – fbiville Jan 26 '21 at 10:00
  • @fbiville yes still the same – Itay Tur Jan 26 '21 at 16:46
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/227859/discussion-between-fbiville-and-itay-tur). – fbiville Jan 26 '21 at 21:06
  • 1
    @fbiville hey, thank you for your help. we found the solution, I added it at the bottom. have a great weekend :) – Itay Tur Jan 27 '21 at 18:38

1 Answers1

2

Neo4j doesn't have fallback when the order by is facing 2 or more nodes with the same value. I tried to order by the title Timer and there is several of Shops with this title. the work around was adding the fallback to another comparison.

ORDER BY shops.title, shops.id

weird that neo4j doesn't provide failsafe ( like, id of the node ), and resolving in sending the same node when skip is used.

Itay Tur
  • 683
  • 1
  • 15
  • 40