I want to have a subset of json response from ElasticSearch server. I am new to ElasticSearch and JavaScript. Please have a look. The json file is the example file here from ElasticSearch doc. This is a sample:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 1.0,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "4",
"_score" : 1.0,
"_source":{"account_number":4,"balance":27658,"firstname":"Rodriquez","lastname":"Flores","age":31,"gender":"F","address":"986 Wyckoff Avenue","employer":"Tourmania","email":"rodriquezflores#tourmania.com","city":"Eastvale","state":"HI"}
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "9",
"_score" : 1.0,
"_source":{"account_number":9,"balance":24776,"firstname":"Opal","lastname":"Meadows","age":39,"gender":"M","address":"963 Neptune Avenue","employer":"Cedward","email":"opalmeadows#cedward.com","city":"Olney","state":"OH"}
}
................................
]
}
}
How to extract an array of account_number, balance and lastname for example [{"account_number":9,"balance":24776,"lastname":"Meadows"},{"account_number":4,"balance":27658,"lastname":"Flores"}, ......... ].
Here is the code I used:
var deferred = $q.defer();
$http.get("http://localhost:9200/bank/_search?q=*").success(function(data){
var country=data.hits.hists.map( function (count) {
return {
account_number: count._source.account_number,
balance: count._source.balance,
lastname: count._source.lastname
};
});
deferred.resolve(country);
});
return deferred.promise;
I took inspiration from similar codes like this one for the REST call. It seems that the data I got is undefined. Is there a configuration to do from ES side?
You can instruct ES to only return those fields using source filtering:
$http.get("http://localhost:9200/bank/_search?_source=account_number,balance,lastname&q=*").success(function(data){
That way you don't have to filter out the fields by yourself.
Then you have a typo on the next line: hists should read hits
var country=data.hits.hits.map( function (count) {
Please try it out.
Related
I am getting a weird problem when I run in shell for mongoDB.
My collection structure is like below in mongoDB.The type of ServiceList items must be integer.
{
"_id" : ObjectId("56565"),
"Contact" : "",
"Email" : "",
"AppList" : [
{
"Code" : "",
"Usage" : NumberInt(542),
"LicenceType" : "Monthly",
"CreatedDate" : ISODate("2015-07-29T06:15:20.520+0300"),
"ServiceList" : [
1,5,8
]
}
]
}
My aim to run this code is converting Usage type from int32 to int64.It works fine.When I run the code below. something weird occours, and the type of the items inside AppList.ServiceList is converted to double from int32 itself.
db.CustomerInfo.find({}).forEach( function (item) {
if(item.AppList != undefined){
item.AppList.forEach(function (x){
x.Usage = NumberLong(x.Usage);
});
db.CustomerInfo.save(item);
}
});
and it seems like this,
{
"_id" : ObjectId("56565"),
"Contact" : "",
"Email" : "",
"AppList" : [
{
"Code" : "",
"Usage" : NumberLong(542),
"LicenceType" : "Monthly",
"CreatedDate" : ISODate("2015-07-29T06:15:20.520+0300"),
"ServiceList" : [
1.0,5.0,8.0
]
}
]
}
How can I solve this problem ?
MongoDB version:3.4.9
MongoShell version:v3.4.9
I've to push a given array of Number values into a selected Document inside my MongoDB database.
The Document that I'm going to update as the following structure:
{
"_id" : {
"id" : 17,
"type" : "f"
},
"__v" : 0,
"created_at" : ISODate("2017-03-22T11:16:21.403Z"),
"token" : {
"expDate" : ISODate("2017-12-31T00:00:00Z"),
"token" : "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJlbWFpbCI6ImFsYWRpbkBjb25zb3J6aW9jZXIuaXQiLCJleHAiOjE1MTQ2Nzg0MDB9.QvbT146bA_KH5XA7MH8ASXm9cr3sPZChJ3prYyDireI"
},
"updated_at" : ISODate("2017-07-24T09:42:33.741Z"),
"plots" : {
"idPlot" : [
23570,
23475
]
},
"machines" : [
{
"idPlotBind" : 1,
"ip" : "",
"mac" : "18-5F-00-4A-FE-F4",
"irrId" : 31,
"_id" : ObjectId("59084f527d634d301338aac6"),
"addr" : "pialadin.ddns.net"
},
{
"idPlotBind" : null,
"ip" : "",
"mac" : "12-01-02-FE-AB-B2",
"irrId" : 35,
"_id" : ObjectId("59084f7d7d634d301338aac7")
}
]
}
I'm using the Mongoose library for JS, and the accused query is this one:
userSchema.findOneAndUpdate({$and:[{ '_id.id': resData.PlotRows.IdUser}, {'_id.type': 'f'}]},{$addToSet:{'plots.$.idPlot': {$each: plotData}}}, {upsert: false}, function(err, usr){
if(err){
console.log(err);
return;
}
});
But when I try to execute it, gives me back:
Error: Can't use $each with Number
I'm unable to fetch JSON value to check if the person is wearing glasses while taking photo, the value relies in four arrays: photos, tags, attributes and glasses, I want to check the value "value" is true or false. I use alert() to test if value fetched but nothing comes out. I don't know which part gets wrong
I use JavaScript to fetch the JSON value as follows:
var facesDet = $.getJSON( APIdetect, function() {
console.log( "success" );
console.log(facesDet);
})
.done(function (facesDet, tid) {
var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;
});
The JSON value looks as follows:
{
"photos" : [
{
"url" : "http://tinyurl.com/673cksr",
"pid" : "F#0c95576847e9cd7123f1e304476b59ae_59ec9bb2ad15f",
"width" : 375,
"height" : 409,
"tags" : [
{
"tid" : "TEMP_F#0c95576847e9cd7123f1e304b1dcbe53_59ec9bb2ad15f_56.53_40.83_0_1",
"recognizable" : true,
"confirmed" : false,
"manual" : false,
"width" : 30.67,
"height" : 28.12,
"center" : { "x" : 56.53, "y" : 40.83},
"eye_left" : { "x" : 66.93, "y" : 33.99},
"eye_right" : { "x" : 51.73, "y" : 33.99},
"yaw" : -16,
"roll" : 0,
"pitch" : 0,
"attributes" : {
"face" : { "value" : "true", "confidence" : 82 },
"gender" : { "value" : "female", "confidence" : 80 },
"glasses":{"value" : "true", "confidence" : 100},
"dark_glasses":{"value" : "true", "confidence" : 72},
"smiling":{"value" : "false", "confidence" : 35}
}
}
]
}
],
"status" : "success",
"usage" : {
"used" : 1,
"remaining" : 99,
"limit" : 100,
"reset_time_text" : "Fri, 21 September 2012 12:57:19 +0000",
"reset_time" : 1348232239
}
}
You tryed to take value from gender like from array:
var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;
But in your json its object, so you should use:
var VALUEIWANT = facesDet.photos[0].tags[0].attributes.gender.value;
Your access is wrong. You have an object, but you treat it like an array
var VALUEIWANT = facesDet.photos[0].tags[0].attributes[0].gender[0].value;
// ^^^ and ^^^
right access:
var VALUEIWANT = facesDet.photos[0].tags[0].attributes.gender.value;
data:
"attributes" : {
"face" : { "value" : "true", "confidence" : 82 },
"gender" : { "value" : "female", "confidence" : 80 },
"glasses":{"value" : "true", "confidence" : 100},
"dark_glasses":{"value" : "true", "confidence" : 72},
"smiling":{"value" : "false", "confidence" : 35}
}
try this one.
facesDet.photos[0].tags[0].attributes.gender.value
getJSON return a promise not the response, you need to use this code:
$.getJSON( APIdetect, function(facesDet) {
console.log( "success" );
console.log(facesDet);
})
Given the following data structure:
{
"comments" : {
"-JcBbk64Gpm1SKoFHv8b" : {
"content" : "blah",
"createdAt" : 1417550954985,
"link" : "http%3A%2F%2Flocalhost%3A3000%2F",
"recommendedCount" : 0,
"replies" : {
"-JcBbk8gF_nQ_vjwag61" : true
},
"replyCount" : 1
},
"-JcBbk8gF_nQ_vjwag61" : {
"content" : "blah blah",
"createdAt" : 1417550955151,
"link" : "http%3A%2F%2Flocalhost%3A3000%2F",
"recommendedCount" : 0,
"replyCount" : 1,
"replyToComment" : "-JcBbk64Gpm1SKoFHv8b"
}
},
"links" : {
"http%3A%2F%2Flocalhost%3A3000%2F" : {
"author" : 5,
"commentCount" : 2,
"comments" : {
"-JcBbk64Gpm1SKoFHv8b" : true,
"-JcBbk8gF_nQ_vjwag61" : true
},
"createdAt" : 1417550954931,
"ratingCount" : 2,
"recommendedCount" : 32,
"score" : 91,
"title" : "A Christian vs. an Atheist: Round 2",
"url" : "http://localhost:3000/"
}
}
}
I would like to retrieve all the comments for a given link and sort them in reverse chronological order. I've gotten this far, but I can't figure out how to do the reverse sort because I've already used orderByChild to narrow down the results by link:
ref.orderByChild('link').equalTo(currentLink).on('value', function (snap) {
console.log(snapshot.val());
});
If I call orderByChild() a second time like this:
ref.orderByChild('link').equalTo(currentLink).orderByChild('createdAt').on('value', function (snap) {
console.log(snapshot.val());
});
it fails with this error message:
Uncaught Error: Query.orderByChild: You can't combine multiple orderBy calls.
I'm stumped. Any suggestions?
I encountered the same issues on my firebase project and I solved that like this.
Please take a look.
var formRef = firebase.child('links').child(currentLink);
formRef.orderByChild('createdAt').on('value', function (snap) {
console.log(snapshot.val());
});
It will works.
Best Regards.
I am trying to delete "virtualNumber" : "12345" in the following document:
{
"_id" : ObjectId("50a9db5bdc7a04df06000005"),
"billingInfo" : null,
"date" : "dsfdsfsdfsd",
"description" : "sdfsdff",
"pbx" : {
"_id" : ObjectId("50a9db5bdc7a04df06000006"),
"did" : {
"1234567890" : {
"inventoryId" : "509df7547e84b25e18000001",
"didcountry" : "india",
"didState" : "bangalore",
"routeType" : "CallForward",
"didNumber" : "1234567890",
"didVirtualNumbers" : [
{
"virtualNumber" : "12345"
},
{
"virtualNumber" : "56789"
}
],
"id" : ObjectId("50a9db9acdfb4f9217000002")
}
},
},
I am using node.js, so I constructed a query in JavaScript:
var query = {_id: ObjectId("50a9db5bdc7a04df06000005")};
var obj = {};
obj["pbx.did.1234567890.didVirtualNumbers.virtualNumber"]=12345;
//problem
collection.update(query,{$pull:obj});
You need to match the array element like:
{"$pull": {"pbx.did.7259591220.didVirtualNumbers": {"virtualNumber": "12345"}}}
So you should change your code to:
obj["pbx.did.7259591220.didVirtualNumbers"]={"virtualNumber": "12345"};
Please refer to http://www.mongodb.org/display/DOCS/Updating#Updating-%24pull
It mentions the pull field should be an array.