Mongo DB aggregation optimization (Spring Data) - java

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).

Related

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

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());
}

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

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

Obtaining a specific field in Elastic search using Kibana and Java

I am very new to ELK stack. I am trying to retrieve a specific field (like in MySQL think we have a table called "Users", trying to retrieve the "phoneNumber" column). I am in need of doing that using Java as well as using the queries typed in Kibana. (for example lets think we need to retrieve the field comments in the elastic search database) Thank you for the help. I am new to Stack Overflow. So if I have done something wrong in asking the question, please let me know, ill correct it. Following is the result obtained in kibana by typing,
POST local_rs_4_0_0_app_test_1552993787904/_search
{
"query": {
"match_all": {}
}
}
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 3,
"successful": 3,
"failed": 0
},
"hits": {
"total": 16,
"max_score": 1,
"hits": [
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "reviewmodel",
"_id": "rm_apptest1552993787904_hotel",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"appKey": "app_test_1552993787904",
"entityType": "hotel",
"reviewParameters": {
"0": {
"key": "title",
"description": "Title",
"writeEnabled": true,
"readEnabled": true,
"usedAsMetadata": false
},
"1": {
"key": "overall",
"description": "Overall",
"writeEnabled": true,
"readEnabled": true,
"usedAsMetadata": false
},
"2": {
"key": "comment",
"description": "Comment",
"writeEnabled": true,
"readEnabled": true,
"usedAsMetadata": false
},
"3": {
"key": "country",
"description": "Country",
"writeEnabled": true,
"readEnabled": true,
"usedAsMetadata": true
}
},
"supportedOntoClass": "hotel"
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "formtemplate",
"_id": "AWmVrLRfjVbLQiy9v6fG",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"templateId": "ft_apptest1552993787904_hotel_hotelcopy1",
"templateName": "hotel Copy 1",
"createdUser": "usr_2012187654",
"createdTime": 1552994317403,
"formDescription": "",
"thankYouNote": "Thank You",
"formHeaderImage": "",
"lastModifiedUser": "usr_2012187654",
"lastModifiedTime": 1552994349085,
"appKey": "app_test_1552993787904",
"entityType": "hotel",
"apiCall": null,
"entityMappedApiCall": null,
"embeddableScript": null,
"deleted": true,
"defaultFormTemplate": false,
"defaultAttachableFormTemplate": false,
"mappedViewTemplateKey": "vt_apptest1552993787904_hotel_hotelcopy1",
"components": {
"0": {
"cmpId": "title1552994317403",
"component": "TEXT_FIELD",
"parameterKey": "title",
"required": true,
"label": "Review Title",
"description": null,
"attributes": [],
"options": [],
"publicViewVisible": true
},
"1": {
"cmpId": "overall1552994317403",
"component": "STAR_INPUT",
"parameterKey": "overall",
"required": true,
"label": "Overall Rating",
"description": null,
"attributes": [],
"options": [],
"publicViewVisible": true
},
"2": {
"cmpId": "comment1552994317403",
"component": "TEXT_AREA",
"parameterKey": "comment",
"required": true,
"label": "Comment",
"description": null,
"attributes": [],
"options": [],
"publicViewVisible": true
},
"3": {
"cmpId": "country1552994317403",
"component": "COUNTRY_PICKER",
"parameterKey": "country",
"required": false,
"label": "Country",
"description": null,
"attributes": [],
"options": [],
"publicViewVisible": true
}
},
"embeddedEntities": [],
"groups": []
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "viewtemplate",
"_id": "AWmVrLTZjVbLQiy9v6fH",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"templateId": "vt_apptest1552993787904_hotel_hotelcopy1",
"templateName": "hotel Copy 1",
"appKey": "app_reviewspotter_1552993787904",
"entityType": "hotel",
"createdUser": "usr_2012187654",
"createdTime": 1552994317527,
"lastModifiedUser": "usr_2012187654",
"lastModifiedTime": 1552994349270,
"components": {
"0": {
"parameterKey": "title",
"component": "TEXT_VIEW",
"label": "Review Title",
"publicViewVisible": true
},
"1": {
"parameterKey": "overall",
"component": "STAR_VIEW",
"label": "Overall Rating",
"publicViewVisible": true
},
"2": {
"parameterKey": "comment",
"component": "TEXT_VIEW",
"label": "Comment",
"publicViewVisible": true
},
"3": {
"parameterKey": "country",
"component": "TEXT_VIEW",
"label": "Country",
"publicViewVisible": true
}
},
"editTemplateId": "ft_apptest1552993787904_hotel_hotelcopy1",
"replyEnabled": true,
"editEnabled": true,
"deleteEnabled": true,
"voteEnabled": true,
"socialShareEnabled": false,
"apiCall": null,
"embeddableScript": null,
"deleted": true,
"defaultViewTemplate": false
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "conversation",
"_id": "cnv_apptest1552993787904_tour__Anuradhapura__1552994436382",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"conversationId": "cnv_apptest1552993787904_tour__Anuradhapura__1552994436382",
"title": "UPDATES",
"startedBy": "usr_2012187654",
"startedByDisplayName": "F",
"startedAt": 1552994436382,
"closedBy": null,
"closedByDisplayName": null,
"closedAt": 0,
"lastUpdatedAt": 1552994436382,
"status": "ONGOING",
"conversationType": "UPDATES",
"startedFromType": "GENERAL",
"priorityLevel": "MEDIUM",
"markedAsIncident": false,
"incidentStatus": "NOT_APPLICABLE",
"appKey": "app_test_1552993787904",
"entityType": "tour",
"entityId": "Anuradhapura",
"groupKey": null,
"limitedToShared": false,
"sharedUserIds": [],
"deleted": false
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "formtemplate",
"_id": "AWmVrobwjVbLQiy9v6fI",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"templateId": "ft_apptest1552993787904_tour_tour",
"templateName": "tour",
"createdUser": "usr_2012187654",
"createdTime": 1552994436841,
"formDescription": "",
"thankYouNote": "Thank You",
"formHeaderImage": "",
"lastModifiedUser": null,
"lastModifiedTime": null,
"appKey": "app_test_1552993787904",
"entityType": "tour",
"apiCall": null,
"entityMappedApiCall": null,
"embeddableScript": null,
"deleted": false,
"defaultFormTemplate": true,
"defaultAttachableFormTemplate": false,
"mappedViewTemplateKey": "vt_apptest1552993787904_tour_tour",
"components": {
"0": {
"cmpId": "title1552994436841",
"component": "TEXT_FIELD",
"parameterKey": "title",
"required": true,
"label": "Review Title",
"description": null,
"attributes": [],
"options": [],
"publicViewVisible": true
},
"1": {
"cmpId": "overall1552994436841",
"component": "STAR_INPUT",
"parameterKey": "overall",
"required": true,
"label": "Overall Rating",
"description": null,
"attributes": [],
"options": [],
"publicViewVisible": true
},
"2": {
"cmpId": "comment1552994436841",
"component": "TEXT_AREA",
"parameterKey": "comment",
"required": true,
"label": "Comment",
"description": null,
"attributes": [],
"options": [],
"publicViewVisible": true
},
"3": {
"cmpId": "country1552994436841",
"component": "COUNTRY_PICKER",
"parameterKey": "country",
"required": false,
"label": "Country",
"description": null,
"attributes": [],
"options": [],
"publicViewVisible": true
}
},
"embeddedEntities": [],
"groups": []
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "review",
"_id": "r_anuradhapura_1552994866938",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"reviewId": "r_anuradhapura_1552994866938",
"version": 1,
"status": "APPROVED",
"deleted": false,
"readyToProcess": true,
"appKey": "app_test_1552993787904",
"appName": "test",
"createdUser": "anonymous-user#app_test_1552993787904.com",
"displayName": "Oshana",
"createdTime": 1552994866943,
"entityType": "tour",
"entityId": "Anuradhapura",
"entityName": "Anuradhapura",
"comment": "Test Comment",
"title": "Test",
"parentReview": null,
"childReviews": [],
"numberData": [
{
"key": "overall",
"value": 5
}
],
"textData": [],
"imageData": [],
"numberMeta": [],
"textMeta": [
{
"key": "user-agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
},
{
"key": "ip",
"value": "0:0:0:0:0:0:0:1"
},
{
"key": "source",
"value": "DIRECT"
},
{
"key": "country",
"value": "Russia"
}
],
"replies": [],
"sentiments": [],
"scoredComment": [],
"rawNumberData": {
"overall": 5
},
"rawTextMeta": {
"country": "Russia",
"ip": "0:0:0:0:0:0:0:1",
"source": "DIRECT",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
},
"rawNumberMeta": {},
"rawScoredComment": {},
"pvCount": 0,
"nvCount": 0
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "viewtemplate",
"_id": "AWmVq7_-jVbLQiy9v6fD",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"templateId": "vt_apptest1552993787904_hotel_hotel",
"templateName": "hotel",
"appKey": "app_test_1552993787904",
"entityType": "hotel",
"createdUser": "usr_2012187654",
"createdTime": 1552994254811,
"lastModifiedUser": null,
"lastModifiedTime": null,
"components": {
"0": {
"parameterKey": "title",
"component": "TEXT_VIEW",
"label": "Review Title",
"publicViewVisible": true
},
"1": {
"parameterKey": "overall",
"component": "STAR_VIEW",
"label": "Overall Rating",
"publicViewVisible": true
},
"2": {
"parameterKey": "comment",
"component": "TEXT_VIEW",
"label": "Comment",
"publicViewVisible": true
},
"3": {
"parameterKey": "country",
"component": "TEXT_VIEW",
"label": "Country",
"publicViewVisible": true
}
},
"editTemplateId": "ft_apptest1552993787904_hotel_hotel",
"replyEnabled": true,
"editEnabled": true,
"deleteEnabled": true,
"voteEnabled": true,
"socialShareEnabled": false,
"apiCall": null,
"embeddableScript": null,
"deleted": false,
"defaultViewTemplate": true
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "viewtemplate",
"_id": "AWmVq8LejVbLQiy9v6fF",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"templateId": "vt_apptest1552993787904_hotel_hotelsubform",
"templateName": "hotel Sub Form",
"appKey": "app_test_1552993787904",
"entityType": "hotel",
"createdUser": "usr_2012187654",
"createdTime": 1552994255578,
"lastModifiedUser": null,
"lastModifiedTime": null,
"components": {
"0": {
"parameterKey": "title",
"component": "TEXT_VIEW",
"label": "Review Title",
"publicViewVisible": true
},
"1": {
"parameterKey": "overall",
"component": "STAR_VIEW",
"label": "Overall Rating",
"publicViewVisible": true
},
"2": {
"parameterKey": "comment",
"component": "TEXT_VIEW",
"label": "Comment",
"publicViewVisible": true
}
},
"editTemplateId": "ft_apptest1552993787904_hotel_hotelsubform",
"replyEnabled": true,
"editEnabled": true,
"deleteEnabled": true,
"voteEnabled": true,
"socialShareEnabled": false,
"apiCall": null,
"embeddableScript": null,
"deleted": false,
"defaultViewTemplate": false
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "viewtemplate",
"_id": "AWmVron1jVbLQiy9v6fL",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"templateId": "vt_apptest1552993787904_tour_toursubform",
"templateName": "tour Sub Form",
"appKey": "app_test_1552993787904",
"entityType": "tour",
"createdUser": "usr_2012187654",
"createdTime": 1552994437618,
"lastModifiedUser": null,
"lastModifiedTime": null,
"components": {
"0": {
"parameterKey": "title",
"component": "TEXT_VIEW",
"label": "Review Title",
"publicViewVisible": true
},
"1": {
"parameterKey": "overall",
"component": "STAR_VIEW",
"label": "Overall Rating",
"publicViewVisible": true
},
"2": {
"parameterKey": "comment",
"component": "TEXT_VIEW",
"label": "Comment",
"publicViewVisible": true
}
},
"editTemplateId": "ft_apptest1552993787904_tour_toursubform",
"replyEnabled": true,
"editEnabled": true,
"deleteEnabled": true,
"voteEnabled": true,
"socialShareEnabled": false,
"apiCall": null,
"embeddableScript": null,
"deleted": false,
"defaultViewTemplate": false
}
},
{
"_index": "local_rs_4_0_0_app_test_1552993787904",
"_type": "review",
"_id": "r_anuradhapura_1552994964151",
"_score": 1,
"_source": {
"modelVersion": "RS_4_0_0",
"reviewId": "r_anuradhapura_1552994964151",
"version": 1,
"status": "APPROVED",
"deleted": false,
"readyToProcess": true,
"appKey": "app_test_1552993787904",
"appName": "test",
"createdUser": "anonymous-user#app_test_1552993787904.com",
"displayName": "Oshana-1",
"createdTime": 1552994964158,
"entityType": "tour",
"entityId": "Anuradhapura",
"entityName": "Anuradhapura",
"comment": "Test",
"title": "Test-2",
"parentReview": null,
"childReviews": [],
"numberData": [
{
"key": "overall",
"value": 5
}
],
"textData": [],
"imageData": [],
"numberMeta": [],
"textMeta": [
{
"key": "user-agent",
"value": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
},
{
"key": "ip",
"value": "0:0:0:0:0:0:0:1"
},
{
"key": "source",
"value": "DIRECT"
},
{
"key": "country",
"value": "Afghanistan"
}
],
"replies": [],
"sentiments": [],
"scoredComment": [],
"rawNumberData": {
"overall": 5
},
"rawTextMeta": {
"country": "Afghanistan",
"ip": "0:0:0:0:0:0:0:1",
"source": "DIRECT",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
},
"rawNumberMeta": {},
"rawScoredComment": {},
"pvCount": 0,
"nvCount": 0
}
}
]
}
}
when you want to return just some specific stuff, your elasticsearch query should contain "_source" : "[fields you desire]" https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

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