Group, Project, Aggregation, Sort, MongoDB Query to Spring Data - java

I want to group by below mongo collection.
Group by for year and month. and I want to populate the below java class as a list. I tried many ways could not find the relevant article so I'm publishing problem here,
[{
"_id": {
"$oid": "620f09ff8ed7d86747d45344"
},
"user_id": "620f09808ed7d86747d4533f",
"price": 351,
"created_time": {
"$date": "2021-12-18T02:52:47.837Z"
},
"orderItem": [
{
"bookId": "620e02dacc3e1d31bd8deb54",
"qty": 1,
"unitPrice": 101
},
{
"bookId": "620f033cb4e4494aff6ef56c",
"qty": 1,
"unitPrice": 250
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620f57e0e7d4cf6156c16682"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 610,
"created_time": {
"$date": "2022-01-18T08:25:04.569Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 2,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620f58aafde9e82682d5a725"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-02-18T08:28:26.483Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8ead2463f3c44d87108"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-01-18T15:19:06.938Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8ebd2463f3c44d87109"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2021-12-18T15:19:07.829Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8ecd2463f3c44d8710a"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-01-18T15:19:08.575Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8edd2463f3c44d8710b"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-02-18T15:19:09.269Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8edd2463f3c44d8710c"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-02-18T15:19:09.867Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8eed2463f3c44d8710d"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-02-18T15:19:10.374Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8eed2463f3c44d8710e"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-02-18T15:19:10.785Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8f0d2463f3c44d8710f"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-02-18T15:19:12.250Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
},{
"_id": {
"$oid": "620fb8f0d2463f3c44d87110"
},
"user_id": "620f09b98ed7d86747d45343",
"price": 380,
"created_time": {
"$date": "2022-02-18T15:19:12.762Z"
},
"orderItem": [
{
"bookId": "620f0313b4e4494aff6ef56b",
"qty": 1,
"unitPrice": 150
},
{
"bookId": "620f0347b4e4494aff6ef56d",
"qty": 1,
"unitPrice": 230
}
],
"_class": "com.code.onlinebookstoreservice.entity.Order"
}]
Finally, I was able to create a native query it is as below,
db.order.aggregate([
{
$project: {
total_book_count: {
$sum: "$orderItem.qty"
},
total_purches_amount: {
"$sum": "$price"
},
monthly: {
$dateToString: {
format: "%Y-%m",
date: "$created_time"
}
}
}
},
{
$group: {
_id: "$monthly",
total_order_count: {
$count: {}
},
total_book_count: {
$sum: "$total_book_count"
},
total_purches_amount: {
$sum: "$total_purches_amount"
}
}
},
{
$sort: {
total_order_count: -1
}
}
])
My Java Class is like below, ultimately I want this output as an array.
#Getter
#Setter
public class StaticDto {
private String _id;
private Integer totalOrderCount;
private Integer totalBookCount;
private Integer totalPurchasedAmount;
}

Finally, I was able to create a aggregation query for my problem,
public ResponseEntity<List<StaticDto>> getStatistics() {
AggregationExpression totalQty = AccumulatorOperators.Sum.sumOf("orderItem.qty");
AggregationExpression totalPrice = AccumulatorOperators.Sum.sumOf("price");
Aggregation agg = Aggregation.newAggregation(
project()
.and(totalQty).as("total_book_count")
.and(totalPrice).as("total_purchase_amount")
.and("created_time").dateAsFormattedString("%Y-%m").as("month"),
group("month")
.count().as("totalOrderCount")
.sum("total_book_count").as("totalBookCount")
.sum("total_purchase_amount").as("totalPurchasedAmount"),
sort(Sort.Direction.DESC, "month")
);
AggregationResults<StaticDto> staticDtos =
mongoTemplate.aggregate(agg, "order", StaticDto.class);
return ResponseEntity.status(HttpStatus.OK).body(staticDtos.getMappedResults());
}

Related

How to get Max and Min Value in MongoDB based on specific key?

I want to get the max and min value of the emp salary based on currency. every employee has a salary range based on currency also all the details in response should be unique. while I am using aggregation function min and max but it fetches the max and min value of salary amount but I need to get max and min based on currency field.
Sample Data:
[
{
"id": "1",
"emp_name": "emp1",
"data": [
{
"emp_country": "country1",
"emp_city": "city1",
"salary": [
{
"currency": "INR",
"amount": 5000
},
{
"currency": "INR",
"amount": 600
},
{
"currency": "MXN",
"amount": 400
}
]
},
{
"emp_country": "country1",
"emp_city": "city2",
"salary": [
{
"currency": "DOLLER",
"amount": 5000
},
{
"currency": "DOLLER",
"amount": 200
},
{
"currency": "MXN",
"amount": 400
}
]
}
]
},
{
"id": "2",
"emp_name": "emp2",
"data": [
{
"emp_country": "country2",
"emp_city": "city2",
"salary": [
{
"currency": "INR",
"amount": 5000
},
{
"currency": "MXN",
"amount": 200
},
{
"currency": "MXN",
"amount": 400
}
]
}
]
},
{
"id": "3",
"emp_name": "emp3",
"data": [
{
"emp_country": "country1",
"emp_city": "city1",
"salary": [
{
"currency": "MXN",
"amount": 400
}
]
}
]
},
{
"id": "4",
"emp_name": "emp4",
"data": [
{
"emp_country": "country1",
"emp_city": "city2",
"salary": [
{
"currency": "DOLLER",
"amount": 200
}
]
}
]
}
]
Expected Output:
city, country, the name should be unique and salary have max and min based on currency.
[
{
"emp_city": "city1",
"emp_country": "country1",
"emp_name": "emp1",
"emp_salary": [{
"currency": "INR",
"max": 5000,
"min": 600
},
{
"currency": "MXN",
"max": 400,
"min": 400
}]
},
{
"emp_city": "city2",
"emp_country": "country1",
"emp_name": "emp1",
"emp_salary":[ {
"currency": "DOLLER",
"max": 5000,
"min": 200
},
{
"currency": "MXN",
"max": 400,
"min": 400
}]
},
{
"emp_city": "city2",
"emp_country": "country2",
"emp_name": "emp2",
"emp_salary": [{
"currency": "INR",
"max": 5000,
"min": 5000
},
{
"currency": "MXN",
"max": 400,
"min": 200
}]
},
{
"emp_city": "city1",
"emp_country": "country1",
"emp_name": "emp3",
"emp_salary": [{
"currency": "MXN",
"max": 400,
"min": 400
}]
},
{
"emp_city": "city2",
"emp_country": "country1",
"emp_name": "emp4",
"emp_salary": [{
"currency": "DOLLER",
"max": 200,
"min": 200
}]
}
]
We can achieve the above result using an aggregation query.
$unwind - to deconstruct array of salary
$group - group by emp city, country, name, currency with min and max value of salary
$group - use another to push in emp_salary
$project - to show fields in your format
db.collection.aggregate([
{
"$unwind": {
"path": "$data"
}
},
{
"$unwind": {
"path": "$data.salary"
}
},
{
"$group": {
"_id": {
"emp_city": "$data.emp_city",
"emp_country": "$data.emp_country",
"emp_name": "$emp_name",
"currency": "$data.salary.currency",
},
"max": {
"$max": "$data.salary.amount"
},
"min": {
"$min": "$data.salary.amount"
}
}
},
{
"$group": {
"_id": {
"emp_city": "$_id.emp_city",
"emp_country": "$_id.emp_country",
"emp_name": "$_id.emp_name",
},
"emp_salary": {
"$push": {
"currency": "$_id.currency",
"min": "$min",
"max": "$max"
}
}
}
},
{
"$project": {
"_id": 0,
"emp_city": "$_id.emp_city",
"emp_country": "$_id.emp_country",
"emp_name": "$_id.emp_name",
"emp_salary": 1,
}
}
])
Mongo Playground

Mongo DB aggregation optimization (Spring Data)

I am using Spring Boot (2.4.2) in my application and MongoDB as the database. I have 1.1M documents in a single collection and I am trying to do some aggregations, my document structure looks like this:
In java code my aggregation query looks like this:
Aggregation aggregation = newAggregation(
match(where("gameRef")
.is(gameRef)),
group("platformRef", "gameRef", "currency")
.sum("bet")
.as("bet")
.sum("win")
.as("win")
.sum("data.bonusWin")
.as("bonus")
.count()
.as("count"),
project("platformRef", "gameRef", "currency")
.andInclude("bet")
.andInclude("win")
.andInclude("bonus")
.andInclude("count")
);
AggregationResults<SpinReport> results = mongoTemplate.aggregate(aggregation, SpinHistory.class, SpinReport.class);
return results.getMappedResults();
This gives me this aggregation in MongoDB language:
{
"aggregate": "__collection__",
"pipeline": [
{
"$match": {
"gameRef": "6047a10c58ed573e490b8f54"
}
},
{
"$group": {
"_id": {
"platformRef": "$platformRef",
"gameRef": "$gameRef",
"currency": "$currency"
},
"bet": {
"$sum": "$bet"
},
"win": {
"$sum": "$win"
},
"bonus": {
"$sum": "$data.bonusWin"
},
"count": {
"$sum": 1
}
}
},
{
"$project": {
"platformRef": "$_id.platformRef",
"gameRef": "$_id.gameRef",
"currency": "$_id.currency",
"bet": 1,
"win": 1,
"bonus": 1,
"count": 1
}
}
]
}
This query takes 5 seconds to execute (in 1.1M documents). I wonder if there is any way to optimize it?
I created these indexes in that collection:
and I can see that gameRef field index is being used when I execute this query, but it doesn't make any difference in terms of performance. It still takes 5 seconds.
Is it possible to somehow make this work faster?
EDIT:
by running explain plan for this query:
db.spinHistory.explain().aggregate([
{
"$match": {
"gameRef": "6047a10c58ed573e490b8f54"
}
},
{
"$project": {
"platformRef": 1,
"gameRef": 1,
"currency": 1,
"win": 1,
"bet": 1,
"bonusWin": "$data.bonusWin",
"_id": 0
}
},
{
"$group": {
"_id": {
"platformRef": "$platformRef",
"gameRef": "$gameRef",
"currency": "$currency"
},
"bet": {
"$sum": "$bet"
},
"win": {
"$sum": "$win"
},
"bonus": {
"$sum": "$data.bonusWin"
},
"count": {
"$sum": 1
}
}
},
{
"$project": {
"platformRef": "$_id.platformRef",
"gameRef": "$_id.gameRef",
"currency": "$_id.currency",
"bet": 1,
"win": 1,
"bonus": 1,
"count": 1
}
}
])
suggested by #Yahya, I can see this:
{
"stages": [
{
"$cursor": {
"queryPlanner": {
"plannerVersion": 1,
"namespace": "oak9e_rgs_temp.spinHistory",
"indexFilterSet": false,
"parsedQuery": {
"gameRef": {
"$eq": "6047a10c58ed573e490b8f54"
}
},
"queryHash": "27C08187",
"planCacheKey": "E204EC8C",
"winningPlan": {
"stage": "PROJECTION_DEFAULT",
"transformBy": {
"bet": true,
"platformRef": true,
"win": true,
"currency": true,
"gameRef": true,
"bonusWin": "$data.bonusWin",
"_id": false
},
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {
"gameRef": 1,
"platformRef": 1,
"currency": 1,
"bet": 1,
"win": 1,
"data.bonusWin": 1
},
"indexName": "idx_spin_history_main_fields",
"isMultiKey": false,
"multiKeyPaths": {
"gameRef": [],
"platformRef": [],
"currency": [],
"bet": [],
"win": [],
"data.bonusWin": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"gameRef": [
"[\"6047a10c58ed573e490b8f54\", \"6047a10c58ed573e490b8f54\"]"
],
"platformRef": [
"[MinKey, MaxKey]"
],
"currency": [
"[MinKey, MaxKey]"
],
"bet": [
"[MinKey, MaxKey]"
],
"win": [
"[MinKey, MaxKey]"
],
"data.bonusWin": [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans": []
}
}
},
{
"$group": {
"_id": {
"platformRef": "$platformRef",
"gameRef": "$gameRef",
"currency": "$currency"
},
"bet": {
"$sum": "$bet"
},
"win": {
"$sum": "$win"
},
"bonus": {
"$sum": "$data.bonusWin"
},
"count": {
"$sum": {
"$const": 1
}
}
}
},
{
"$project": {
"_id": true,
"bet": true,
"bonus": true,
"count": true,
"win": true,
"platformRef": "$_id.platformRef",
"gameRef": "$_id.gameRef",
"currency": "$_id.currency"
}
}
],
"serverInfo": {
"host": "DESKTOP-V3NTFPM",
"port": 27017,
"version": "4.4.3",
"gitVersion": "913d6b62acfbb344dde1b116f4161360acd8fd13"
},
"ok": 1
}
And this is the index I create for all fields that are being used in this query:
{
"v": 2,
"key": {
"gameRef": 1,
"platformRef": 1,
"currency": 1,
"bet": 1,
"win": 1,
"data.bonusWin": 1
},
"name": "idx_spin_history_main_fields",
"background": false
}
With execution stats:
{
"stages": [
{
"$cursor": {
"queryPlanner": {
"plannerVersion": 1,
"namespace": "oak9e_rgs_temp.spinHistory",
"indexFilterSet": false,
"parsedQuery": {
"gameRef": {
"$eq": "6047a10c58ed573e490b8f54"
}
},
"queryHash": "27C08187",
"planCacheKey": "E204EC8C",
"winningPlan": {
"stage": "PROJECTION_DEFAULT",
"transformBy": {
"gameRef": true,
"win": true,
"platformRef": true,
"bet": true,
"currency": true,
"bonusWin": "$data.bonusWin",
"_id": false
},
"inputStage": {
"stage": "IXSCAN",
"keyPattern": {
"gameRef": 1,
"platformRef": 1,
"currency": 1,
"bet": 1,
"win": 1,
"data.bonusWin": 1
},
"indexName": "idx_spin_history_main_fields",
"isMultiKey": false,
"multiKeyPaths": {
"gameRef": [],
"platformRef": [],
"currency": [],
"bet": [],
"win": [],
"data.bonusWin": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"gameRef": [
"[\"6047a10c58ed573e490b8f54\", \"6047a10c58ed573e490b8f54\"]"
],
"platformRef": [
"[MinKey, MaxKey]"
],
"currency": [
"[MinKey, MaxKey]"
],
"bet": [
"[MinKey, MaxKey]"
],
"win": [
"[MinKey, MaxKey]"
],
"data.bonusWin": [
"[MinKey, MaxKey]"
]
}
}
},
"rejectedPlans": []
},
"executionStats": {
"executionSuccess": true,
"nReturned": 1145023,
"executionTimeMillis": 4473,
"totalKeysExamined": 1145023,
"totalDocsExamined": 0,
"executionStages": {
"stage": "PROJECTION_DEFAULT",
"nReturned": 1145023,
"executionTimeMillisEstimate": 623,
"works": 1145024,
"advanced": 1145023,
"needTime": 0,
"needYield": 0,
"saveState": 1295,
"restoreState": 1295,
"isEOF": 1,
"transformBy": {
"gameRef": true,
"win": true,
"platformRef": true,
"bet": true,
"currency": true,
"bonusWin": "$data.bonusWin",
"_id": false
},
"inputStage": {
"stage": "IXSCAN",
"nReturned": 1145023,
"executionTimeMillisEstimate": 161,
"works": 1145024,
"advanced": 1145023,
"needTime": 0,
"needYield": 0,
"saveState": 1295,
"restoreState": 1295,
"isEOF": 1,
"keyPattern": {
"gameRef": 1,
"platformRef": 1,
"currency": 1,
"bet": 1,
"win": 1,
"data.bonusWin": 1
},
"indexName": "idx_spin_history_main_fields",
"isMultiKey": false,
"multiKeyPaths": {
"gameRef": [],
"platformRef": [],
"currency": [],
"bet": [],
"win": [],
"data.bonusWin": []
},
"isUnique": false,
"isSparse": false,
"isPartial": false,
"indexVersion": 2,
"direction": "forward",
"indexBounds": {
"gameRef": [
"[\"6047a10c58ed573e490b8f54\", \"6047a10c58ed573e490b8f54\"]"
],
"platformRef": [
"[MinKey, MaxKey]"
],
"currency": [
"[MinKey, MaxKey]"
],
"bet": [
"[MinKey, MaxKey]"
],
"win": [
"[MinKey, MaxKey]"
],
"data.bonusWin": [
"[MinKey, MaxKey]"
]
},
"keysExamined": 1145023,
"seeks": 1,
"dupsTested": 0,
"dupsDropped": 0
}
}
}
},
"nReturned": NumberLong(1145023),
"executionTimeMillisEstimate": NumberLong(4074)
},
{
"$group": {
"_id": {
"platformRef": "$platformRef",
"gameRef": "$gameRef",
"currency": "$currency"
},
"bet": {
"$sum": "$bet"
},
"win": {
"$sum": "$win"
},
"bonus": {
"$sum": "$data.bonusWin"
},
"count": {
"$sum": {
"$const": 1
}
}
},
"nReturned": NumberLong(3),
"executionTimeMillisEstimate": NumberLong(4467)
},
{
"$project": {
"_id": true,
"bonus": true,
"count": true,
"win": true,
"bet": true,
"platformRef": "$_id.platformRef",
"gameRef": "$_id.gameRef",
"currency": "$_id.currency"
},
"nReturned": NumberLong(3),
"executionTimeMillisEstimate": NumberLong(4467)
}
],
"serverInfo": {
"host": "DESKTOP-V3NTFPM",
"port": 27017,
"version": "4.4.3",
"gitVersion": "913d6b62acfbb344dde1b116f4161360acd8fd13"
},
"ok": 1
}
If the size of an index isn't an issue (which sounds like it doesn't). Then remove all of these new indexes that you have created for your aggregation pipeline. Create the following index:
{ gameRef: 1, platformRef: 1, currency: 1, bet: 1, win: 1, "data.bonusWin": 1 }
Which is essentially indexing all the fields you need.
All you need next is projection.
So, between match and group stage. Add another stage for projection like:
{ _id: 0, gameRef: 1, platformRef: 1, currency: 1, bet: 1, win: 1, "data.bonusWin": 1 }
Important bit here is to project out _id
After edits are made to the question, it is evident that's the best it can get. But it is still 4seconds long.
Possible solutions:
Don't run the aggregation pipeline every time. Use a $merge stage to create materialised view. And query it from there.
Create a summary collection, this can be done from the app code itself, or can be done using Change Streams (those again work in app scope).

Elastic Search query Not workng with java but with elastic URI

I created an Index for a few documents. However I am facing trouble while running for a search query as it is showing zero hits. Find the indexing file, indexed data and and Java code below:
Mapping.json:
{
"settings": {
"index": {
"max_ngram_diff": 39
},
"analysis": {
"normalizer": {
"custom_normalizer": {
"type": "custom",
"char_filter": [],
"filter": [
"lowercase",
"asciifolding"
]
}
},
"analyzer": {
"custom_analyzer": {
"tokenizer": "custom_tokenizer",
"filter": [
"lowercase"
]
},
"autocomplete_search": {
"type": "custom",
"tokenizer": "keyword",
"filter": "lowercase"
}
},
"tokenizer": {
"custom_tokenizer": {
"type": "ngram",
"min_gram": 1,
"max_gram": 40,
"token_chars": [
"letter",
"digit",
"whitespace",
"punctuation",
"symbol"
]
}
}
}
},
"mappings": {
"type_name": {
"properties": {
"primaryTerm": {
"type": "text",
"analyzer": "custom_analyzer",
"search_analyzer": "autocomplete_search",
"fielddata": "true",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "custom_normalizer"
}
}
},
"entityType": {
"type": "keyword",
"normalizer": "custom_normalizer"
},
"variants": {
"type": "text",
"analyzer": "custom_analyzer",
"search_analyzer": "autocomplete_search",
"fielddata": "true",
"fields": {
"raw": {
"type": "keyword",
"normalizer": "custom_normalizer"
}
}
}
}
}
}
}
Elastic data.json:
{
"took": 82,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 4,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "relmap",
"_type": "type_name",
"_id": "X4NutXcBKX-KYFzehi4s",
"_score": 1,
"_source": {
"entityType": "Agency",
"primaryTerm": "K2IPR INTELLECTUAL PROPERTY ATTORNEYS",
"variants": [
"K2IPR",
"K2I.P.R. (PATENT & TRADEMARK ATTRONEYS)",
"K2I.P.R",
"K2.I.P.R",
"K2 I.P.R",
"K2 I.P.R",
"K2 IPR",
"K2 I. P. R."
]
}
},
{
"_index": "relmap",
"_type": "type_name",
"_id": "YINutXcBKX-KYFzeoy7V",
"_score": 1,
"_source": {
"entityType": "Agency",
"primaryTerm": "EPIPHANY IP SOLUTIONS PVT. LTD.",
"variants": [
"EPIPHANY IP SOLUTIONS PRIVATE LIMITED",
"EPIPHANY IP SOLUTIONS PVT. LTD"
]
}
},
{
"_index": "relmap",
"_type": "type_name",
"_id": "YYNutXcBKX-KYFzepC44",
"_score": 1,
"_source": {
"entityType": "Agency",
"primaryTerm": "Natural Remedies Private Limited",
"variants": [
"NATURAL REMEDIES PVT. LTD",
"NATURAL REMEDIES PVT LTD",
"NATURAL REMEDIES"
]
}
},
{
"_index": "relmap",
"_type": "type_name",
"_id": "YoNutXcBKX-KYFzepC5i",
"_score": 1,
"_source": {
"entityType": "Agency",
"primaryTerm": "NICHE, INTELLECTUAL PROPERTY OFFICES",
"variants": [
"NICHE INTELLECTUAL PROPERTY OFFICES",
"NICHE INTELLECTUAL PROPERTY OFFICECS",
"NICHE INTELLECTUAL PROPERTY LAW OFFICES"
]
}
}
]
}
}
Search query:
String query="{\"query_string\":{\"query\":\""+searchTerm.toUpperCase()+"\",\"default_operator\":\"AND\",\"default_field\":\"primaryTerm\"}}";
Java Code for elastic Query:
SearchResponse searchResponse=null;
try {
String queryJson=json;
System.out.println("queryJson:"+queryJson);
//System.out.println(indexName);
SearchRequest searchRequest = new SearchRequest(indexName);
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.from(start)
.size(length)
.query(QueryBuilders.wrapperQuery(queryJson));
searchRequest.source(searchSourceBuilder);
searchResponse = elasticsearchRestTemplate.getClient().search(searchRequest, RequestOptions.DEFAULT);
For a Search Term = 'Ni' in an autosuggest drpDown I am getting zero hits for the field 'primaryTerm'
I also tried the termQuerybuilder instead of wrapper query:
SearchRequestBuilder searchRequestBuilder = client.prepareSearch("relmap");
SearchResponse searchResponse = searchRequestBuilder.
setQuery(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("variants", "NATURAL REMEDIES PVT. LTD"))).setFrom(0).setSize(1000).setExplain(true).setTrackTotalHits(true).execute().actionGet();
SearchHits searchHits = searchResponse.getHits();
Interestinlgy I am getting the desired output with elastic url.
Elastic URI code:
http://localhost:9201/relmap/_search?q=NIC&df=primaryTerm&explain=true&default_operator=AND
Can anyone help let me know the fault /correct way to get the hits from the query

Looking for highlight search where it will return all matching fields from one record

In highlighting search for my application how search works for us is if we pass pageSize it will only return one matching field from record.
For example
There are 4 records
remaining information added in comments as I am unable add code here please advice how can I achieve this requirement
If you need to highlight the result of all matching fields of each document, then you can achieve this in the following way:
Adding a working example with index data, mapping, search query, and search result
Index Mapping:
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 20,
"token_chars": [
"letter",
"digit"
]
}
}
},
"max_ngram_diff": 50
},
"mappings": {
"properties": {
"name": {
"type": "text",
"analyzer": "my_analyzer"
},
"lastname": {
"type": "text",
"analyzer": "my_analyzer"
},
"firstname": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
Index Data:
{
"name": "jhon",
"Age": 24,
"lastname": "willam",
"firstname": "henry"
}
{
"name": "kellin",
"Age": 24,
"lastname": "kevin",
"firstname": "mathew"
}
{
"name": "keeper",
"Age": 24,
"lastname": "Jr",
"firstname": "gomez"
}
{
"name": "Asif",
"Age": 24,
"lastname": "peter",
"firstname": "willaim kemp"
}
Search Query:
{
"query": {
"multi_match": {
"query": "ke"
}
},
"highlight": {
"fields": {
"*": {
"type": "plain",
"fragment_size": 20,
"pre_tags": "<span class='bold'>",
"post_tags": "</span>",
"number_of_fragments": 1
}
}
}
}
Search Result:
"hits": [
{
"_index": "64996939",
"_type": "_doc",
"_id": "2",
"_score": 1.1374959,
"_source": {
"name": "kellin",
"Age": 24,
"lastname": "kevin",
"firstname": "mathew"
},
"highlight": {
"name": [
"<span class='bold'>ke</span>llin"
], <-- note this
"lastname": [
"<span class='bold'>ke</span>vin"
]
}
},
{
"_index": "64996939",
"_type": "_doc",
"_id": "4",
"_score": 0.9552834,
"_source": {
"name": "Asif",
"Age": 24,
"lastname": "peter",
"firstname": "willaim kemp"
},
"highlight": {
"firstname": [
"willaim <span class='bold'>ke</span>mp" <-- note this
]
}
},
{
"_index": "64996939",
"_type": "_doc",
"_id": "3",
"_score": 0.62883455,
"_source": {
"name": "keeper",
"Age": 24,
"lastname": "Jr",
"firstname": "gomez"
},
"highlight": {
"name": [
"<span class='bold'>ke</span>eper" <--note this
]
}
}
]

get single value from json string

i have Json string like this where the function return List:
[
{
"name": "DEFAULT",
"effectiveDate": "Apr 19, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 20, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "NO_BILLING_PERIOD",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 21, 2018 3:35:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
},
{
"name": "DEFAULT",
"effectiveDate": "Apr 21, 2018 3:38:31 PM",
"currencies": [
"IDR"
],
"products": [
{
"type": "BASE",
"name": "PostPaid",
"plans": [
{
"name": "postpaid-monthly-fix",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 300000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial1",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 1
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 250000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
},
{
"name": "postpaid-monthly-trial7",
"billingPeriod": "MONTHLY",
"phases": [
{
"type": "TRIAL",
"prices": [],
"fixedPrices": [
{
"currency": "IDR",
"value": 0
}
],
"duration": {
"unit": "DAYS",
"number": 7
},
"usages": []
},
{
"type": "EVERGREEN",
"prices": [
{
"currency": "IDR",
"value": 350000
}
],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": []
}
]
}
],
"included": [],
"available": []
},
{
"type": "BASE",
"name": "PrePaid",
"plans": [
{
"name": "prepaid-signature-item",
"billingPeriod": "NO_BILLING_PERIOD",
"phases": [
{
"type": "EVERGREEN",
"prices": [],
"fixedPrices": [],
"duration": {
"unit": "UNLIMITED",
"number": -1
},
"usages": [
{
"billingPeriod": "DAILY",
"tiers": [
{
"blocks": [
{
"unit": "item",
"size": "1.0",
"max": "5.0",
"prices": [
{
"currency": "IDR",
"value": 250000
}
]
}
],
"limits": [],
"fixedPrice": [],
"recurringPrice": []
}
]
}
]
}
]
}
],
"included": [],
"available": []
}
],
"priceLists": [
{
"name": "DEFAULT",
"plans": [
"postpaid-monthly-fix",
"postpaid-monthly-trial1",
"postpaid-monthly-trial7",
"prepaid-signature-item"
]
}
]
}
]
where json build by this code:
String json = new Gson().toJson(price);
then i want to get single value, for example :
"plans":["postpaid-monthly-fix","postpaid-monthly-trial1","postpaid-monthly-trial7","prepaid-signature-item"]
i try with json.get("plans") but get syntax erro, any clue ?
Try the following:
String s = "[{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 19, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 20, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"NO_BILLING_PERIOD\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 21, 2018 3:35:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]},{\"name\":\"DEFAULT\",\"effectiveDate\":\"Apr 21, 2018 3:38:31 PM\",\"currencies\":[\"IDR\"],\"products\":[{\"type\":\"BASE\",\"name\":\"PostPaid\",\"plans\":[{\"name\":\"postpaid-monthly-fix\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":300000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial1\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":1},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]},{\"name\":\"postpaid-monthly-trial7\",\"billingPeriod\":\"MONTHLY\",\"phases\":[{\"type\":\"TRIAL\",\"prices\":[],\"fixedPrices\":[{\"currency\":\"IDR\",\"value\":0}],\"duration\":{\"unit\":\"DAYS\",\"number\":7},\"usages\":[]},{\"type\":\"EVERGREEN\",\"prices\":[{\"currency\":\"IDR\",\"value\":350000}],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[]}]}],\"included\":[],\"available\":[]},{\"type\":\"BASE\",\"name\":\"PrePaid\",\"plans\":[{\"name\":\"prepaid-signature-item\",\"billingPeriod\":\"NO_BILLING_PERIOD\",\"phases\":[{\"type\":\"EVERGREEN\",\"prices\":[],\"fixedPrices\":[],\"duration\":{\"unit\":\"UNLIMITED\",\"number\":-1},\"usages\":[{\"billingPeriod\":\"DAILY\",\"tiers\":[{\"blocks\":[{\"unit\":\"item\",\"size\":\"1.0\",\"max\":\"5.0\",\"prices\":[{\"currency\":\"IDR\",\"value\":250000}]}],\"limits\":[],\"fixedPrice\":[],\"recurringPrice\":[]}]}]}]}],\"included\":[],\"available\":[]}],\"priceLists\":[{\"name\":\"DEFAULT\",\"plans\":[\"postpaid-monthly-fix\",\"postpaid-monthly-trial1\",\"postpaid-monthly-trial7\",\"prepaid-signature-item\"]}]}]\n";
JSONArray jsonArray = (JSONArray) JSONValue.parseWithException(s);
List plans = (List)((Map)((List)((Map)jsonArray.get(0)).get("priceLists")).get(0)).get("plans");
System.out.println(plans);
System.out.println(plans.get(0));
System.out.println(plans.get(1));
System.out.println(plans.get(2));
System.out.println(plans.get(3));
Output:
["postpaid-monthly-fix","postpaid-monthly-trial1","postpaid-monthly-trial7","prepaid-signature-item"]
postpaid-monthly-fix
postpaid-monthly-trial1
postpaid-monthly-trial7
prepaid-signature-item

Categories

Resources