elasticsearch.js bulk insert error - javascript

I am trying to insert/update data with the javascript elasticsearch client, but I am getting the error:
{
"error": {
"root_cause": [
{
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [1], expected a simple value for field [_data] but found [START_OBJECT]"
}
],
"type": "illegal_argument_exception",
"reason": "Malformed action/metadata line [1], expected a simple value for field [_data] but found [START_OBJECT]"
},
"status": 400
}
This is the data that is being sent
esclient.bulk({
body: [
{
"index":
{
"_index":"myindex",
"_type":"movie",
"_id":"1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
"_data":
{
"title":"Title 2",
"description":"This should be updated with this new data.",
"score":1,
"suggest_title":"Title 2",
"img":"http://url.to.image/img.jpeg",
"genres":["Comedy"],
"release":"2015-01-07T23:00:00.000Z",
"language":"EN",
"provider":
{
"id":"InstaFilmFlixify",
"url":"http://www.InstaFilmFlixify.com/play?id=238412"
}
}
}
}
]
})
It appears as this code generates the following request to ES:
-> POST http://docker.me:9200/_bulk
{
"index": {
"_index": "myindex",
"_type": "movie",
"_id": "1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
"_data": {
"title": "Title 2",
"description": "This should be updated with this new.",
"score": 1,
"suggest_title": "Title 2",
"img": "http://url.to.image/img.jpeg",
"genres": [
"Comedy"
],
"release": "2015-01-07T23:00:00.000Z",
"language": "EN",
"provider": {
"id": "InstaFilmFlixify",
"url": "http://www.InstaFilmFlixify.com/play?id=238412"
}
}
}
}
What am I doing wrong? What is happening? Can this possibly be a bug in ES / the ES adapter.
Elasticsearch version 2.1

I haven't seen the "_data" parameter before. Where did you get the idea to use that?
Take a look at the docs for the js client.
Anyway, this should work for you:
esclient.bulk({
body: [
{
"index":
{
"_index":"myindex",
"_type":"movie",
"_id":"1IEAEHNOIORANIT4SEOASNIE3HAETN2E...",
}
},
{
"title":"Title 2",
"description":"This should be updated with this new data.",
"score":1,
"suggest_title":"Title 2",
"img":"http://url.to.image/img.jpeg",
"genres":["Comedy"],
"release":"2015-01-07T23:00:00.000Z",
"language":"EN",
"provider":
{
"id":"InstaFilmFlixify",
"url":"http://www.InstaFilmFlixify.com/play?id=238412"
}
}
]
})

Related

CouchDB view with include_docs=true yields doc: null

I have 2 distinct data types in the same couchdb database, message and user. They look like follows:
// message
{
"_id": "test__032142f981b1bddd00bb9a0161688480",
"_rev": "8-d1b19caabac334c4b9e9eb1f0b0b8986",
"created": "2011-12-27T23:48:17.940699Z",
"text": "Omnis quam nesciunt dolor quaerat.",
"author_id": "test__d3f62f343144dfef8fd112946da9c1b3",
"type": "message",
"subject": "Some text"
"recipients": [
"user id 1",
"user id 2",
]
}
//user
{
"_id": "test__d3f62f343144dfef8fd112946da9c1b3",
"_rev": "12-88206492cc5274248aa5a64ff6a49b9a",
"type": "user",
"profile": {
"callname": "User name",
"fullname": "User name",
"profile_text": "Blah blah",
"urls": [
{
"url": "http://www.example.org",
"description": "CV, Blog, Pictures, Research, Links",
"label": "Homepage"
}
],
"profile_image_url": "http://localhost:5000/static/img/profiles/2e38f9fdd9e458eb4db0e7bb3b8e9576.jpg"
},
"registration_date": "2014-08-14T20:07:28Z",
"verified": "none",
"preferences": {
"lang": "en"
},
"last_login": "2014-08-14T20:07:28Z",
"email": "user#example.org"
}
Out of reasons, inlining is not a possibility in this case. What I want now is to get a list of all messages with the author object included (Like SQL SELECT * FROM messages JOIN users on message.author_id = message.id).
I read this two articles:
http://www.cmlenz.net/archives/2007/10/couchdb-joins
http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views#Linked_documents
So I wanted to use include_docs=true and then a list to format it nicely. My messages map function:
function(doc) {
if (doc.type == 'message') {
emit(doc._id, {_id: doc.author_id, entity: doc});
}
}
I call this using the URL http://localhost:5984/dedri/_design/dedri/_view/messages?include_docs=true. But I get this, with doc: null:
{
"total_rows": 34,
"offset": 0,
"rows": [
{
"id": "test__032142f981b1bddd00bb9a0161688480",
"key": "test__032142f981b1bddd00bb9a0161688480",
"value": {
"_id": "test__d3f62f343144dfef8fd112946da9c1b3",
"entity": {
"_id": "test__032142f981b1bddd00bb9a0161688480",
"_rev": "8-d1b19caabac334c4b9e9eb1f0b0b8986",
"created": "2011-12-27T23:48:17.940699Z",
"text": "Omnis quam nesciunt dolor quaerat.",
"version": "0.1",
"author_id": "test__d3f62f343144dfef8fd112946da9c1b3",
"type": "message",
"subject": "Some text"
}
},
"doc": null
},
...
I don't know what to do anymore. There is no editing or deleting done in the map function. Is this a problem of mine or a bug?
Try change your map js to a simpler form for debugging, e.g.
function(doc) {
if (doc.type == 'message') {
// emit(doc._id, {_id: doc.author_id, entity: doc});
emit(doc._id, { '_id': doc.author_id });
}
}

Passing function argument to retrieve data from an object

I am have some trouble with a script I am working on. I have been provided with an object with multiple items from a product catalog.
What I am trying to do is to write a function which to which will allow me to render this data easily.
<script type="application/javascript">
SKUinfo =
{
"s238554": {
"Age": {
"Description": "Age 18+",
"Thumbnail": "/productImages/assets/img/icon18.gif"
},
"Barcode": {
"Barcode": "50622132430794"
},
"Currency": "£",
"Description": "Description goes here",
"Id": 44305,
"Packshots": [
"/productImages/238556/1min.jpg",
"/productImages/238556/2med.jpg",
"/productImages/238556/3max.jpg"
],
"Pegis": [],
"Platform": {
"Button": "Xbox 360",
"ID": 0
},
"Publisher": {
"Description": null
},
"Release": "/Date(1392940800000+0000)/",
"Screenshots": [
{
"ScreenshotMax": "/productImages/238556/5scrmax1.jpg",
"ScreenshotMin": "/productImages/238556/4scrmin1.jpg"
}
],
"Title": "Product title 2 goes here",
"Variants": [
{
"Id": 58242,
"MaxOrderQuantity": 3,
"Presellable": true,
"Price": 29.97,
"PriceCultureFormat": "29.97",
"PriceWithCurrencyFormat": "£29.97",
"Sku": 238556,
"Type": {
"Description": "New"
}
},
],
"Vendor": {
"Description": ""
},
},
"s238556": {
"Age": {
"Description": "Age 18+",
"Thumbnail": "/productImages/assets/img/pegi/icon18.gif"
},
"Barcode": {
"Barcode": "5060134530794"
},
"Currency": "£",
"Description": "Description here",
"Id": 654654,
"Packshots": [
"/productImages/238556/1min.jpg",
"/productImages/238556/2med.jpg",
"/productImages/238556/3max.jpg"
],
"Pegis": [],
"Platform": {
"Button": "PlayStation 3",
"ID": 0
},
"Publisher": {
"Description": null
},
"Release": "/Date(1392940800000+0000)/",
"Screenshots": [
{
"ScreenshotMax": "/productImages/238556/5scrmax1.jpg",
"ScreenshotMin": "/productImages/238556/4scrmin1.jpg"
},
{
"ScreenshotMax": "/productImages/238556/7scrmax2.jpg",
"ScreenshotMin": "/productImages/238556/6scrmin2.jpg"
},
],
"Title": "Product title 2 goes here",
"Variants": [
{
"Id": 58242,
"MaxOrderQuantity": 3,
"Presellable": true,
"Price": 29.97,
"PriceCultureFormat": "29.97",
"PriceWithCurrencyFormat": "£29.97",
"Sku": 238556,
"Type": {
"Description": "New"
}
},
],
"Vendor": {
"Description": ""
},
"VideoHTML": "html here",
"status": {
"Response": "product found",
"Success": true
}
}
}
</script>
The above example is the output I get for two products.
If I try to get access to this data this is where I have a problem
<script type="application/javascript">
function getSKU(s)
{
console.log(SKUinfo.s.Title);
}
getSKU(s238554);
</script>
I imagine this is being caused when I am passing the argument s back to the function getSKU a the node selection in the data object. In this I would expect the console output to be the Title from SKU s238554.
What I get however, is: Uncaught ReferenceError: s238554 is not defined
I would appreciate any guidance that can be offered as I am a javascript novice.
Access your property by used[] on SKUinfo.s.Title like SKUinfo[s].Title
And also pass your property name within the quotes 's238554' as it's not variable.
Something like this.
function getSKU(s){
console.log(SKUinfo[s].Title);
}
getSKU('s238554'); // s238554 within quotes.

Facebook Open Graph not returning all results for Books.Read?

When using the Facebook Open Graph explorer, I run the following, to get a list of my books read.
/706685881/books.reads?limit=25&offset=0
This returns only 5 results, even though I have more books read than that, and I've explicitly requested up to 25 results. Following the paging link to the (theoretical) more results, shows a page with no more results.
here's my output of the query:
{
"data": [
{
"data": {
"book": {
"id": "452418224806835",
"url": "https://www.facebook.com/pages/Fantastic-Voyage/452418224806835",
"type": "books.book",
"title": "Fantastic Voyage"
}
},
"id": "10151871872740882"
},
{
"data": {
"book": {
"id": "106151566083577",
"url": "https://www.facebook.com/pages/The-Andromeda-Strain/106151566083577",
"type": "books.book",
"title": "The Andromeda Strain"
}
},
"id": "10151870390740882"
},
{
"data": {
"progress": [
{
"timestamp": 1388668020,
"percent_complete": 0
},
{
"timestamp": 1392949225,
"percent_complete": 100
}
],
"book": {
"id": "10150233066083545",
"url": "http://www.goodreads.com/book/show/10664113-a-dance-with-dragons",
"type": "good_reads:book",
"title": "A Dance with Dragons (A Song of Ice and Fire, #5)"
}
},
"id": "10151867499170882"
},
{
"data": {
"progress": [
{
"timestamp": 1391350736,
"percent_complete": 0
},
{
"timestamp": 1392569208,
"percent_complete": 100
}
],
"book": {
"id": "1390736374489290",
"url": "http://www.goodreads.com/book/show/18405477-city-of-the-sun",
"type": "good_reads:book",
"title": "City of the Sun"
}
},
"id": "10151860185710882"
},
{
"data": {
"book": {
"id": "526531387368738",
"url": "http://apps.facebook.com/bookscout/?currworkid=9000000000133742",
"type": "books.book",
"title": "Gates of Fire"
}
},
"id": "10151226827780882"
}
],
"paging": {
"next": "https://graph.facebook.com/706685881/books.reads?fields=data&limit=25&offset=25&__after_id=10151226827780882"
}
}
Anybody seeing what I am doing wrong?

I update the same video ID, but the youtube-api v3 still show "Video not found"

I used google js api Link1and youtube v3 api Link2
I want use js to update my video, the js code for listing videos:
var requestOptions = {
playlistId: playlistId,
part: 'snippet',
//id: ['uY3InyzU79U','nOoHrAR50f8'],
myRating:'like',
maxResults: 9
};
if (pageToken) {
requestOptions.pageToken = pageToken;
pToken=pageToken;
}
var request = gapi.client.youtube.videos.list(requestOptions);
request.execute(somefunction());
The json response:
[
{
"id": "gapiRpc",
"result": {
"kind": "youtube#videoListResponse",
"etag": "\"Wes3wj_Yozi4a9GfHnpc8qG73KI/8qQBat1N6baCDG6C3Eed_XV_sZo\"",
"pageInfo": {
"totalResults": 3,
"resultsPerPage": 9
},
"items": [
{
"id": "nOoHrAR50f8",
"kind": "youtube#video",
"etag": "\"Wes3wj_Yozi4a9GfHnpc8qG73KI/f7JZaoHeZG-CDYzXVRw2Q11Lins\"",
"snippet": {
"publishedAt": "2013-05-30T05:37:50.000Z",
"channelId": "UCx0RClYCMhtsOXRlSyRcuRA",
"title": "Webcam video from May 30, 2013 3:36 PM",
"description": "",
"thumbnails": {
"default": {
"url": "https://i3.ytimg.com/vi/nOoHrAR50f8/default.jpg"
},
"medium": {
"url": "https://i3.ytimg.com/vi/nOoHrAR50f8/mqdefault.jpg"
},
"high": {
"url": "https://i3.ytimg.com/vi/nOoHrAR50f8/hqdefault.jpg"
},
"standard": {
"url": "https://i3.ytimg.com/vi/nOoHrAR50f8/sddefault.jpg"
}
},
"channelTitle": "foretribe1",
"tags": [
"test",
"webcam"
],
"categoryId": "22"
}
}
]
}
}
]
the update video js code:
var requestOptions = {
part:'snippet',
id: String(id),
snippet:{ title: "ok update"}
};
var request = gapi.client.youtube.videos.update(requestOptions);
request.execute(nothing());
the request json:
[{"jsonrpc":"2.0",
"id":"gapiRpc",
"method":"youtube.videos.update",
"params": {
"part":"snippet",
"id":"nOoHrAR50f8",
"snippet":{"title":"ok update"}
},
"apiVersion":"v3"
}]
you can see here the id is the same as the list request json, but I still get the error respond:
[
{
"error": {
"code": 500,
"message": "Video not found",
"data": [
{
"message": "Video not found"
}
]
},
"id": "gapiRpc"
}
]
I also test on Link2, but also get 500 error.
You need wrap your updates in a 'resource' attribute:
gapi.client.youtube.videos.update({
fields: "snippet",
part: "snippet",
resource: {
id: "vid_id",
snippet: {
description: "updating vid",
}
}
});
Remember to set other attributes in the snippet (like categoryId, tags, etc.); otherwise, you'll get another error or they'll be unset.
Probably the best way to do this is to first retrieve the video snippet using gapi.client.youtube.videos.list and alter the snippet, then call gapi.client.youtube.videos.update passing in the altered snippet in the resource attrbute.

Getting Messages from Inbox

I have a question about the Graph API.
I use Javascript for the API and make a little test website ,where you can log in ,look for new messages and write a new status.
My problem is that I can't get the messages or the thread.
FB.api('/me/inbox',function(response) { alert(response.id); } ); don't work.
Have somebody an example for getting the messages in the inbox??
Thanks
The /me/inbox request requires that you have the read_mailbox permission granted.
Once you've got that, the /me/inbox request will return an array of Thread's, which will look something like this;
{
"data": [
{
"id": "1126884978255",
"from": {
"name": "Someone's Name",
"id": "34723472"
},
"to": {
"data": [
{
"name": "Someone's Name",
"id": "34723472"
},
{
"name": "Matt Lunn",
"id": "560914724"
}
]
},
"message": "Testing the one-ness.",
"updated_time": "2012-01-31T12:13:00+0000",
"unread": 0,
"unseen": 0,
"comments": {
"data": [
{
"id": "1126884978255_6769",
"from": {
"name": "Someone's Name",
"id": "34723472"
},
"message": "£140!?",
"created_time": "2012-01-31T11:33:15+0000"
},
{
"id": "1126884978255_6771",
"from": {
"name": "Matt Lunn",
"id": "560914724"
},
"message": "^^ month in advance as well",
"created_time": "2012-01-31T11:33:26+0000"
}
]
},
"type": "thread"
}
],
"summary": {
"unseen_count": 0,
"unread_count": 21,
"updated_time": "2012-01-31T13:19:31+0000"
}
}
So depending which ID you're after, you'll have to do;
for (var i=0;i<response.data.length;i++) {
var thread = response.data[i];
for (var j=0;j<thread.comments.data.length;j++) {
var comment = thread.comments.data[j];
console.log(comment.message);
}
}
Hopefully you get the idea...

Categories

Resources