Unwind Returns null Object in Spring Mongodb - spring

I want to group all documents in the collection rooms_booked by multiple fields ( startdate,enddate,user.id and house.id) but i'm getting an empty array when i use Unwind.
This is an example of the documents in the collection Rooms_booked :
{
"_id": {
"$oid": "5f071b74140d397a78c9c726"
},
"startdate": {
"$date": "2020-07-14T15:00:00.000Z"
},
"enddate": {
"$date": "2020-07-16T11:15:00.000Z"
},
"house": {
"$ref": "houses",
"$id": {
"$oid": "5efdf2a7fbe1ed5e11e1b08b"
}
},
"user": {
"$ref": "users",
"$id": {
"$oid": "5e60ff0167e87e569fafc15e"
}
},
"room": {
"$ref": "rooms",
"$id": {
"$oid": "5efdf2a7fbe1ed5e11e1b08d"
}
},
"created": {
"$date": "2020-07-09T13:28:20.604Z"
},
"_class": "com.guestnhousebackpfe.models.RoomsBooked"
}
I tried with this function :
Aggregation aggregate = newAggregation(
unwind("house"),
unwind("house.id"),
match(Criteria.where("house.id").is(idHouse)),
sort(Sort.Direction.DESC, "created"),
group("startdate","enddate").push("$$ROOT").as("roomsbooked"),
project("roomsbooked").previousOperation()
);
AggregationResults<RoomsBookedDTO> groupResults = mongoOperations.aggregate(aggregate, "rooms_booked", RoomsBookedDTO.class);
List<RoomsBookedDTO> result = groupResults.getMappedResults();
return result;
It returns empty array.
How can i fix this?
EDIT :
These are 3 documents from my collection
{
_id:5ee3a8b68caa2837ac3f04fc
priceperday:48
priceperdayweekend:0
price:1764
startdate:2020-06-22T15:00:00.000+00:00
enddate:2020-06-27T11:15:00.000+00:00
house:DBRef(houses, 5efdf2a7fbe1ed5e11e1b08b, undefined)
user:DBRef(users, 5e70bb248574d576731fc6d5, undefined)
room:DBRef(rooms, 5efdf2a7fbe1ed5e11e1b08d, undefined)
created:2020-06-12T16:09:26.506+00:00
modified:2020-07-06T15:04:42.962+00:00
_class:"com.guestnhousebackpfe.models.RoomsBooked"
},
{
_id:5ee3a8b68caa2837ac3f04fe
priceperday:48
priceperdayweekend:0
price:1764
startdate:2020-06-22T15:00:00.000+00:00
enddate:2020-06-27T11:15:00.000+00:00
house:DBRef(houses, 5efdf2a7fbe1ed5e11e1b08b, undefined)
user:DBRef(users, 5e70bb248574d576731fc7t2, undefined)
room:DBRef(rooms, 5efdf2a7fbe1ed5e11e1b08d, undefined)
created:2020-06-12T16:09:26.506+00:00
modified:2020-07-06T15:04:42.962+00:00
_class:"com.guestnhousebackpfe.models.RoomsBooked"
},
{
_id:5ee3a8b68caa2837ac3f04fk
priceperday:48
priceperdayweekend:0
price:1764
startdate:2020-06-22T15:00:00.000+00:00
enddate:2020-06-27T11:15:00.000+00:00
house:DBRef(houses, 5efdf2a7fbe1ed5e11e1b08b, undefined)
user:DBRef(users, 5e70bb248574d576731fc6d5, undefined)
room:DBRef(rooms, 5efdf2a7fbe1ed5e11e1b08f, undefined)
created:2020-06-12T16:09:26.506+00:00
modified:2020-07-06T15:04:42.962+00:00
_class:"com.guestnhousebackpfe.models.RoomsBooked"
},
Output
I Want to group the documents by the id of the user ,startdate and enddate.
{
"user" : "5e70bb248574d576731fc6d5",
"roomsbooked" : [
{
_id:5ee3a8b68caa2837ac3f04fc
priceperday:48
priceperdayweekend:0
price:1764
startdate:2020-06-22T15:00:00.000+00:00
enddate:2020-06-27T11:15:00.000+00:00
house:DBRef(houses, 5efdf2a7fbe1ed5e11e1b08b, undefined)
user:DBRef(users, 5e70bb248574d576731fc6d5, undefined)
room:DBRef(rooms, 5efdf2a7fbe1ed5e11e1b08d, undefined)
created:2020-06-12T16:09:26.506+00:00
modified:2020-07-06T15:04:42.962+00:00
_class:"com.guestnhousebackpfe.models.RoomsBooked"
},
{
_id:5ee3a8b68caa2837ac3f04fk
priceperday:48
priceperdayweekend:0
price:1764
startdate:2020-06-22T15:00:00.000+00:00
enddate:2020-06-27T11:15:00.000+00:00
house:DBRef(houses, 5efdf2a7fbe1ed5e11e1b08b, undefined)
user:DBRef(users, 5e70bb248574d576731fc6d5, undefined)
room:DBRef(rooms, 5efdf2a7fbe1ed5e11e1b08f, undefined)
created:2020-06-12T16:09:26.506+00:00
modified:2020-07-06T15:04:42.962+00:00
_class:"com.guestnhousebackpfe.models.RoomsBooked"
}
]
},
{
"user":"5e70bb248574d576731fc7t2",
"roomsbooked":[
{
_id:5ee3a8b68caa2837ac3f04fe
priceperday:48
priceperdayweekend:0
price:1764
startdate:2020-06-22T15:00:00.000+00:00
enddate:2020-06-27T11:15:00.000+00:00
house:DBRef(houses, 5efdf2a7fbe1ed5e11e1b08b, undefined)
user:DBRef(users, 5e70bb248574d576731fc7t2, undefined)
room:DBRef(rooms, 5efdf2a7fbe1ed5e11e1b08d, undefined)
created:2020-06-12T16:09:26.506+00:00
modified:2020-07-06T15:04:42.962+00:00
_class:"com.guestnhousebackpfe.models.RoomsBooked"
} ]
}

Related

Bucket_selector on nested agg bucket doesn't works

I'm trying to make an aggregation on a sibling's children aggregation to filter bucket based on a requested quantity condition, so here is my query :
GET _search
{
"size": 0,
"query": {
"bool": {
"must": [
{
"terms": {
"product.id": [20,21,22,23,24]
}
}
]
}
},
"aggs": {
"carts": {
"terms": {
"field": "item.cart_key"
},
"aggs": {
"unique_product": {
"terms": {
"field": "product.id"
},
"aggs": {
"quantity": {
"sum": {
"field": "item.quantity"
}
}
}
},
"filtered_product_quantity": {
"bucket_selector": {
"buckets_path": {
"productId": "unique_product.key",
"productQuantity": "unique_product>quantity"
},
"script": {
"params": {
"requiredQuantities": {
"20": null,
"21": null,
"22": null,
"23": 3,
"24": null
}
},
"lang": "painless",
"source": "params.requiredQuantities[params.productId] <= params.productQuantity"
}
}
}
}
}
}
}
And the error :
{
"error": {
"root_cause": [],
"type": "search_phase_execution_exception",
"reason": "",
"phase": "fetch",
"grouped": true,
"failed_shards": [],
"caused_by": {
"type": "aggregation_execution_exception",
"reason": "buckets_path must reference either a number value or a single value numeric metric aggregation, got: [Object[]] at aggregation [unique_product]"
}
},
"status": 500
}
Here is a sample document set :
[
{
product.id: 12,
item.cart_key: abc_123,
item.quantity: 2
},
{
product.id: 11,
item.cart_key: abc_123,
item.quantity: 1
},
{
product.id: 23,
item.cart_key: def_456,
item.quantity: 1
}
]
Is it the appropriate aggregation in use ?
In other way, I would like to :
Aggregate my documents by cart_key.
Per product.id , sum the quantity
Filter aggregations that have a quantity higher than a given Record object {[product.id]: minimum_quantity} (here is the requiredQuantities param
I don't know if the source script will works as elasticsearch can't reach it.
I don't think you handle the problem correctly, but here an hacky working solution
{
"size": 0,
"aggs": {
"carts": {
"terms": {
"field": "item.cart_key"
},
"aggs": {
"unique_product": {
"terms": {
"field": "product.id"
},
"aggs": {
"quantity": {
"sum": {
"field": "item.quantity"
}
},
"hackProductId": {
"max": {
"field": "product.id"
}
},
"filtered_product_quantity": {
"bucket_selector": {
"buckets_path": {
"productQuantity": "quantity",
"productId": "hackProductId"
},
"script": {
"params": {
"requiredQuantities": {
"12": 0,
"11": 0,
"22": 0,
"23": 0,
"24": 0
}
},
"lang": "painless",
"source": "params.requiredQuantities[((int)params.productId).toString()] <= params.productQuantity"
}
}
}
}
}
}
}
}
}

Elasticsearch - aggregation and counting elements from array

I'm learning Elasticsearch and I have a problem with aggregating and counting elements. Let's say we have our data like this:
{
"sessionId": "session1",
"messages": [
{ "intent": "intentX", "message": "foo1" },
{ "intent": "intentY", "message": "foo2" },
{ "intent": "intentY", "message": "foo3" },
{ "intent": "intentZ", "message": "foo4" }
]
},
{
"sessionId": "session2",
"messages": [
{ "intent": "intentX", "message": "foo5" },
{ "intent": "intentY", "message": "foo6" },
{ "intent": "intentY", "message": "foo7" },
{ "intent": "intentY", "message": "foo8" }
]
}
How can I count how many times each intent occurred? I've tried the query as follows but seems like it returns invalid values - not sure how is it counted now. All mappings are default.
{
"size": 0,
"aggs": {
"intents": {
"terms": {
"field": "messages.intent.keyword"
},
"aggs" : {
"count" : {
"value_count" : {
"field" : "messages.intent.keyword"
}
}
}
}
}
}
The result for the query:
{
"took":2,
"timed_out":false,
"_shards":{
"total":1,
"successful":1,
"skipped":0,
"failed":0
},
"hits":{
"total":{
"value":2,
"relation":"eq"
},
"max_score":null,
"hits":[
]
},
"aggregations":{
"intents":{
"doc_count_error_upper_bound":0,
"sum_other_doc_count":0,
"buckets":[
{
"key":"intentX",
"doc_count":2,
"count":{
"value":5
}
},
{
"key":"intentY",
"doc_count":2,
"count":{
"value":5
}
},
{
"key":"intentZ",
"doc_count":1,
"count":{
"value":3
}
}
]
}
}
}
The result I want to have is intentX.count.value = 2, intentY.count.value = 5 and intentZ.count.value = 1, so sum of intents from every array including duplicates.
Thanx for help
I've got it - I had to set "messages" field as "nested" in mapping, and then use this query to get what I wanted:
{
"size": 0,
"aggs": {
"intents": {
"nested": {
"path": "messages"
},
"aggs": {
"count": {
"terms": {
"field": "messages.intent.keyword"
}
}
}
}
}
}
This gave me the desired result.

Elasticsearch - how to filter over a list of nested objects

I have a list of object and I need to get only the objects based on the value of his children, for example, given the example below how can I retrieve only the people that have some children with age greater than a given age parameter?
hits:[
person:{
value1: "something",
value2: "something",
children: [
{age:18},
{age:20}
]
}]
I have tried something with query context but without success:
{
"min_score": 0.1,
"query": {
"function_score": {
"functions": [
{
"script_score": {
"script": {
"source": """
if (params["_source"]["children"][0]['age'] === params.age) {
return 1;
}else{
return 0;
""",
"params": {
"age": "20"
}
}
}
}
]
}
}
}
You'll need a nested data type mapping and a nested query to go along with it.
Rudimentary mapping:
PUT persons
{
"mappings": {
"properties": {
"person": {
"type": "object",
"properties": {
"children": {
"type": "nested"
}
}
}
}
}
}
An inserted person:
POST persons/_doc
{
"person": {
"value1": "something",
"value2": "something",
"children": [
{
"age": 18
},
{
"age": 20
}
]
}
}
Searching for children of the exact age of 20:
GET persons/_search
{
"query": {
"nested": {
"path": "person.children",
"query": {
"term": {
"person.children.age": {
"value": 20
}
}
}
}
}
}
You could also use a range query with gte == lte == 20.

Elasticsearch query for nested field existence

I have the following document saved in the Elasticsearch:
{
"transactionGroup": [
{
"name": "Test123",
"groups": [
{
"name": "Grp1",
"groups": [
{
"name": "g1",
"groups": [
{
"name": "g11",
"columns": {
"cpm": {
"newValue": "0"
},
"imps": {
"newValue": "0"
}
}
},
{
"name": "g12",
"columns": {
"cpm": {
"newValue": "0"
},
"imps": {
"newValue": "0"
}
}
}
],
"columns": {
"period": {
"newValue": "3Q"
},
"channelid": {
"newValue": "rQ"
}
}
},
{
"name": "g2",
"groups": [
{
"name": "g21",
"columns": {
"cpm": {
"newValue": "0"
},
"imps": {
"newValue": "0"
}
}
},
{
"name": "g22",
"columns": {
"cpm": {
"newValue": "0"
},
"imps": {
"newValue": "0"
}
}
}
],
"columns": {
"period": {
"newValue": "3Q"
},
"channelid": {
"newValue": "rQ"
}
}
}
],
"columns": {
"entityid": {
"newValue": "3292"
}
}
}
],
"columns": {
"lastmodifieddate": {
"newValue": "2020-05-18 07:29:54"
},
"lastmodifiedby": {
"newValue": "ajay"
}
}
}
]
}
I want following query.
How can I execute an exists query for:
If a group having name="g1", nested name="g11" having column field "cpm.newvalue" ?
I was trying something like:
"bool" : {
"should" : [
{
"exists" : {
"field" : "*transactionGroup.groups.groups.groups.columns.cpm.newValue",
"boost" : 1.0
}
},
But it's not working in all cases.
Here is the mapping:
{"unifiedplanner":{"_all":{"enabled":true,"norms":false},"properties":{"transactionGroup":{"properties":{"columns":{"properties":{"calendarid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"channel":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"class":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createdby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"dealyearid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"demo":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"enddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"endperiodid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"entityid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"equivalised":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"exceptions":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"exclusivity":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"fluidity":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"guaranteed":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"hiatus":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifiedby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifieddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"oldValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"marketplaceid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"maxlineid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"ordertypeid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"periodbreakdown":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planclassid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planname":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"plannotes":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"platform":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"ratecard":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"ratingstreamid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"startdate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"startperiodid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"type":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"version":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}}}},"groups":{"properties":{"columns":{"properties":{"$oid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"address":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"asofdate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"brandid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"categoryid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"channelid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"commission":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"contactid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"corporate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createdby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"daysofweek":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"demo":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"divisionid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"downstreamplanstatusid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"enddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"endtime":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"entityid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"externalid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"extsourceid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"gender":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"guaranteedcalculated":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"id":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifiedby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifieddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"mode":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"name":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"notes":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optionabledollartotal":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optionablepercenttotal":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optiondata":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planhasnooptions":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"primary":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"revision":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"role":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"roles":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"seccategoryid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"secondary":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"self":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"separationtype":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"split":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"startdate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"starttime":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"statusid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"oldValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"statusname":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"oldValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"subdivisionid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"subjecttooptiontotal":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"teamid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"title":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"username":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"version":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}}}},"groups":{"properties":{"columns":{"properties":{"$oid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"channelid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"channelname":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"class":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"cpm":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createdby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"createddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"daysinadvance":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"daysofweek":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"dollar":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"externalid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"externalsourcesystems":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"extsourceid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"id":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"imps":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifiedby":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"lastmodifieddate":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"oldValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"name":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optionabledollar":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"optionablepercent":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"period":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"planclass":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"platform":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"programid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"statusid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"statusname":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"subjecttooption":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}}}},"groups":{"properties":{"columns":{"properties":{"$oid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"cpm":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"dollar":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"externalid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"extsourceid":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"id":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"imps":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}},"name":{"properties":{"newValue":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"type":{"type":"long"}}}}},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}},"transactionSummary":{"properties":{"auditID":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"binLogTransactionId":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"databaseType":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"modifiedTime":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"moduleName":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"moduleTransactionId":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"rootEntityID":{"type":"keyword"},"schema":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"source":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"sourceTstamp":{"type":"long"},"userFullName":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"userName":{"type":"keyword"}}}}},"_default_":{"_all":{"enabled":true,"norms":false},"properties":{"transactionSummary":{"properties":{"rootEntityID":{"type":"keyword"},"userName":{"type":"keyword"}}}}}}
firstly you need to decide which type to assign to your field object or nested.
Example If you have an array with first names and last names
"users:[
{
"firstName":"a",
"lastName":"b"
},
{
"firstName":"x",
"lastName":"y"
}
]
In object type relationship between fields of array doesn't exist.
Suppose if you search for user whose firstName is "a" and last name "y". With object type above document will be returned (even though no such user exists) as objects are not treated as separate documents , you need to use nested type which is less performant than object.
If you decide to make our arrays as nested type, you need to use nested query to search on them
{
"query": {
"bool": {
"filter": {
"nested": {
"path": "groups",
"query": {
"bool": {
"must": [
{
"term": {
"groups.name": {
"value": "grp1"
}
}
},
{
"nested": {
"path": "groups.groups",
"query": {
"exists": {
"field": "groups.groups.field1"--> full name needed(full heirerchy)
}
}
}
}
]
}
}
}
}
}
}
}

how to get mongoDB documents in priority order

Is there any way to get documents from mongoDB in priority order.
the documents in mongo are indexed like this :
{
"productId":1
},
{
"productId":2
},
{
"productId":4
},
{
"productId":5
},
{
"productId":6
},
{
"productId":7
}
i need to get documents in order as term query order
i try this query but not work :
{
"productId": {
"$in": [
{
"$numberLong": "5"
},
{
"$numberLong": "7"
},
{
"$numberLong": "1"
},
{
"$numberLong": "4"
},
{
"$numberLong": "6"
},
{
"$numberLong": "2"
}
]
}
}
i expect result are documents sorted as term queries order :
{
"productId":5
},
{
"productId":7
},
{
"productId":1
},
{
"productId":4
},
{
"productId":6
},
{
"productId":2
}

Resources