Documents are not deleted when using the Javascript APIs deleteByQuery()? - javascript

Using deleteByQuery() in the Javascript API I would like to delete some documents from an index called people.
It might be important to note that this is version 5.1 in AWS Elastic Search Service.
The index mapping looks like this:
"people": {
"mappings": {
"person": {
"properties": {
"name": {
"type": "string"
},
"parent_id": {
"type": "long"
},
"query": {
"properties": {
"match": {
"properties": {
"parent_id": {
"type": "long"
}
}
},
"term": {
"properties": {
"parent_id": {
"type": "long"
}
}
}
}
}
}
}
}
}
And a search query that is structured as follows:
query: {
term: {
parent_id: 1
}
}
Responds with:
"hits": {
"total": 1,
"max_score": 10.135444,
"hits": [
{
"_index": "people",
"_type": "person",
"_id": "AVp6EDRVm933W5mwl4O9",
"_score": 10.135444,
"_source": {
"name": "Bob",
"parent_id": 1
}
}
]
}
So then using the documentation for the JS API I have created the following:
const elasticsearch = require('elasticsearch')
function deletePeople (parentId) {
new elasticsearch
.Client({ host: 'es.host.domain' })
.deleteByQuery({
index: 'people',
type: 'person',
body: {
query: {
term: {
parent_id: parentId
}
}
}
}, function afterDeletingPeople (error, response) {
if (error) {
return console.log("deletePeople :: Error :: ", error);
}
else {
return console.log("deletePeople :: Response :: ", response);
}
});
};
deletePeople(1);
Which gives a response like:
info: deletePeople :: Response :: { _index: 'people',
_type: 'person',
_id: '_delete_by_query',
_version: 30,
_shards: { total: 2, successful: 1, failed: 0 },
created: false }
And that looks hopeful but no people are actually removed from the index. I tried to use match instead of term in the query but that does the same thing.
Could anyone suggest where I have gone wrong here with this as currently my function deletes no people? TIA!

Related

What value do I insert inside the 'Body' parameter of my API request according to this API Specification?

I'm trying to make a data request from a URL based on the data provider's API specification.
My app is built using the Node.JS and NPM.
I'm subscribed to use the API and I've verified that all my identity credentials, policies and IAM keys that give me permission to access the API.
Could someone please help me define the correct value to place inside the 'Body' parameter of my API request based on the API specification provided by the URL.
Thanks!
My current request parameter code looks like below:
const request_parameter = {
DataSetId: "xxxxxac09b4xxxxxxxxxx",
RevisionId: "xxxxxxc8df94fxxxxxxx",
AssetId: "xxxxxxx2bde1fa3xxxxxxx",
Method: "POST",
Path: "/pxxx/graphql",
QueryStringParameters: {
param1: " ",
param2: " "
},
RequestHeaders: {
"Content-Type": "application/json"
},
Body: {\"body_param\":\"body_param_value\"} // FIX ME
};
The data provider's actual API specification looks like below:
{
"openapi": "3.0.1",
"info": {
"title": "image-services",
"description": "API Gateway for integration of Platform Services into AWS Data Exchange",
"version": "2022-03-03T20:04:18Z"
},
"servers": [
{
"url": ""
}
],
"paths": {
"/psdm/graphql": {
"post": {
"requestBody": {
"description": "GraphQL query for the Catalog in the form of:\n\n `{\n availablemetadatarecord(\n QUERYPARAMS\n ) { \n FIELDLIST\n } \n }`\n",
"content": {
"application/graphql": {
"schema": {
"$ref": "#/components/schemas/CatalogSearchRequest"
},
"examples": {
"Limit returned data": {
"description": "Limit the number of records in a response.\n\nIn your query, simply specify `limit: <int>` \n",
"value": "{\n availablemetadatarecord(\n limit: 10\n content: { catalogProperties: { vendor: { eq: \"EYE\" } } }\n ) {\n assignedId\n }\n}\n"
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "200 response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/CatalogSearchResponse"
}
}
}
}
},
"security": [
{
"sigv4": []
}
]
}
}
},
"components": {
"schemas": {
"CatalogSearchRequest": {
"title": "CatalogSearchRequest",
"type": "object"
},
"CatalogSearchResponse": {
"title": "Catalog Search Response",
"required": [
"data",
"paginationInfo"
],
"type": "object",
"properties": {
"paginationInfo": {
"type": "object",
"properties": {
"recordsInPage": {
"type": "integer"
},
"nextOffset": {
"type": "integer"
}
}
},
"data": {
"type": "object",
"properties": {
"availablemetadatarecord": {
"type": "array",
"description": "Records returned by query",
"items": {
"type": "object"
}
}
}
}
}
}
},
"securitySchemes": {
"sigv4": {
"type": "apiKey",
"name": "Authorization",
"in": "header",
"x-amazon-apigateway-authtype": "awsSigv4"
}
}
}
}

Issue while updating nested data createEntityAdapter RTK query

I am working on the chat that is utilising RTK Query with entity adapters. We created nested normalised createEntityAdapter . I want to update the message key from onCacheEntryAdded, but not able to update nested object. I'm attaching the query endpoint code along with sample response.
RTK Query Endpoint
endpoints: builder => ({
getMessages: builder.query({
query: ({ orderId, userId, channel }) => {
return {
url: `/messages?orderId=${orderId}&userId=${userId}`,
method: 'GET',
}
},
transformResponse(response, meta, arg) {
return messagesAdapter.setAll(messagesAdapter.getInitialState(), [
{
id: `${arg.orderId}-${arg.userId}`,
isTyping: false,
channel: arg.channel,
orderId: arg.orderId,
messages: messagesAdapter.addMany(
messagesAdapter.getInitialState(),
response,
),
},
])
},
async onCacheEntryAdded(
arg,
{ updateCachedData, cacheDataLoaded, cacheEntryRemoved, cacheData },
) {
try {
await cacheDataLoaded
updateCachedData(draft => {
messagesAdapter.updateOne(draft, {
id: '62d529f60be7549be1f53df3-62d64eef37d10fea52e1ce72',
changes: {
messages: messagesAdapter.updateOne(
messagesAdapter.getInitialState(),
{
id: '62da7221bc0eb3a259fc2256',
changes: {
message: 'updated message',
},
},
),
},
})
})
} catch {
// no-op in case `cacheEntryRemoved` resolves before `cacheDataLoaded`,
// in which case `cacheDataLoaded` will throw
}
await cacheEntryRemoved
},
providesTags: (result, error, arg) => {
console.log('providesTags', result, error, arg)
return ['chat']
},
}),
}),
Sample response
{
"ids": [
"62d529f60be7549be1f53df3-62d64eef37d10fea52e1ce72"
],
"entities": {
"62d529f60be7549be1f53df3-62d64eef37d10fea52e1ce72": {
"id": "62d529f60be7549be1f53df3-62d64eef37d10fea52e1ce72",
"isTyping": false,
"channel": "62d7d9266599a9f0c3bcdaba",
"orderId": "62d529f60be7549be1f53df3",
"messages": {
"ids": [
"62da7221bc0eb3a259fc2256",
"62da724dbc0eb3a259fc2260",
"62da75559f7a23f89a0958d4",
"62da94e59f7a23f89a0958ed"
],
"entities": {
"62da7221bc0eb3a259fc2256": {
"order": "62d529f60be7549be1f53df3",
"alias": "Jacques82",
"sender": "62d7999d976791c65dd6eacd",
"senderRole": "ops",
"receiver": "62d64eef37d10fea52e1ce72",
"receiverRole": "student",
"receiverAlias": "Daniela_Jacobs0",
"message": "dsds",
"sentAt": "2022-07-22T09:47:13.000Z",
"id": "62da7221bc0eb3a259fc2256"
},
"62da724dbc0eb3a259fc2260": {
"order": "62d529f60be7549be1f53df3",
"alias": "Jacques82",
"sender": "62d7999d976791c65dd6eacd",
"senderRole": "ops",
"receiver": "62d64eef37d10fea52e1ce72",
"receiverRole": "student",
"receiverAlias": "Daniela_Jacobs0",
"message": "hello nm",
"sentAt": "2022-07-22T09:47:57.000Z",
"id": "62da724dbc0eb3a259fc2260"
},
"62da75559f7a23f89a0958d4": {
"order": "62d529f60be7549be1f53df3",
"alias": "Jacques82",
"sender": "62d7999d976791c65dd6eacd",
"senderRole": "ops",
"receiver": "62d64eef37d10fea52e1ce72",
"receiverRole": "student",
"receiverAlias": "Daniela_Jacobs0",
"message": "b",
"sentAt": "2022-07-22T10:00:53.000Z",
"id": "62da75559f7a23f89a0958d4"
},
"62da94e59f7a23f89a0958ed": {
"order": "62d529f60be7549be1f53df3",
"alias": "Jacques82",
"sender": "62d7999d976791c65dd6eacd",
"senderRole": "ops",
"receiver": "62d64eef37d10fea52e1ce72",
"receiverRole": "student",
"receiverAlias": "Daniela_Jacobs0",
"message": "hello there",
"sentAt": "2022-07-22T12:15:31.000Z",
"id": "62da94e59f7a23f89a0958ed"
}
}
}
}
}
}

How To $pull From Deeply Nested Document (mongoose)

I need to delete a location object from the locations array. It is deeply nested. I followed mongoose documentation but my attempts didn't work:
lists = [{
"listName": "Test",
"_id": "8d55f0afe545a0178c320706",
"listId": "5fd9a3bef6c39b2f9c4df65b",
"date": "12/15/2020",
"dueDate": "2020-11-18",
"items": [
{
"itemNumber": 123,
"description": "item123",
"onHand": 60,
"_id": "13dd1f26ecd2baeb61b3b455",
"locations": [
{
"locationName": "loc1",
"count": 10,
"_id": "50a2c969465ba8010bd48977"
},
{
"locationName": "loc2",
"count": 20,
"_id": "51c2f1d25311dc8fabdbf604a59b"
},
{
"locationName": "Loc3",
"count": 30,
"_id": "7cb0c1f51a91c384846d65f8b2ae"
}
]
},
{more lists}
Attempt:
router.post("/lists/deleteLoc", (req, res) => {
const {
listId,
list_id,
item_id,
location_id
} = req.body;
List.updateOne({
"lists.listId": listId,
"lists._id": list_id
}, {
$pull: {
"lists.$.items": {
locations: {
$elemMatch: {
_id: location_id
})
.then(() => res.json({
msg: "location removed"
}))
.catch((err) => res.status(400).json({
msg: "Error: " + err
}));
});
If the request location_id was "7cb0c1f51a91c384846d65f8b2ae" it should delete the last location from the array. The desired result:
lists = [{
"listName": "Test",
"_id": "8d55f0afe545a0178c320706",
"listId": "5fd9a3bef6c39b2f9c4df65b",
"date": "12/15/2020",
"dueDate": "2020-11-18",
"items": [
{
"itemNumber": 123,
"description": "item123",
"onHand": 60,
"_id": "13dd1f26ecd2baeb61b3b455",
"locations": [
{
"locationName": "loc1",
"count": 10,
"_id": "50a2c969465ba8010bd48977"
},
{
"locationName": "loc2",
"count": 20,
"_id": "51c2f1d25311dc8fabdbf604a59b"
}
]
},
{more lists}
I've tried basically all variations of this, but none have worked.
I'm also not sure if making a router.post or an axios.post request for deletion is correct. Should this be axios.delete and router.delete?
I've tried this in one of my similar DB and worked!
List.updateOne({ "listId": yourListId },
{
'$pull': {
'items.$[item].locations': { "_id": yourLocationId }
}
}, {
"arrayFilters": [
{
"item._id": yourItemId
}
]
}, function (err) {
if (err) {
res.json(err)
} else {
res.json({ message: "Updated" })
}
})
}
You've to put the values that're inside your DB from the object that you want to delete.
So if you want to delete the object with
"locationname" : "Loc3"
You should use
var yourListId = "5fd9a3bef6c39b2f9c4df65b";
var yourItemId = "13dd1f26ecd2baeb61b3b455";
var yourLocationId = "7cb0c1f51a91c384846d65f8b2ae";
Try it out!

Sending rich message in Dialogflow fullfilment

I am trying to create a DialogFlow chatbot setting up my fullfilment with rich message but the payload that carry it never show up.
Here is the inline code I'm using:
const functions = require('firebase-functions');
const {WebhookClient} = require('dialogflow-fulfillment');
const {Card, Suggestion, Payload} = require('dialogflow-fulfillment');
var answers = [];
process.env.DEBUG = 'dialogflow:debug'; // enables lib debugging statements
exports.dialogflowFirebaseFulfillment = functions.https.onRequest((request, response) => {
const agent = new WebhookClient({ request, response });
console.log('Dialogflow Request headers: ' + JSON.stringify(request.headers));
console.log('Dialogflow Request body: ' + JSON.stringify(request.body));
function fallback(agent) {
agent.add(`I didn't understand FULLFILMENT`);
agent.add(`I'm sorry, can you try again? FULLFILMENT`);
}
function answer1Handler(agent){
agent.add(`Intent answer1 called`);
const answer = agent.parameters.number;
answers.push(answer);
const payload = {
"text": "Trucmuche"/*,
"attachments": []*/
};
agent.add(new Payload(agent.SLACK, payload));
/*agent.add(
new Payload(agent.SLACK, payload, {rawPayload: true, sendAsMessage: true})
);*/
}
// Run the proper function handler based on the matched Dialogflow intent name
let intentMap = new Map();
intentMap.set('Default Fallback Intent', fallback);
intentMap.set('answer1', answer1Handler);
agent.handleRequest(intentMap);
});
This is called when reaching "answer1" intent. It works well when my answer1Handler is only:
function answer1Handler(agent){
agent.add(`Intent answer1 called`);
}
I don't know what I missed I tried both to follow the official documentation on rich message as well as this response but none helped me create my rich responses.
However, when I add a the custom payload you can see above, it doesn't answer anymore and I get an error message in raw api response. Indeed, it wither gives an error:
{
"responseId": "49e306dc-c2a9-4667-ac67-60eb3692ce98-e15c53b8",
"queryResult": {
"queryText": "1",
"parameters": {
"number": 1
},
"allRequiredParamsPresent": true,
"outputContexts": [
{
"name": "projects/pollingagent-jnscpa/agent/sessions/b6a5e9be-dd3e-7b54-3c4f-d942de297036/contexts/await_answer2",
"lifespanCount": 5,
"parameters": {
"number.original": "1",
"number": 1
}
},
{
"name": "projects/pollingagent-jnscpa/agent/sessions/b6a5e9be-dd3e-7b54-3c4f-d942de297036/contexts/await_answer1",
"lifespanCount": 4,
"parameters": {
"number.original": "1",
"number": 1
}
}
],
"intent": {
"name": "projects/pollingagent-jnscpa/agent/intents/bea1536a-dede-4faf-ab44-2ba4f80e9c0f",
"displayName": "answer1"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 4993
},
"languageCode": "en"
},
"webhookStatus": {
"code": 4,
"message": "Webhook call failed. Error: DEADLINE_EXCEEDED."
},
"alternativeQueryResults": [
{
"queryText": "1",
"outputContexts": [
{
"name": "projects/pollingagent-jnscpa/agent/sessions/b6a5e9be-dd3e-7b54-3c4f-d942de297036/contexts/await_answer1",
"lifespanCount": 4,
"parameters": {
"number.original": "1",
"number": 1
}
},
{
"name": "projects/pollingagent-jnscpa/agent/sessions/b6a5e9be-dd3e-7b54-3c4f-d942de297036/contexts/await_answer2",
"lifespanCount": 3,
"parameters": {
"number.original": "1",
"number": 1
}
}
],
"languageCode": "en"
}
]
}
Or only gives back the first message as you can see in the raw API response:
{
"responseId": "b6bc8807-eca9-45e5-b25e-30e1b4142da7-e15c53b8",
"queryResult": {
"queryText": "1",
"parameters": {
"number": 1
},
"allRequiredParamsPresent": true,
"fulfillmentMessages": [
{
"text": {
"text": [
"Intent answer1 called"
]
}
}
],
"outputContexts": [
{
"name": "projects/pollingagent-jnscpa/agent/sessions/b6a5e9be-dd3e-7b54-3c4f-d942de297036/contexts/await_answer2",
"lifespanCount": 5,
"parameters": {
"number.original": "1",
"number": 1
}
},
{
"name": "projects/pollingagent-jnscpa/agent/sessions/b6a5e9be-dd3e-7b54-3c4f-d942de297036/contexts/await_answer1",
"lifespanCount": 4,
"parameters": {
"number.original": "1",
"number": 1
}
}
],
"intent": {
"name": "projects/pollingagent-jnscpa/agent/intents/bea1536a-dede-4faf-ab44-2ba4f80e9c0f",
"displayName": "answer1"
},
"intentDetectionConfidence": 1,
"diagnosticInfo": {
"webhook_latency_ms": 217
},
"languageCode": "en"
},
"webhookStatus": {
"message": "Webhook execution successful"
},
"alternativeQueryResults": [
{
"queryText": "1",
"outputContexts": [
{
"name": "projects/pollingagent-jnscpa/agent/sessions/b6a5e9be-dd3e-7b54-3c4f-d942de297036/contexts/await_answer1",
"lifespanCount": 4
}
],
"languageCode": "en"
}
]
}
Consequently how can I send rich message within dialogflow fullfilment?

Mongoose findOneAndUpdate inside nested Arrays

I need to update the type, key and values in the 'fields' array if the componentId and the wfInstanceId id matches.
This is the document that i need to do so.
{
"_id": {
"$oid": "586b6d756937c22f207dd5af"
},
"wfInstanceId": "0111",
"workflowId": "ash3",
"folderURL": "ash3",
"teamName": "teamName",
"dataStream": [
{
"componentInstanceId": "componentInstanceId1",
"componentId": "componentId1",
"_id": {
"$oid": "586b6d756937c22f207dd5b0"
},
"fields": [
{
"type": "String",
"value": "value1",
"key": "key",
"_id": {
"$oid": "586b6d756937c22f207dd5b1"
}
},
{
"type": "String",
"value": "value2",
"key": "key1",
"_id": {
"$oid": "586b6d756937c22f207dd5b1"
}
}
]
},
{
"componentInstanceId": "componentInstanceId2",
"componentId": "componentId22",
"_id": {
"$oid": "586b6d756937c22f207dd5b0"
},
"fields": [
{
"type": "String",
"value": "value1",
"key": "key",
"_id": {
"$oid": "586b6d756937c22f207dd5b1"
}
},
{
"type": "String",
"value": "value2",
"key": "key2",
"_id": {
"$oid": "586b6d756937c22f207dd5b1"
}
}
]
}
],
"id": "38f356f0-d196-11e6-b0b9-3956ed7f36f0",
"__v": 0
}
I tried this like this, Also i tried $set over $push which is also doesn't work.
Model.findOneAndUpdate( {'wfInstanceId': '0111', 'dataStream.componentId': 'componentId1'}, {$push : { 'dataStream.fields.$.key' : 'sdsdds' }}, { upsert: true }, function (err, data) {
if (err) {
reject(err);
console.log('error occured' + err);
}
console.info("succesfully saved");
resolve (data);
});
As they describe in this DOC it shouldbe working for update method that is also didn't work for me. Any help will be really appreciated to overcome this issue i'm facing.
As you are using $ operator it updates only the first matched array element in each document.
As of now it is not possible in mongoose to directly update all array elements.
You can do this in your case:
db.collection.find({'wfInstanceId': '0111', 'dataStream.componentId': 'componentId1'})
.forEach(function (doc) {
doc.datastream.forEach(function (datastream) {
if (dataStream.componentId === componentId1) {
dataStream.fields.forEach(function(fields){
// you can also write condition for matching condition in field
dataStream.fields.key="";
dataStream.fields.value="";
dataStream.fields.type="";
}
}
});
db.collection.save(doc);
});
It is normal javascript code. I think it's clearer for mongo newbies.

Categories

Resources