Related
I want to write Elasticsearch query where I am able to look into a nested array, and check a key for all those elements inside the array, if all of them have that value then the root document should be selected.
this is a sample data :
[
{
"_index": "allproperties",
"_type": "_doc",
"_id": "5241050",
"_score": 0.0,
"_source": {
"id": 5241050,
"type": "HOUSE",
"state": "NSW",
"agency": {
"id": 31,
"code": "AU_LNT",
"name": "Luthor Properties",
"config": "{}"
},
"branch": {
"id": 89,
"name": "Luthor Sales Unit Trust",
"tradingName": "Luthor Properties",
"internalCode": "AU_LNT_G"
},
"images": null,
"leases": [
{
"id": 26439,
"bond": 2000,
"leased": true,
"source": "PORTAL",
"status": "SIGNED",
"endDate": "2022-11-06T00:00:00",
"tenants": [
{
"id": 11106,
"role": "TENANT",
"user": {
"id": 38817,
"dob": "1900-01-01",
"name": "Liam",
"email": "tempo#tempo.com",
"phone": "+61400000000",
"surname": "Tempo",
"salutation": "Mr.",
"invitations": null,
"lastInAppActivity": null
},
"address": "45675 Bruce Hwy, Coolbie QLD 4850, Australia",
"extendedData": "{\"rentSplitAmount\":22.62}",
"roleAdjective": "PRIMARY",
"addressComponents": {
"state": "QLD",
"suburb": "Coolbie",
"country": "Australia",
"postcode": "4850",
"streetName": "Bruce Hwy",
"unitNumber": null,
"poboxNumber": null,
"streetNumber": "45675",
"addressComponentsUnavailable": null
}
}
],
"signDate": "2022-10-05T04:58:08.887",
"startDate": "2022-10-06T00:00:00",
"moveInDate": "2022-10-06T00:00:00",
"appointments": [
{
"id": 3506,
"type": "REMOTE",
"date_time": "2022-10-12T04:56:00+00:00",
"createdBy": "Lex Luthor",
"createdDate": "2022-10-05T04:56:52.936",
"lastModifiedBy": "Lex Luthor",
"lastModifiedDate": "2022-10-05T04:56:53.51"
}
],
"createdDate": "2022-10-05T04:55:42.247",
"rentalAmount": 500,
"dateAvailable": null,
"extendedData": {
"last_rent_adjustment_date": 1665014400000
},
"leaseDuration": 32,
"rentalFrequency": "2",
"signedManually": false
}
],
"refId": "b66326eb-a6b2-42b6-b058-b46847e13399",
"source": "PT",
"status": null,
"suburb": "Hurstville",
"address": "456 Forest Road Hurstville NSW AUSTRALIA 2220",
"country": "Australia",
"bedrooms": 3,
"category": null,
"pmsCode": "84c34d15-a0ab-4791-b9e3-1bdac215b99c",
"postcode": "2220",
"agencyId": 31,
"bathrooms": 3,
"branchId": 89,
"carspaces": 3,
"createdBy": "system",
"streetname": "Forest Road",
"unitnumber": null,
"description": null,
"createdDate": "2022-10-05T04:51:12.619",
"entitlements": [
{
"id": 3453799,
"role": "LANDLORD",
"user": {
"id": 22855,
"dob": null,
"name": "please enter ownership name",
"email": "mag.m#rr.com.au",
"phone": "0400000000",
"surname": "",
"salutation": null,
"lastInAppActivity": null
},
"company": {
"id": 137,
"abn": "",
"acn": "",
"phone": "0400000000",
"address": "1234 Park Avenue New York NY USA 10037-1702",
"companyName": "please enter ownership name",
"displayName": "please enter ownership name",
"email": "mag.m#rr.com.au"
},
"roleAdjective": "GROUP"
},
{
"id": 3453800,
"role": "AGENT",
"user": {
"id": 20054,
"dob": null,
"name": "Paul",
"email": "p.b#mr.com",
"phone": "+61403084232",
"surname": "Botti",
"salutation": null,
"lastInAppActivity": null
},
"company": null,
"roleAdjective": "MANAGING"
},
{
"id": 3453801,
"role": "AGENT",
"user": {
"id": 20054,
"dob": null,
"name": "Paul",
"email": "p.b#mr.com",
"phone": "+61403084232",
"surname": "Botti",
"salutation": null,
"lastInAppActivity": null
},
"company": null,
"roleAdjective": "LEASING"
}
],
"streetnumber": "456",
"advertised": false,
"commission_type": null,
"lastModifiedBy": "system",
"lastModifiedDate": "2022-10-05T04:58:09.043",
"_meta": {
"branches": {
"id": [
89
]
},
"agencies": {
"id": [
31
]
},
"user_property_entitlement": {
"id": [
3453799,
3453800,
3453801
]
},
"users": {
"id": [
20054,
22855,
38817
]
},
"companies": {
"id": [
137
]
},
"leases": {
"id": [
26439
]
},
"user_lease_entitlement": {
"id": [
11106
]
},
"appointments": {
"id": [
3506
]
}
}
},
"sort": [
1664945472619,
0.0
]
}
]
In this particular case, leases is a nested document, I want to select the documents where all the leases are in cancelled state or leases is null, i.e; either all of the objects inside leases should have status as CANCELLED or the leases key should be null.
Already went through this question, but did'nt quite get it as its an old answer from 2015 and methods used in this got deprecated.
Try this query:
GET idx_test/_search?filter_path=hits.hits
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "leases",
"query": {
"bool": {
"must_not": [
{
"bool": {
"should": [
{
"term": {
"leases.status.keyword": "CANCELLED"
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "leases"
}
}
]
}
}
]
}
}
]
}
}
}
}
]
}
}
}
I want to use ELK stack to analyze some Kubernetes audit logs. They're sent to the Logstash webhook as JSON. Here's my config file :
input{
http {
port => 8888
codec => "json"
type => "json"
}
}
filter{
json {
source => "message"
}
}
output{
file {
path => "/home/ubuntu/logstash-kubernetes/audit.log"
}
elasticsearch{
hosts => "localhost:9200"
index => "kubernetes"
}
}
The output file is populated with good looking entries, I'm happy with it. But when it comes to exporting to Elasticsearch, I'm getting the following error :
elasticsearch - Could not index event to Elasticsearch. {:status=>400, :action=>["index", {:_id=>nil, :_index=>"kubernetes", :routing=>nil, :_type=>"_doc"}, #<LogStash::Event:0x71740f4b>], :response=>{"index"=>{"_index"=>"kubernetes", "_type"=>"_doc", "_id"=>"x6QKJHYBkIKyNOo2Q-z8", "status"=>400, "error"=>{"type"=>"mapper_parsing_exception", "reason"=>"failed to parse", "caused_by"=>{"type"=>"array_index_out_of_bounds_exception", "reason"=>"Index -1 out of bounds for length 0"}}}}}
It only happens for some entries. Let me show you an generated entry that was processed without error :
{
"_index": "kubernetes",
"_type": "_doc",
"_id": "xKQKJHYBkIKyNOo2POwJ",
"_version": 1,
"_score": 0,
"_source": {
"headers": {
"content_type": "application/json",
"request_method": "POST",
"accept_encoding": "gzip",
"request_path": "/",
"http_host": "****:8888",
"http_accept": "application/json, */*",
"content_length": "2813",
"http_version": "HTTP/1.1",
"http_user_agent": "Go-http-client/1.1"
},
"#version": "1",
"host": "****",
"apiVersion": "audit.k8s.io/v1",
"items": [
{
"requestObject": {
"type": "Normal",
"firstTimestamp": "2020-12-02T15:20:19Z",
"involvedObject": {
"namespace": "kube-system",
"name": "kube-scheduler",
"apiVersion": "v1",
"resourceVersion": "2573",
"uid": "0ebf0ea5-f8c0-475a-9c9e-57674edefe8d",
"kind": "Endpoints"
},
"message": "****1_938d6a87-bccb-4ea0-bd6b-31fc27e48b7a became leader",
"eventTime": null,
"source": {
"component": "default-scheduler"
},
"reason": "LeaderElection",
"count": 1,
"reportingInstance": "",
"apiVersion": "v1",
"reportingComponent": "",
"lastTimestamp": "2020-12-02T15:20:19Z",
"metadata": {
"name": "kube-scheduler.164cef6d1f3ed55e",
"namespace": "kube-system",
"creationTimestamp": null
},
"kind": "Event"
},
"requestURI": "/api/v1/namespaces/kube-system/events",
"responseStatus": {
"code": 201,
"metadata": {}
},
"user": {
"groups": [
"system:authenticated"
],
"username": "system:kube-scheduler"
},
"annotations": {
"authorization.k8s.io/reason": "RBAC: allowed by ClusterRoleBinding \"system:kube-scheduler\" of ClusterRole \"system:kube-scheduler\" to User \"system:kube-scheduler\"",
"authorization.k8s.io/decision": "allow"
},
"stageTimestamp": "2020-12-02T15:20:19.090835Z",
"userAgent": "kube-scheduler/v1.19.4 (linux/amd64) kubernetes/d360454/scheduler",
"objectRef": {
"namespace": "kube-system",
"name": "kube-scheduler.164cef6d1f3ed55e",
"apiVersion": "v1",
"resource": "events"
},
"responseObject": {
"type": "Normal",
"firstTimestamp": "2020-12-02T15:20:19Z",
"involvedObject": {
"namespace": "kube-system",
"name": "kube-scheduler",
"apiVersion": "v1",
"resourceVersion": "2573",
"uid": "0ebf0ea5-f8c0-475a-9c9e-57674edefe8d",
"kind": "Endpoints"
},
"message": "****1_938d6a87-bccb-4ea0-bd6b-31fc27e48b7a became leader",
"eventTime": null,
"source": {
"component": "default-scheduler"
},
"reason": "LeaderElection",
"count": 1,
"reportingInstance": "",
"apiVersion": "v1",
"reportingComponent": "",
"lastTimestamp": "2020-12-02T15:20:19Z",
"metadata": {
"name": "kube-scheduler.164cef6d1f3ed55e",
"namespace": "kube-system",
"creationTimestamp": "2020-12-02T15:20:19Z",
"managedFields": [
{
"operation": "Update",
"fieldsType": "FieldsV1",
"time": "2020-12-02T15:20:19Z",
"manager": "kube-scheduler",
"apiVersion": "v1",
"fieldsV1": {
"f:source": {
"f:component": {}
},
"f:lastTimestamp": {},
"f:reason": {},
"f:firstTimestamp": {},
"f:type": {},
"f:involvedObject": {
"f:kind": {},
"f:namespace": {},
"f:name": {},
"f:apiVersion": {},
"f:uid": {},
"f:resourceVersion": {}
},
"f:message": {},
"f:count": {}
}
}
],
"resourceVersion": "2576",
"selfLink": "/api/v1/namespaces/kube-system/events/kube-scheduler.164cef6d1f3ed55e",
"uid": "573e4082-7c38-473c-9116-a114be97a47d"
},
"kind": "Event"
},
"level": "RequestResponse",
"requestReceivedTimestamp": "2020-12-02T15:20:19.021481Z",
"auditID": "cf291b3f-f5fa-4e43-a177-adf9cbfc0197",
"sourceIPs": [
"****"
],
"stage": "ResponseComplete",
"verb": "create"
}
],
"#timestamp": "2020-12-02T15:20:19.094Z",
"type": "json",
"metadata": {},
"kind": "EventList"
}
}
And here's the JSON for that error happens :
{
"headers": {
"content_type": "application/json",
"request_method": "POST",
"accept_encoding": "gzip",
"request_path": "/",
"http_host": "****:8888",
"http_accept": "application/json, */*",
"content_length": "2668",
"http_version": "HTTP/1.1",
"http_user_agent": "Go-http-client/1.1"
},
"#version": "1",
"host": "****",
"apiVersion": "audit.k8s.io/v1",
"items": [
{
"requestObject": {
"metadata": {
"name": "kube-scheduler",
"namespace": "kube-system",
"creationTimestamp": "2020-12-02T14:36:33Z",
"annotations": {
"control-plane.alpha.kubernetes.io/leader": "{\"holderIdentity\":\"****38d6a87-bccb-4ea0-bd6b-31fc27e48b7a\",\"leaseDurationSeconds\":15,\"acquireTime\":\"2020-12-02T15:18:52Z\",\"renewTime\":\"2020-12-02T15:18:52Z\",\"leaderTransitions\":67}"
},
"managedFields": [
{
"operation": "Update",
"fieldsType": "FieldsV1",
"time": "2020-12-02T15:18:23Z",
"manager": "kube-scheduler",
"apiVersion": "v1",
"fieldsV1": {
"f:metadata": {
"f:annotations": {
".": {},
"f:control-plane.alpha.kubernetes.io/leader": {}
}
}
}
}
],
"resourceVersion": "2531",
"selfLink": "/api/v1/namespaces/kube-system/endpoints/kube-scheduler",
"uid": "0ebf0ea5-f8c0-475a-9c9e-57674edefe8d"
},
"apiVersion": "v1",
"kind": "Endpoints"
},
"requestURI": "/api/v1/namespaces/kube-system/endpoints/kube-scheduler?timeout=10s",
"responseStatus": {
"code": 200,
"metadata": {}
},
"user": {
"groups": [
"system:authenticated"
],
"username": "system:kube-scheduler"
},
"annotations": {
"authorization.k8s.io/reason": "RBAC: allowed by ClusterRoleBinding \"system:kube-scheduler\" of ClusterRole \"system:kube-scheduler\" to User \"system:kube-scheduler\"",
"authorization.k8s.io/decision": "allow"
},
"stageTimestamp": "2020-12-02T15:18:52.067347Z",
"userAgent": "kube-scheduler/v1.19.4 (linux/amd64) kubernetes/d360454/leader-election",
"objectRef": {
"namespace": "kube-system",
"name": "kube-scheduler",
"apiVersion": "v1",
"resourceVersion": "2531",
"uid": "0ebf0ea5-f8c0-475a-9c9e-57674edefe8d",
"resource": "endpoints"
},
"responseObject": {
"metadata": {
"name": "kube-scheduler",
"namespace": "kube-system",
"creationTimestamp": "2020-12-02T14:36:33Z",
"annotations": {
"control-plane.alpha.kubernetes.io/leader": "{\"holderIdentity\":\"****1_938d6a87-bccb-4ea0-bd6b-31fc27e48b7a\",\"leaseDurationSeconds\":15,\"acquireTime\":\"2020-12-02T15:18:52Z\",\"renewTime\":\"2020-12-02T15:18:52Z\",\"leaderTransitions\":67}"
},
"managedFields": [
{
"operation": "Update",
"fieldsType": "FieldsV1",
"time": "2020-12-02T15:18:52Z",
"manager": "kube-scheduler",
"apiVersion": "v1",
"fieldsV1": {
"f:metadata": {
"f:annotations": {
".": {},
"f:control-plane.alpha.kubernetes.io/leader": {}
}
}
}
}
],
"resourceVersion": "2542",
"selfLink": "/api/v1/namespaces/kube-system/endpoints/kube-scheduler",
"uid": "0ebf0ea5-f8c0-475a-9c9e-57674edefe8d"
},
"apiVersion": "v1",
"kind": "Endpoints"
},
"level": "RequestResponse",
"requestReceivedTimestamp": "2020-12-02T15:18:52.062449Z",
"auditID": "a45860c7-e0c9-4724-be2e-705a1b2955f4",
"sourceIPs": [
"****"
],
"stage": "ResponseComplete",
"verb": "update"
}
],
"#timestamp": "2020-12-02T15:20:22.001Z",
"type": "json",
"metadata": {},
"kind": "EventList"
}
(I removed some personnal data from the JSONs)
Both JSONs appears as valid. I noticed that the JSONs with responseStatus.code = 201 were correctly processed, and those with code = 200 were throwing this error.
I'm getting a bit mad so if anyone can help with that, I'd be very pleased !
The error is thrown on this line because subfields has a length of 0 and hence it is trying to access index -1, which is not allowed.
The method that splits the field names into pathsis called splitAndValidatePath and what it does is to split field names when it encounters . (i.e. a dot).
In your second document, you have two fields called "." (i.e. it is just a dot) and they are not really valid.
".": {},
If you remove them both, your document will be indexed correctly.
Another way of solving this is to disable indexing for the parent f:annotations object field and that will do the trick... provided, of course, you don't need to search for content within f:annotations.
I face same problem with fluentd.
As per #val suggestion. Fixed by below.
PUT /[index_name]/_mapping
{
"properties" : {
"metadata" : {
"properties" : {
"managedFields" : {
"properties" : {
"fieldsV1" : {
"properties" : {
"f:metadata" : {
"type" : "object",
"enabled": false
}
}
}
}
}
}
}
}
}
I only want to return this course if 'Grade' = 'G6' and Type = 'Open' are a match in the SAME audience tag, they must exist in the SAME tag to return this course. Currently this course is returned if it finds G6 and OPEN is DIFFERENT audiences which is not what I want.
This is not correct and i am getting incorrect data back, I need to query to apply in each audience and only return data if it is true in the same audience
here is my json:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 71,
"max_score": 3.3118114,
"hits": [
{
"_index": "courses",
"_type": "course",
"_id": "LBTBWdzyRw-jgiiYssjv8A",
"_score": 3.3118114,
"_source": {
"id": "LBTBWdzyRw-jgiiYssjv8A",
"title": "1503 regression testing",
"shortDescription": "asdf",
"description": "asdf",
"learningOutcomes": "",
"modules": [],
"learningProvider": {
"id": "ig2-zIY_QkSpMC4O0Lm0hw",
"name": null,
"termsAndConditions": [],
"cancellationPolicies": []
},
"audiences": [
{
"id": "VfDpsS_5SXi8iZubzTkUBQ",
"name": "comm",
"areasOfWork": [
"Communications"
],
"departments": [],
"grades": [
"G6"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "OPEN",
"eventId": null
},
{
"id": "eZPPPqTqRdiDAE3xCPlJMQ",
"name": "analysis",
"areasOfWork": [
"Analysis"
],
"departments": [],
"grades": [
"G6"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "REQUIRED",
"eventId": null
}
],
"preparation": "",
"owner": {
"scope": "LOCAL",
"organisationalUnit": "co",
"profession": 63,
"supplier": ""
},
"visibility": "PUBLIC",
"status": "Published",
"topicId": ""
}
}
]
}
}
My ES Code:
BoolQueryBuilder boolQuery = boolQuery();
boolQuery.should(QueryBuilders.matchQuery("audiences.departments.keyword", department));
boolQuery.should(QueryBuilders.matchQuery("audiences.areasOfWork.keyword", areaOfWork));
boolQuery.should(QueryBuilders.matchQuery("audiences.interests.keyword", interest));
BoolQueryBuilder filterQuery = boolQuery();
filterQuery.must(QueryBuilders.matchQuery("audiences.grades.keyword", "G6"));
filterQuery.must(QueryBuilders.matchQuery("audiences.type", "OPEN"));
Here is index mapping:
{
"media": {
"aliases": {}
},
"courses": {
"aliases": {}
},
"feedback": {
"aliases": {}
},
"learning-providers": {
"aliases": {}
},
"resources": {
"aliases": {}
},
"courses-0.4.0": {
"aliases": {}
},
".security-6": {
"aliases": {
".security": {}
}
},
"payments": {
"aliases": {}
}
}
Since you want your query to apply in each audience and only return data if it is true in the same audience, you need to specify nested datatype for audiences field otherwise ElasticSearch stores it in form of Objects and it doesnt have concept of nested objects because of which Elasticsearch flattens object hierarchies into a simple list of field names and values.You can refer this for more detail https://www.elastic.co/guide/en/elasticsearch/reference/current/nested.html
Taking your example suppose this was your document :
"audiences": [
{
"id": "1",
"field": "comm"
},
{
"id": "2",
"field": "arts"
}
]
Elasticsearch flattens in the form of :
{
"audiences.id":[1,2],
"audiences.field":[comm,arts]
}
Now here if you search query says that audience must have id:1 and field:arts then also above document will get matched.
So, in order to avoid this such type of objects should be defined as nested object. ElasticSearch will store each object separately instead of flattening it as a result each object will be searched separately.
Mapping of your above mentioned document should be :
Mapping
{
"mappings": {
"properties": {
"shortDescription": {
"type": "text"
},
"audiences": {
"type": "nested"
},
"description": {
"type": "text"
},
"modules": {
"type": "text"
},
"preparation": {
"type": "text"
},
"owner": {
"properties": {
"scope": {
"type": "text"
},
"organisationalUnit": {
"type": "text"
},
"profession": {
"type": "text"
},
"supplier": {
"type": "text"
}
}
},
"learningProvider": {
"properties": {
"id": {
"type": "text"
},
"name": {
"type": "text"
},
"termsAndConditions": {
"type": "text"
},
"cancellationPolicies": {
"type": "text"
}
}
},
"visibility": {
"type": "text"
},
"status": {
"type": "text"
},
"topicId": {
"type": "text"
}
}
}
}
Now, if we index this document :
Document
{
"shortDescription": "asdf",
"description": "asdf",
"learningOutcomes": "",
"modules": [],
"learningProvider": {
"id": "ig2-zIY_QkSpMC4O0Lm0hw",
"name": null,
"termsAndConditions": [],
"cancellationPolicies": []
},
"audiences": [
{
"id": "VfDpsS_5SXi8iZubzTkUBQ",
"name": "comm",
"areasOfWork": [
"Communications"
],
"departments": [],
"grades": [
"G6"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "OPEN",
"eventId": null
},
{
"id": "eZPPPqTqRdiDAE3xCPlJMQ",
"name": "analysis",
"areasOfWork": [
"Analysis"
],
"departments": [],
"grades": [
"G7"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "REQUIRED",
"eventId": null
}
],
"preparation": "",
"owner": {
"scope": "LOCAL",
"organisationalUnit": "co",
"profession": 63,
"supplier": ""
},
"visibility": "PUBLIC",
"status": "Published",
"topicId": ""
}
If you search query is this :
Search Query 1
:
{
"query": {
"nested": {
"path": "audiences",
"query": {
"bool": {
"must": [
{
"match": {
"audiences.type.keyword": "OPEN"
}
},
{
"match": {
"audiences.grades.keyword": "G6"
}
}
]
}
}
}
}
}
Result
"hits": [
{
"_index": "product",
"_type": "_doc",
"_id": "1",
"_score": 0.9343092,
"_source": {
"shortDescription": "asdf",
"description": "asdf",
"learningOutcomes": "",
"modules": [],
"learningProvider": {
"id": "ig2-zIY_QkSpMC4O0Lm0hw",
"name": null,
"termsAndConditions": [],
"cancellationPolicies": []
},
"audiences": [
{
"id": "VfDpsS_5SXi8iZubzTkUBQ",
"name": "comm",
"areasOfWork": [
"Communications"
],
"departments": [],
"grades": [
"G6"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "OPEN",
"eventId": null
},
{
"id": "eZPPPqTqRdiDAE3xCPlJMQ",
"name": "analysis",
"areasOfWork": [
"Analysis"
],
"departments": [],
"grades": [
"G7"
],
"interests": [],
"requiredBy": null,
"frequency": null,
"type": "REQUIRED",
"eventId": null
}
],
"preparation": "",
"owner": {
"scope": "LOCAL",
"organisationalUnit": "co",
"profession": 63,
"supplier": ""
},
"visibility": "PUBLIC",
"status": "Published",
"topicId": ""
}
}
]
But now if your search query is :
Search Query 2 :
{
"query": {
"nested": {
"path": "audiences",
"query": {
"bool": {
"must": [
{
"match": {
"audiences.type.keyword": "OPEN"
}
},
{
"match": {
"audiences.grades.keyword": "G7"
}
}
]
}
}
}
}
}
Result :
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
}
So, in short you need to change datatype of audiences field in your mapping and your rest query as well so that it can search for nested datatype.
So, instead of this code fragment :
BoolQueryBuilder filterQuery = boolQuery();
filterQuery.must(QueryBuilders.matchQuery("audiences.grades.keyword", "G6"));
filterQuery.must(QueryBuilders.matchQuery("audiences.type", "OPEN"));
you should use this nested query :
BoolQueryBuilder filterQuery = new BoolQueryBuilder();
filterQuery.must(QueryBuilders.matchQuery("audiences.grades.keyword", "G6"));
filterQuery.must(QueryBuilders.matchQuery("audiences.type", "OPEN"));
NestedQueryBuilder nested = new NestedQueryBuilder("audiences", filterQuery, ScoreMode.None);
sample data
[
{
"createdDate": 1508588333821,
"data": {
"image_extension": "png",
"name": "Golden",
"qty": 1,
"remark": "#296-2",
"status": "RETURN",
"owner": [
{
"name": "app1emaker",
"location": 1
},
{
"name": "simss92_lmao",
"location": 31
}
]
},
"deleted": false,
"docId": 307,
"docType": "product",
"id": "db0131f9-9359-4aa3-b6ed-cd9f3ff4aa3e",
"updatedDate": 1553155281691
},
{
"createdDate": 1508588333324,
"data": {
"image_extension": "png",
"name": "Golden",
"qty": 1,
"remark": "#296-2",
"status": "DISCARD",
"owner": [
{
"name": "At533",
"location": 7
},
{
"name": "madsimon",
"location": 64
},
{
"name": "boyboy96",
"location": 1
},
{
"name": "xinfengCN",
"location": 5
}
]
},
"deleted": false,
"docId": 308,
"docType": "product",
"id": "3790bdaa-5347-4ab0-8149-37332c23c6ea",
"updatedDate": 1554555231691
},
...
...
]
And said that, I would like to select the data.owner on array index 0 only (or I should say data.owner[0]), which are
{
"name": "app1emaker",
"location": 1
}
and
{
"name": "At533",
"location": 7
}
in this case. I have a failed code below.
r.db('carbon').table("items").pluck(['id', 'docId', 'createdDate',{data:{name: true, owner:[0]}}])
I saw that for some functions like orderBy, rethinkdb allowed to use orderBy(r.row('data')('owner')(0)('name')) for access nested object, but I have no idea how to do this for pluck? could anyone give me some hints?
Thanks a lot
pluck can not do that, but you can fall back to use the map:
r.db("carbon").table("items").map(function(doc){
return {
"id": doc("id"),
"docId": doc("docId"),
"createdDate": doc("createdDate"),
"data": {
"name": doc("data")("name"),
"owner": doc("data")("owner")(0)
}
}
})
I am trying to persist some objects that have a composite id. If I am only sending an array with one element it works fine, but it the array has more than one it throws an exception when saving the first one. public boolean
addParamsToChart(List<ChartParams> chartParams, Long chartId) {
List<ChartParams> chartParamsList = new ArrayList<>();
for(ChartParams chartParam: chartParams) {
ChartParamsId id = new ChartParamsId();
// id.setChartId(chartId);
id.setChartId(chartParam.getChart().getId());
id.setParamId(chartParam.getParam().getId());
id.setContextSourceId(chartParam.getContextSource().getId());
chartParam.setChartParamsId(id);
if(chartParamsRepository.save(chartParams) !=null) {
chartParamsList.add(chartParam);
}
}
if(chartParamsList.size()!=chartParams.size()) {
// something went wrong, delete previous inserted
deleteChartParams(chartParamsList);
chartRepository.delete(chartId);
return false;
}
return true;
}
[{
"chart": {
"id": 49,
"cv": {
"id": 1,
"name": "Money",
"category": {
"id": 1,
"name": "Euros"
},
"definition": "\"European curreny.\" [EU:euro]",
"enabled": true,
"cvid": "CC:1010"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Euro saving charts"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 3,
"name": "euro",
"internal": "eu",
"abbreviatedName": "eu"
} }]
But for this having two objects inside instead of one is not working, throwing an exception at the first save.
[{
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 6,
"name": "stock",
"internal": "st",
"abbreviatedName": "st"
} }, {
"chart": {
"id": 52,
"cv": {
"id": 55,
"name": "Stocks",
"category": {
"id": 1,
"name": "Stocks"
},
"definition": "\"General stocks.\" [GS:ST]",
"enabled": true,
"cvid": "ST:0111"
},
"accountType": {
"id": 1,
"name": "saving"
},
"name": "Stock saving chart"
},
"param": {
"id": 8,
"name": "Totals",
"isFor": "Currency"
},
"contextSource": {
"id": 7,
"name": "Sold stock",
"internal": "st_sold",
"abbreviatedSequence": "st_sold"
} }]
The exception I got is:
org.hibernate.id.IdentifierGenerationException: null id generated for:class eu.stocks.chart.chartParams.ChartParams