@GET @Path("{userID}") @Produces({MediaType.TEXT_PLAIN, CSVMessageBodyWriter.TEXT_CSV, MediaType.APPLICATION_JSON}) public List<IDValue> get( @PathParam("userID") String userID, @DefaultValue("10") @QueryParam("howMany") int howMany, @DefaultValue("0") @QueryParam("offset") int offset) throws OryxServingException { check(howMany > 0, "howMany must be positive"); check(offset >= 0, "offset must be nonnegative"); ALSServingModel model = getALSServingModel(); float[] userVector = model.getUserVector(userID); checkExists(userVector != null, userID); List<Pair<String, float[]>> knownItemVectors = model.getKnownItemVectorsForUser(userID); if (knownItemVectors == null || knownItemVectors.isEmpty()) { return Collections.emptyList(); } Iterator<Pair<String, Double>> idDots = knownItemVectors .stream() .map( itemIDVector -> new Pair<>( itemIDVector.getFirst(), VectorMath.dot(userVector, itemIDVector.getSecond()))) .iterator(); Ordering<Pair<?, Double>> ordering = Ordering.from((p1, p2) -> p1.getSecond().compareTo(p2.getSecond())); return toIDValueResponse(ordering.leastOf(idDots, howMany + offset), howMany, offset); }