Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pagination using offset/length is not working #404

Closed
jonasanso opened this issue Aug 30, 2017 · 3 comments
Closed

Pagination using offset/length is not working #404

jonasanso opened this issue Aug 30, 2017 · 3 comments

Comments

@jonasanso
Copy link

jonasanso commented Aug 30, 2017

Executing

client.execute {
  search into "index" query
    Query(query = Some("text"),
      attributesToRetrieve = Some(Seq("objectID")),
      attributesToHighlight = Some(Nil),
      offset = Some(25),
      length = Some(10)
    )
}.recover {
  case NonFatal(e) => throw new AlgoliaException("fail", e)
}

Throws this exception

Exception in thread "main" com.depop.product.algolia.AlgoliaException: Error executing fail
	at Main$$anonfun$1.applyOrElse(Main.scala:29)
	at Main$$anonfun$1.applyOrElse(Main.scala:28)
	at scala.runtime.AbstractPartialFunction.apply(AbstractPartialFunction.scala:36)
	at scala.util.Failure$$anonfun$recover$1.apply(Try.scala:216)
	at scala.util.Try$.apply(Try.scala:192)
	at scala.util.Failure.recover(Try.scala:216)
	at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:326)
	at scala.concurrent.Future$$anonfun$recover$1.apply(Future.scala:326)
	at scala.concurrent.impl.CallbackRunnable.run(Promise.scala:36)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)
Caused by: algolia.AlgoliaClientException: Failed on last retry
	at algolia.AlgoliaClient$$anonfun$request$1.applyOrElse(AlgoliaClient.scala:153)
	at algolia.AlgoliaClient$$anonfun$request$1.applyOrElse(AlgoliaClient.scala:149)
	at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:346)
	at scala.concurrent.Future$$anonfun$recoverWith$1.apply(Future.scala:345)
	... 4 more
Caused by: org.json4s.package$MappingException: No usable value for page
Did not find value which can be converted into int
	at org.json4s.reflect.package$.fail(package.scala:93)
	at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$buildCtorArg(Extraction.scala:514)
	at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$14.apply(Extraction.scala:534)
	at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$14.apply(Extraction.scala:534)
	at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
	at scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:234)
	at scala.collection.mutable.ResizableArray$class.foreach(ResizableArray.scala:59)
	at scala.collection.mutable.ArrayBuffer.foreach(ArrayBuffer.scala:48)
	at scala.collection.TraversableLike$class.map(TraversableLike.scala:234)
	at scala.collection.AbstractTraversable.map(Traversable.scala:104)
	at org.json4s.Extraction$ClassInstanceBuilder.org$json4s$Extraction$ClassInstanceBuilder$$instantiate(Extraction.scala:522)
	at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$8.apply(Extraction.scala:585)
	at org.json4s.Extraction$ClassInstanceBuilder$$anonfun$result$8.apply(Extraction.scala:580)
	at org.json4s.Extraction$.org$json4s$Extraction$$customOrElse(Extraction.scala:594)
	at org.json4s.Extraction$ClassInstanceBuilder.result(Extraction.scala:580)
	at org.json4s.Extraction$.extract(Extraction.scala:389)
	at org.json4s.Extraction$.extract(Extraction.scala:39)
	at org.json4s.ExtractableJsonAstNode.extract(ExtractableJsonAstNode.scala:21)
	at algolia.AlgoliaHttpClient$$anon$1.onCompleted(AlgoliaHttpClient.scala:72)

The response from algolia servers does not contain some fields required in the class algolia.SearchResult.

{"hits":[{"objectID":"55696231"},...],"nbHits":5240,"offset":25,"length":10,"processingTimeMS":3,"exhaustiveNbHits":false,"query":"text","params":"attributesToRetrieve=objectID&query=text&offset=25&length=10&attributesToHighlight="}

The specification says page and hitsPerPage are not returned when we search using offset/length
https://www.algolia.com/doc/api-reference/api-methods/search/
But SearchResult has them defined as mandatory and the parsing of the response crashes.

https://github.com/algolia/algoliasearch-client-scala/blob/master/src/main/scala/algolia/responses/SearchResult.scala

This can be solved having a one to one mapping with the specification or allowing to provide alternative classes to parse the response or maybe in a totally different way. Personally I do not even need page and hitsPerPage in the SearchResult class so that offers a third solution to my issue, but I imagine others might rely on them.

Please let me know if there is any other input needed.

@jonasanso
Copy link
Author

Great! Quite fast turn around.
The code is in master, but does not seem to be released in a new version of the library.
What is the process you follow for releases?

@jonasanso
Copy link
Author

jonasanso commented Sep 1, 2017

Looks like there were 3 fields optional. I reported 2 in the issue and you fixed a different one.

I think this change does not totally fix the problem as hitsPerPage is still mandatory.

@ElPicador
Copy link
Contributor

I've forgot to add my changes, so the previous PR was incomplete 🙄.

It's fixed in master. I'm releasing a new version 1.24.1 right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants