How to delete multiple id documents in elasticsearch index - javascript

var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'ABC',
log: 'trace',
apiVersion: '7.1'
});
client.delete({
index: 'allevents',
type: '_doc',
id:"2"
}).then(function(resp) {
console.log("Successful query!");
console.log(JSON.stringify(resp, null, 4));
}, function(err) {
console.trace(err.message);
});
When I delete a single document by passing single id value.its working fine.
But I want to delete multiple documents in a single query.
How will we do?
I tried
client.delete({
index: 'allevents',
type: '_doc',
id: ["2","3"]
})
This function returning error.

I suggest to leverage the bulk method/API, like this:
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'ABC',
log: 'trace',
apiVersion: '7.1'
});
var idsToDelete = ["2", "3"];
var bulk = idsToDelete.map(id => {
return {
'delete': {
'_index': 'allevents',
'_type': '_doc',
'_id': id
}
};
});
client.bulk({
body: bulk
}).then(function(resp) {
console.log("Successful query!");
console.log(JSON.stringify(resp, null, 4));
}, function(err) {
console.trace(err.message);
});
Another way of doing it is to leverage the deleteByQuery method/API:
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'ABC',
log: 'trace',
apiVersion: '7.1'
});
var idsToDelete = ["2", "3"];
client.deleteByQuery({
index: 'allevents',
type: '_doc',
body: {
query: {
terms: {
_id: idsToDelete
}
}
}
}).then(function(resp) {
console.log("Successful query!");
console.log(JSON.stringify(resp, null, 4));
}, function(err) {
console.trace(err.message);
});

Related

How to make a post call to insert into database with an array of object in Express JS. Can some one helppp?

I'm using ExpressJS for serverside. Whenever making a post call with single object in an array everything is working fine. but when there are multiple objects in array then im getting this error:
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
this is the object:
selectedItems: [
{ id: '1', pipeId: 3, heatId: '1111', idNo: '1' },
{ id: '1', pipeId: 4, heatId: '1111', idNo: '2' },
{ id: '4', pipeId: 6, heatId: '1111', idNo: '4' },
{ id: '5', pipeId: 5, heatId: '1111', idNo: '5' }
],
this is my post call:
function saveLoadDetails(requestBody, res) {
console.log(requestBody);
let countOfSelectedItems = requestBody.selectedItems.length;
let dDate = new Date();
var out = {};
let currentDate = moment(dDate).format("YYYY-MM-DD");
console.log(date + "uploadLoadDetails fired");
for (let i = 0; i <= countOfSelectedItems - 1; i++) {
const request = new sql.Request(dbconn1);
request.input("loadNo", sql.Int, requestBody.loadNumber);
request.input("loadedOn", sql.VarChar, currentDate);
request.input("pipeId", sql.NVarChar, requestBody.selectedItems[i].pipeId);
request.input("heatNo", sql.NVarChar, requestBody.selectedItems[i].heatId);
request.input("uid_ent", sql.VarChar, requestBody.uid_ent);
request.execute("mobile_save_shipping_load_details", (err, result) => {
if (!err) {
out = {
code: 200,
status: "OK",
message: "pipe saved",
data: {
result,
},
};
} else {
out = {
code: 404,
status: " DB Error : Data Operation Failed.",
data: { err },
};
}
res.send(out);
});
}
console.log(out);
sendEmail(requestBody);
}
this error occurs whenever you try to send more than one response, your res.send () is inside a callback inside a loop, that is, several objects in the array are equivalent to several responses.
https://www.nodejsconnect.com/blog/articles/sending-multiple-http-responses-expressjs

Using query_string search with post_filter

I'm trying to search for a city using query_string and asterix (query: `${city}*`) while also filtering the results by countryId.
The query works well without filtering for countryId, but when adding the filter it finds nothing. Boty city and countryId were mapped as text.
Code:
const elasticsearch = require('elasticsearch');
...
this.client = new elasticsearch.Client({
hosts: [this._serviceUri]
});
...
// The interesting part:
const results = await this.client.search({
index: 'cities',
body: {
query: {
query_string: {
query: `${city}*`,
fields: ['city', 'countryId'],
// type: 'best_fields'
}
},
post_filter: { term: { countryId } }
}
});
How can I filter the results with post_filter or something alike correctly?
UPDATE:
Here is how the mapping looks like:
mappings: {
_doc: {
properties: {
"city": {type: 'text'},
"countryId": {type: 'text'}
}
}
}
I would do it this way, without resorting to post_filter:
// The interesting part:
const results = await this.client.search({
index: 'cities',
body: {
query: {
bool: {
must: {
query_string: {
query: `${city}*`,
fields: ['city', 'countryId'],
// type: 'best_fields'
}
},
filter: {
term: {
countryId: `${countryId}`,
}
}
}
}
}
});

Merge two Arrays using underscore.js in AngularJS

I have two api requests and both gets a result of JSON. The first request is "account" request and second is "Container" request. Now:
Request for all Accounts (accountid: 1, name: account1, blabla)
Request for all Containers (accountid: 1, name: Container1, blabla)
This is the result JSON of Accounts:
account: [
{
accountId: "123456789",
fingerprint: null,
name: "Account2",
path: "accounts/123456789",
},
This is the result JSON of Containers:
{
accountId: "123456789",
containerId: "123****",
domainName: null,
fingerprint: "123*****",
name: "Container23",
path: "accounts/123456789/containers/123******",
publicId: "GTM-****"
},
As you can see the container result contains the accountid so im trying to merge those two results so it becomes this combined (container):
{
accountId: "123456789",
name: "Account2", <------ THIS is what i want to merge
containerId: "123****",
domainName: null,
fingerprint: "123*****",
name: "Container23",
path: "accounts/123456789/containers/123******",
publicId: "GTM-****"
},
Remember there are many containers and accounts not just one block.
What i have tried using underscore:
var mergedList = _.map($scope.GTMcontainers, function(data){
return _.extend(data, _.findWhere($scope.account, { id: data.accountId }));
});
console.log(mergedList);
Here is my AngularJS
function getContainer() {
$http.get("http://******")
.then(function (response) {
$scope.GTMcontainers = response.data;
console.log(response);
$scope.loading = false;
})
.then(function () {
$http.get("http://******")
.then(function (response2) {
$scope.account = response2.data;
console.log(response2);
var mergedList = _.map($scope.GTMcontainers, function(data){
return _.extend(data, _.findWhere($scope.account, { id: data.accountId }));
});
console.log(mergedList);
})
})
}
Using this underscore gives me exactly the same JSON result as i requested (no merge).
Hope some one has experience with this.
Thanks
Updated:
using simple javascript
var accounts = [
{
accountId: "123456789",
fingerprint: null,
name: "Account2",
path: "accounts/123456789",
},{
accountId: "123456780",
fingerprint: null,
name: "Account3",
path: "accounts/123456789",
}]
var containers =[{
accountId: "123456789",
containerId: "123****",
domainName: null,
fingerprint: "123*****",
name: "Container23",
path: "accounts/123456789/containers/123******",
publicId: "GTM-****"
},{
accountId: "123456780",
containerId: "123****",
domainName: null,
fingerprint: "123*****",
name: "Container24",
path: "accounts/123456789/containers/123******",
publicId: "GTM-****"
}]
var result=[];
containers.forEach(function(item){
var temp=accounts.find(function(d){return d.accountId == item.accountId});
if(temp)
item.name = temp.name;
result.push(item);
})
console.log(result);
I have faced that issue, I knew I have got responses, but data was not merged. So I have used callbacks.
function getContainer(callback) {
$http.get("http://******")
.then(function (response) {
$scope.GTMcontainers = response.data;
console.log(response);
$scope.loading = false;
})
.then(function () {
$http.get("http://******")
.then(function (response2) {
$scope.account = response2.data;
console.log(response2);
if(response && response2){
callback(response,response2);
}
})
})
}
At function call time--
getContainer(function(array1,array2){
if(array1 && array2 && array1.length>0 && array2.length>0){
var mergedList = _.map(array1, function(data){
return _.extend(data, _.findWhere(array2, { id: data.accountId }));
});
console.log(mergedList);
}
})

How can i calculate the response time in the below code and print it on the console

describe("Prefabs basic", function() {
it("It should create simple plrefab", function(done) {
var data = {
name: "Pre",
project: {
_id: settings.projectId,
name: "PM_40"
},
__t: "Prefabs",
stage: "planning",
multiTrade: {
value: false,
companies: []
},
owner: {
user: {
_id: ""
},
company: {
_id: ""
}
},
_customStage: "planning",
dates: [],
dateIndices: {
additional: {},
coord: 0,
deliver: 1
},
fileIndices: [],
todoIndices: [0],
new_prefab: true,
todos: [],
items: {
fileIndices: [],
todoIndices: [],
customId: "1",
name: "Item0",
level: "1",
zone: "west"
},
keywords: ["This is Prefab"]
};
chai
.request(server)
.post("/v3/prefabs/create")
.set("Authorization", settings.authKey)
.send(data)
.end(function(err, res) {
res.should.have.status(200);
prefab = res.body;
prefabId = res.body._id;
console.log(prefab);
});
});
});
I took the liberty of formatting your code so it is slightly more readable - in general you will get a better response to your questions if you present a question that is easy to understand. With that said, you can use process.hrtime() to time things in Node. For example,
it('does something', function() {
const startTime = process.hrtime();
chai
.request(server)
.post("/v3/prefabs/create")
.set("Authorization", settings.authKey)
.send(data)
.end(function(err, res) {
res.should.have.status(200);
const timeDifference = process.hrtime(startTime);
console.log(`Request took ${timeDifference[0] * 1e9 + timeDifference[1]} nanoseconds`);
});
});

Why does it not console.log anything when running findOne with Mongoose?

I'm trying to Query my MongoDB with Mongoose.
I search for the string 13 in the field eClassSegment in the collection eclasses. Something is printed in the console. Why?
Code:
var mongoose = require('mongoose'),
EClass = require('./models/eclass');
mongoose.Promise = require('bluebird');
EClass.findOne({ 'eClassSegment': '13' }, 'eClassSegment', function (err, result) {
if (err) {
console.log("Error: ", err);
}
console.log('All eClassSegments that equals 13: ', result);
});
The Moongoose Schema :
var mongoose = require('mongoose');
mongoose.Promise = require('bluebird');
// Defining Mongoose Schema
const eClassSchema = mongoose.Schema({
eclassSegment: { type: String, min: 2, max: 2 },
eclassMainGroup: { type: String, min: 2, max: 2 },
eclassGroup: { type: String, min: 2, max: 2 },
eclassCommodityClass: { type: String, min: 2, max: 2 },
preferredName: { type: String, max: 80 },
definition: { type: String, max: 1023 },
level: { type: String, min: 1, max: 1 },
mkSubclass: { type: String, min: 1, max: 1 },
mkKeyword: { type: String, min: 1, max: 1 }
});
// Create mongoose model
module.exports = mongoose.model('EClass', eClassSchema);
Example of an document in the MongoDB (I have many documents with eClassSegment = '13'..)
{
"_id" : ObjectId("58e5d8d8fc0788063e587e1a"),
"mkKeyword" : "0",
"mkSubclass" : "1",
"level" : "1",
"definition" : "Services for the development of a product basically on the basis of service contracts or contract development",
"preferredName" : "Development (Service)",
"eclassCommodityClass" : "00",
"eclassGroup" : "00",
"eclassMainGroup" : "00",
"eclassSegment" : "13",
"__v" : 0
}
So you are trying to search for eClassSegment.
But the key in the schema and the database is eclassSegment
I forgot to connect to the database... Need some coffe... :-)
Here is the solution!
var mongoose = require('mongoose'),
EClass = require('./models/eclass');
mongoose.Promise = require('bluebird');
mongoose.connect('mongodb://localhost/eclassCSV');
const db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
var geteClass = function() {
EClass.findOne({ 'eclassSegment': '13' }, 'eclassSegment', function (err, result) {
if (err) {
console.log('Error: ', err);
}
console.log('All eclassSegments that equals 13: ', result);
})
.then(function() {
mongoose.disconnect();
})
.catch(function(err) {
console.log('There was an error', err);
});
};
geteClass();
});
Example of a Console.log of result
All eclassSegments that equals 13: { _id: 58e5d8d8fc0788063e587e1a, eclassSegment: '13' }

Categories

Resources