we are migrating to elastic search 8 and when we are trying to fetch the data of parent document inner hits using has parent query .elastic search returning exception when runnning innerhits for has parent query.
https://discuss.elastic.co/t/inner-hits-in-has-parent-giving-error-couldnt-find-nested-source-for-path-currentcompany/318232
I had the same problem
bellow query giving me same error:, I had to pick parent document by using a field instead of _id
GET /user_data_factory/_search?from=0&size=20
{
"query": {
"bool": {
"must": [
{
"match": {
"relation_type": "uinsp"
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must": [
{
"match": {
"userInspirer": "63bef9f9a8c98000126589eb"
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"has_parent": {
"parent_type": "user",
"query": {
"match": {
"_id": "63bd1ff29510390012760322"
}
},
"inner_hits": {
"_source": ["id"]
}
}
}]
}
}
]
}
}]
}
}
]
}
}
}
using field instead of _id
GET /user_data_factory/_search?from=0&size=20
{
"query": {
"bool": {
"must": [
{
"match": {
"relation_type": "uinsp"
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"bool": {
"must": [
{
"match": {
"userInspirer": "63bef9f9a8c98000126589eb"
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"has_parent": {
"parent_type": "user",
"query": {
"match": {
"id": "63bd1ff29510390012760322"
}
},
"inner_hits": {
"_source": ["id"]
}
}
}]
}
}
]
}
}]
}
}
]
}
}
}
So,suppose I have a document like :
{
"country": "USA",
"capital": "Washington DC",
"language": "English",
"other": {
"China": {
"capital": "Shanghai",
"language": "Chinese"
},
"Spain": {
"capital": "Madrid",
"language": "Spanish"
}
}
}
Now I want to return my document as:
{
"country": "USA",
"capital": "Washington DC",
"language": "English",
}
when I choose the country as "USA" but when I have the country set as "Spain", then I want my mongodb query to return the document as:
{
"country": "Spain",
"capital": "Madrid",
"language": "Spanish",
}
The fields should merge in accordance with the country specified in the "other" field.
How can I achieve this?
Try this one:
country = "Spain"
db.collection.aggregate([
// Make a uniform structure (why do you have 'USA' and 'other'?)
{ $replaceRoot: { newRoot: { $mergeObjects: ["$other", { USA: { capital: "$capital", language: "$language" } }] } } },
{ $project: { countries: { $objectToArray: "$$ROOT" } } },
{
$set: {
countries: {
$filter: {
input: "$countries",
cond: { $eq: ["$$this.k", country] }
}
}
}
},
{
$replaceRoot: {
newRoot: {
$mergeObjects: [
{ country: { $first: "$countries.k" } },
{ $first: "$countries.v" }]
}
}
}
])
I am using Jolt to transform data from
{
"Data": {
"ROOT": {
"MODIFIED_DATE": "2018-06-27T13:53:47.8",
"A1": [
{
"FLD1": "BB",
"A2": [
{
"FLD2": 1
}
]
},
{
"FLD1": "AA",
"A2": [
{
"FLD2": 2
}
]
}
]
}
},
"metaData": {
"FLD3": "5f3c4"
}
}
To
{
"modifiedDate": "2018-06-27T13:53:47.8",
"a1": [
{
"fld1": "BB",
"a2": [
{
"fld2": 1
}
]
},
{
"fld1": "AA",
"a2": [
{
"fld2": 2
}
]
}
],
fld3: "5f3c4"
}
My spec is
[
{
"operation": "shift",
"spec": {
"Data": {
"ROOT": {
"MODIFIED_DATE": "modifiedDate",
"A1": {
"*": {
"FLD1": "a1[&1]",
"A2": {
"*": {
"FLD2": "a2[&2].fld2"
}
}
}
}
}
},
"metaData": {
"FLD3": "fld3"
}
}
},
{
"operation": "default",
"spec": {}
}
]
But it doesn't work properly. What have i missed?
Figured it out.
[
{
"operation": "shift",
"spec": {
"Data": {
"ROOT": {
"MODIFIED_DATE": "modifiedDate",
"A1": {
"*": {
"FLD1": "a1[&1].fld1",
"A2": {
"*": {
"FLD2": "a1[&3].a2[&1].fld2"
}
}
}
}
}
},
"metaData": {
"FLD3": "fld3"
}
}
},
{
"operation": "default",
"spec": {}
}
]
i have following json string response received from the server.
{
"entities":[
{
"Fields":[
{
"Name":"detection-version",
"values":[
{
}
]
},
{
"Name":"subject",
"values":[
]
},
{
"Name":"description",
"values":[
{
}
]
},
{
"Name":"target-rcyc",
"values":[
{
}
]
},
{
"Name":"project",
"values":[
{
}
]
},
{
"Name":"changeset",
"values":[
{
}
]
},
{
"Name":"has-linkage",
"values":[
{
"value":"N"
}
]
},
{
"Name":"last-modified",
"values":[
{
"value":"2016-05-31 18:38:54"
}
]
},
{
"Name":"has-others-linkage",
"values":[
{
"value":"N"
}
]
},
{
"Name":"attachment",
"values":[
{
}
]
},
{
"Name":"cycle-id",
"values":[
]
},
{
"Name":"request-type",
"values":[
{
}
]
},
{
"Name":"build-detected",
"values":[
{
}
]
},
{
"Name":"creation-time",
"values":[
{
"value":"2016-05-17"
}
]
},
{
"Name":"actual-fix-time",
"values":[
]
},
{
"Name":"id",
"values":[
{
"value":"4"
}
]
},
{
"Name":"run-reference",
"values":[
]
},
{
"Name":"request-note",
"values":[
{
}
]
},
{
"Name":"request-server",
"values":[
{
}
]
},
{
"Name":"severity",
"values":[
{
"value":"1-Low"
}
]
},
{
"Name":"to-mail",
"values":[
{
}
]
},
{
"Name":"owner",
"values":[
{
}
]
},
{
"Name":"detected-by",
"values":[
{
"value":"demouser02"
}
]
},
{
"Name":"build-closed",
"values":[
{
}
]
},
{
"Name":"step-reference",
"values":[
]
},
{
"Name":"estimated-fix-time",
"values":[
]
},
{
"Name":"reproducible",
"values":[
{
}
]
},
{
"Name":"ver-stamp",
"values":[
{
"value":"1"
}
]
},
{
"Name":"request-id",
"values":[
]
},
{
"Name":"priority",
"values":[
{
}
]
},
{
"Name":"cycle-reference",
"values":[
{
}
]
},
{
"Name":"environment",
"values":[
{
}
]
},
{
"Name":"target-rel",
"values":[
{
}
]
},
{
"Name":"test-reference",
"values":[
]
},
{
"Name":"planned-closing-ver",
"values":[
{
}
]
},
{
"Name":"extended-reference",
"values":[
{
}
]
},
{
"Name":"dev-comments",
"values":[
{
}
]
},
{
"Name":"detected-in-rcyc",
"values":[
{
}
]
},
{
"Name":"closing-version",
"values":[
{
}
]
},
{
"Name":"name",
"values":[
{
"value":"test"
}
]
},
{
"Name":"has-change",
"values":[
{
}
]
},
{
"Name":"user-01",
"values":[
{
}
]
},
{
"Name":"has-changeset-link",
"values":[
{
"value":"N"
}
]
},
{
"Name":"detected-in-rel",
"values":[
{
}
]
},
{
"Name":"status",
"values":[
{
}
]
},
{
"Name":"closing-date",
"values":[
]
}
],
"Type":"defect",
"children-count":0
},
{
"Fields":[
{
"Name":"detection-version",
"values":[
{
}
]
},
{
"Name":"subject",
"values":[
]
},
{
"Name":"description",
"values":[
{
}
]
},
{
"Name":"target-rcyc",
"values":[
{
}
]
},
{
"Name":"project",
"values":[
{
}
]
},
{
"Name":"changeset",
"values":[
{
}
]
},
{
"Name":"has-linkage",
"values":[
{
"value":"N"
}
]
},
{
"Name":"last-modified",
"values":[
{
"value":"2016-05-31 18:38:58"
}
]
},
{
"Name":"has-others-linkage",
"values":[
{
"value":"N"
}
]
},
{
"Name":"attachment",
"values":[
{
}
]
},
{
"Name":"cycle-id",
"values":[
]
},
{
"Name":"request-type",
"values":[
{
}
]
},
{
"Name":"build-detected",
"values":[
{
}
]
},
{
"Name":"creation-time",
"values":[
{
"value":"2016-05-17"
}
]
},
{
"Name":"actual-fix-time",
"values":[
]
},
{
"Name":"id",
"values":[
{
"value":"5"
}
]
},
{
"Name":"run-reference",
"values":[
]
},
{
"Name":"request-note",
"values":[
{
}
]
},
{
"Name":"request-server",
"values":[
{
}
]
},
{
"Name":"severity",
"values":[
{
"value":"1-Low"
}
]
},
{
"Name":"to-mail",
"values":[
{
}
]
},
{
"Name":"owner",
"values":[
{
}
]
},
{
"Name":"detected-by",
"values":[
{
"value":"demouser02"
}
]
},
{
"Name":"build-closed",
"values":[
{
}
]
},
{
"Name":"step-reference",
"values":[
]
},
{
"Name":"estimated-fix-time",
"values":[
]
},
{
"Name":"reproducible",
"values":[
{
}
]
},
{
"Name":"ver-stamp",
"values":[
{
"value":"1"
}
]
},
{
"Name":"request-id",
"values":[
]
},
{
"Name":"priority",
"values":[
{
}
]
},
{
"Name":"cycle-reference",
"values":[
{
}
]
},
{
"Name":"environment",
"values":[
{
}
]
},
{
"Name":"target-rel",
"values":[
{
}
]
},
{
"Name":"test-reference",
"values":[
]
},
{
"Name":"planned-closing-ver",
"values":[
{
}
]
},
{
"Name":"extended-reference",
"values":[
{
}
]
},
{
"Name":"dev-comments",
"values":[
{
}
]
},
{
"Name":"detected-in-rcyc",
"values":[
{
}
]
},
{
"Name":"closing-version",
"values":[
{
}
]
},
{
"Name":"name",
"values":[
{
"value":"test"
}
]
},
{
"Name":"has-change",
"values":[
{
}
]
},
{
"Name":"user-01",
"values":[
{
}
]
},
{
"Name":"has-changeset-link",
"values":[
{
"value":"N"
}
]
},
{
"Name":"detected-in-rel",
"values":[
{
}
]
},
{
"Name":"status",
"values":[
{
}
]
},
{
"Name":"closing-date",
"values":[
]
}
],
"Type":"defect",
"children-count":0
}
],
"TotalResults":920
}
i need to update the following values for all defects in this string
{
"Name":"detected-by",
"values":[
{
"value":"demouser02"
}
]
}
i need to update detected by to "Reported by" and demouser02 to "Adwait"
Please help.
JSONSimple. AS Simple as it gets. Try it! https://www.mkyong.com/java/json-simple-example-read-and-write-json/
If I understood correctly what you mean, you can do somthing like this :
public class EntiteFile extends Serializable{
private List<Entite<Filed>> entites;
....
}
public class Entite<Filed> extends Serializable {
private List<Filed> fileds;
}
public clas Value extends Serializable {
private String value;
....
}
public class Filed extends Serializable {
private String name;
private List<Value> values ;
.....
}
and to read you're file you can use :
EntiteFile entities = mapper.readValue(getJsonFile(nameFile), EntiteFile.class);
for(Entite entite : entities.getEntites ){
for(Field field : Fields ){
if("detected-by".equals(field.getName())){
field.setName("Reported by");
for(Value value : field.getValues){
value.setValue("Adwait");
}
}
}
}
...
...