Incorrect totalElements and totalPages values for Spring Page - java

I have 21 rows in a database and I am using Spring's Page to get them.
If I specify a page size less than 22, it will show totalElements: 3864 and totalPages is also wrong. What could be wrong?
Example:
GET myurl.com/foo?size=5
yields a result like this
{
"content": [
{
{ "pretend_that": "there are five elements here" }
}
],
"pageable": {
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 5,
"paged": true,
"unpaged": false
},
"last": false,
"totalPages": 773,
"totalElements": 3864,
"size": 5,
"number": 0,
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"first": true,
"numberOfElements": 5,
"empty": false
}
Example 2:
GET myurl.com/foo?size=50
yields a result like this
{
"content": [
{
{ "pretend_that": "there are 21 elements here" }
}
],
"pageable": {
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"offset": 0,
"pageNumber": 0,
"pageSize": 50,
"paged": true,
"unpaged": false
},
"last": true,
"totalPages": 1,
"totalElements": 21,
"size": 50,
"number": 0,
"sort": {
"empty": true,
"sorted": false,
"unsorted": true
},
"first": true,
"numberOfElements": 21,
"empty": false
}
P.S. if I get the final page, it shows the page and elements count correctly.
I have tried changing Spring Boot versions and that hasn't helped.

Related

Group by with count in mongo db using spring data

I am new to mongo with spring data. i am trying to achive grouping for one of my data collection which is like below
[{
"id": 1,
"validNumber": true,
"validEmail": true
},
{
"id": 2,
"validNumber": false,
"validEmail": false
},
{
"id": 3,
"validNumber": true,
"validEmail": false
},
{
"id": 4,
"validNumber": false,
"validEmail": true
}
]
I am trying to group to above data set to get the total number of validNumber and valid emails . expected response should be something like below
{
"total": 4,
"validNumber": 2,
"inValidNumber": 2,
"validEmail": 1,
"inValidEmail": 3
}
I have written the below mongo query to achive the same
db.collection.aggregate([
{
"$group": {
"_id": null,
"total": {
"$sum": 1
},
"validEmail": {
"$sum": {
"$cond": [{
"$eq": ["$validEmail", true]
}, 1, 0]
}
},
"inValidEmail": {
"$sum": {
"$cond": [{
"$eq": ["$validEmail", false]
}, 1, 0]
}
},
"validNumber": {
"$sum": {
"$cond": [{
"$eq": ["$validNumber", true]
}, 1, 0]
}
},
"inValidNumber": {
"$sum": {
"$cond": [{
"$eq": ["$validNumber", false]
}, 1, 0]
}
}
}
},
{
"$project": {
_id: 0,
}
}
]);
How to implement the above query using Spring data JPA.
Thanks in advance
I tried to implement using the Aggregate function looking at the below example from here,but unable to add conditions as in my mongo query.
collection.aggregate(Aggregates.group("$stars", Accumulators.sum("count", 1))));

JSON Object Navigation to nested value

I am new to JSON and trying to manipulate JSON for some validation
My JSON looks like this . I need to pick the JSON object based on the refcode and then get count of different object in that, and navigate deeper inside to get the key value pair. Can someone guide me how I can navigate.
{
"components": [
{
"id": 12,
"text": "ABC",
"refCode": "CO_ABC",
"patternCode": "0",
"components": [
{
"id": 1234,
"text": "types",
"refCode": "CO_TYPES",
"questions": [
{
"questionId": 122324,
"questionText": "Is this you",
"questionSequence": 1,
"questionRefCode": "QN_STAY",
"hasPreselectedAnswer": false,
"responsesMetadata": {
"cardinality": "single",
"patternCode": "5",
"dataType": "STRING",
"numberMin": null,
"numberMax": null
},
"choices": [
{
"choiceId": 5456,
"choiceRefCode": "YES",
"choiceText": "Yes",
"sequence": 1
},
{
"choiceId": 8798,
"choiceRefCode": "NO",
"choiceText": "No",
"sequence": 2
}
],
"editable": true,
"accessible": true
}
]
},
{
"id": 13,
"text": "State of stay",
"refCode": "CO_STATE",
"questions": [
{
"questionId": 1,
"questionText": "Which state do you stay",
"questionSequence": 2,
"questionRefCode": "QN_STATE",
"hasPreselectedAnswer": false,
"responsesMetadata": {
"cardinality": "multiple",
"patternCode": "1",
"dataType": "STRING",
"numberMin": null,
"numberMax": null
},
"choices": [
{
"choiceId": 1,
"choiceRefCode": "CH_AZ",
"choiceText": "Arizona",
"sequence": 1
},
{
"choiceId": 2,
"choiceRefCode": "CH_PA",
"choiceText": "Pennsylvania",
"sequence": 2
}
],
"accessible": true
}
]
}
]
}
]
}

Spring Data Page object serialization to JSON with Spring Boot 2?

I am trying to upgrade my Spring Boot 1.5 project to Spring Boot 2.0.2. This includes an update to Spring Data 2.x as well.
I have now noticed that the JSON representation of Page has changed. It now looks like:
{
"content": [{
"id": "96ab09c6-2cfc-4195-899b-899b623e6e97",
"title": "Test Title",
"shortDescription": "Short description",
"description": "Test Description",
"date": "2018-02-14",
"imageUrl": "/api/images/newsposts/f637e6bd-a13a-4ebc-8c58-8ba639e09f70"
}],
"pageable": "INSTANCE",
"totalPages": 1,
"totalElements": 1,
"last": true,
"size": 0,
"number": 0,
"first": true,
"numberOfElements": 1,
"sort": {
"unsorted": true,
"sorted": false
}
}
If it is sorted, it looks similar to this:
"pageable": {
"sort": {
"sorted": true,
"unsorted": false
},
"offset": 0,
"pageSize": 20,
"pageNumber": 0,
"paged": true,
"unpaged": false
},
"totalElements": 1,
"last": true,
"totalPages": 1,
"size": 20,
"number": 0,
"first": true,
"numberOfElements": 1,
"sort": {
"sorted": true,
"unsorted": false
}
Notice the pageable element that was not there before. Also the sort element is not very useful. Is this something that was intended? Or it is just a bad idea to return org.springframework.data.domain.Page object in my REST controller?
APIwise, Spring tends to be very backward compatible.
This could be one of the minor case of backward incompatibility but Pageable is a Spring-specific construct and who's to say it will never change. Depending on one of the Spring's implementation data class till the REST layer does not seem right somehow. It would be good to handle this change in the service layer.
This is a very good example which shows the benefits of a 'layered' architecture :)

How do I get sum of a field in solr 4.8

This is my response data:
"response": {
"numFound": 2,
"start": 0,
"docs": [
{
"total_amount": 10,
"id": "2"
},
{
"total_amount": 10,
"id": "1"
}
]
}
I want to get sum of total_amount. I tried facet query also. But I did't get sum. I got some blog on this but that is for solr 5.1. http://yonik.com/solr-facet-functions/
You can use the stats functionality to get this information. Just put the followed parameters in your query:
stats=true&stats.field=total_amount
Your response will be like that:
"response": {
"numFound": 2,
"start": 0,
"docs": [
{
"id": "1",
"total_amount": 15
},
{
"id": "2",
"total_amount": 12
}
]
},
"stats": {
"stats_fields": {
"total_amount": {
"min": 12,
"max": 15,
"count": 2,
"missing": 0,
"sum": 27,
"sumOfSquares": 369,
"mean": 13.5,
"stddev": 2.1213203435596424,
"facets": {}
}
}
Note that you have lots of information around the total_amount field including the sum.

How can I parse this JSON in Android?

I want to pull out the user block. The JSON result will always change, sometimes 4 users will be returned, sometimes 10 etc.
{
"results": [
{
"user": {
"avatar_url_thumb": "http://avatars.stocktwits.com/production/9998/thumb-1270014645.png?1270014645",
"avatar_url_medium": "http://avatars.stocktwits.com/production/9998/medium-1270014645.png?1270014645",
"created_at": "2010-03-15T05:44:51Z",
"following_count": 14,
"updated_at": "2010-08-30T18:22:15Z",
"id": 9998,
"updates_count": 31,
"avatar_url_large": "http://avatars.stocktwits.com/production/9998/large-1270014645.png?1270014645",
"investor_relations": false,
"last_name": "Reporter",
"followers_count": 25,
"recommended": false,
"bio": "Apple News & AAPL Stock Analysis, visit Apple Digest blog link above",
"login": "AppleReporter",
"first_name": "Apple"
}
},
{
"user": {
"avatar_url_thumb": "http://api.stocktwits.com/images/default_avatar_thumb.jpg",
"avatar_url_medium": "http://api.stocktwits.com/images/default_avatar_medium.jpg",
"created_at": "2010-04-14T01:02:05Z",
"following_count": 0,
"updated_at": "2010-08-30T18:29:56Z",
"id": 12924,
"updates_count": 1,
"avatar_url_large": "http://api.stocktwits.com/images/default_avatar_large.jpg",
"investor_relations": false,
"last_name": "Shareholder",
"followers_count": 0,
"recommended": false,
"bio": null,
"login": "Imurphit",
"first_name": "Apple"
}
},
{
"user": {
"avatar_url_thumb": "http://api.stocktwits.com/images/default_avatar_thumb.jpg",
"avatar_url_medium": "http://api.stocktwits.com/images/default_avatar_medium.jpg",
"created_at": "2010-04-17T20:52:09Z",
"following_count": 0,
"updated_at": "2010-08-30T18:31:23Z",
"id": 13234,
"updates_count": 0,
"avatar_url_large": "http://api.stocktwits.com/images/default_avatar_large.jpg",
"investor_relations": false,
"last_name": "Apple",
"followers_count": 0,
"recommended": false,
"bio": null,
"login": "apple11",
"first_name": "John"
}
},
{
"user": {
"avatar_url_thumb": "http://api.stocktwits.com/images/default_avatar_thumb.jpg",
"avatar_url_medium": "http://api.stocktwits.com/images/default_avatar_medium.jpg",
"created_at": "2010-07-12T19:04:51Z",
"following_count": 0,
"updated_at": "2010-08-30T20:12:15Z",
"id": 18691,
"updates_count": 0,
"avatar_url_large": "http://api.stocktwits.com/images/default_avatar_large.jpg",
"investor_relations": false,
"last_name": "Smith",
"followers_count": 0,
"recommended": false,
"bio": null,
"login": "apple",
"first_name": "Jacob"
}
},
{
"user": {
"avatar_url_thumb": "http://api.stocktwits.com/images/default_avatar_thumb.jpg",
"avatar_url_medium": "http://api.stocktwits.com/images/default_avatar_medium.jpg",
"created_at": "2010-07-13T17:06:27Z",
"following_count": 0,
"updated_at": "2010-08-30T20:12:30Z",
"id": 18808,
"updates_count": 3,
"avatar_url_large": "http://api.stocktwits.com/images/default_avatar_large.jpg",
"investor_relations": false,
"last_name": "apple",
"followers_count": 0,
"recommended": false,
"bio": null,
"login": "applejames",
"first_name": "James"
}
}
],
"page": 1,
"symbol": false,
"per_page": 20,
"response": {
"status": 200
},
"total_pages": 1,
"total_entries": 6
}
Use the JSONObject
// Get some JSON from wherever
String json = getJSONFromServer();
// Parse the JSON response into an object
JSONObject object = new JSONObject(json);
// Get the results array
JSONArray users = object.getJSONArray("results");
for(int i = 0; i < users.length(); i++) {
// Each element in the results array is a JSONObject with a single
// property "user" which is a JSONObject that contains the user data
JSONObject user = users.getJSONObject(i).getJSONObject("user");
// Do something with the user
String firstName = user.getString("first_name");
}

Categories

Resources