I am trying to update the document in elasticsearch which matches the id field. If the formData.id
matches any of the documents in elasticsearch, I want it's mobile_number
and primary_email_id
to be updated. I also want to insert a new field website
to the matching result. For this, I tried the following query:
function updateForm(formData) {
var deferred = Q.defer();
client.update({
index: 'collections',
type: 'automobiles',
id: '8s5WEbYZ6NUAliwOBf',
body: {
query: {
must: {
match : {
id: formData.id
}
},
},
doc: {
'mobile_number' : formData.phone,
'primary_email_id': formData.email,
'website' : formData.website
}
}
},function (error, response) {
if(error){
console.log(error);
return error;
}else {
deferred.resolve(response);
}
});
return deferred.promise;
}
This gives me the following error:
Error: [action_request_validation_exception] Validation Failed: 1: script or doc is missing;
I already have a doc field in my query. What modifications should I make to perform such an update? Is there any other better way for such an operation?
I'm not very familiar with the JS API but I think it should look this way:
function updateForm(formData) {
var deferred = Q.defer();
client.update(
{
index: 'collections',
type: 'automobiles',
id: formData.id,
body: {
doc: {
'mobile_number' : formData.phone,
'primary_email_id': formData.email,
'website' : formData.website
}
}
},
function (error, response) {
if(error) {
console.log(error);
return error;
} else {
deferred.resolve(response);
}
}
);
return deferred.promise;
}
I've modified my query and it's working now. id
is the only unique field for each of the document in collections
. So, I had to include that as well in doc
. The updated function:
function putForm(formData) {
console.log(formData);
var deferred = Q.defer();
client.update({
index: 'collections',
type: 'automobile',
id: '8s5WEbYZ6NUAliwOBf',
body: {
doc: {
'id' : formData.id,
'mobile_number' : formData.mobile_number,
'primary_email_id': formData.primary_email_id,
'website' : formData.website,
}
}
},function (error, response) {
if(error){
console.log(error);
return error;
}else {
deferred.resolve(response);
}
});
return deferred.promise;
}
Here, id
outside the body field is id of collections
while formData.id
refers to individual documents present in the collection.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.