How to insert json to the elasticsearch field? - json
I want to index document that include json in one field. I used folloeing code.
JSONOBject myjson=new JSONObject(myJSONstring);
IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
.setSource(jsonBuilder()
.startObject()
.field("jsondata",myjson )
.field("postDate", "date")
.field("message", "trying out Elasticsearch")
.endObject()
)
.get();
this document successfully added. when I try to query data ,myjson get as string.But I want to query inside the myjson also. can anyone help me. Thank you.
query using kibana sense,
POST /twitter/tweet/_search
{
"query": {
"match": {
"jsondata.amount": 0
}
}
}
this gives null,
{
"took": 14,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
Related
How to remove 'data' array key in Json response
I'm new in Laravel 7 and now my problem is the JSON response data wrapped by the data array key. Response data { "data": [ { "id": 3017, "total": "87.98", "subtotal": 83, "total_tax_items": 4.98, "count_items": 3, } ] } Expected response data { { "id": 3017, "total": "87.98", "subtotal": 83, "total_tax_items": 4.98, "count_items": 3, } } Here is my code that returns from the eloquent collection. $query = Sale::with(['items']) ->select('id', 'total') ->get(); return json_resp($query);
try return as: return response()->json($result);
Remove pivote attribute from JSON response in Laravel 5.2
I'm using many to many relationships using pivot table. But the problem is when I'm returning json response, the json also contains the pivot attribute as shown below: { "id": 2, "job_title": "et", "job_description": "Iusto provident.", "job_industry": "Braun, Jast and Quigley", "job_location": "Christiansenland", "job_experience": 7, "employment_type": "full", "recruiter_id": 9, "status": 1, "posted_date": "2016-02-02 07:55:28", "skills": [ { "id": 1, "value": "molestiae", "pivot": { "job_id": 2, "skill_id": 1 } } ] } What I want is something like this: { "id": 2, "job_title": "et", "job_description": "Iusto provident.", "job_industry": "Braun, Jast and Quigley", "job_location": "Christiansenland", "job_experience": 7, "employment_type": "full", "recruiter_id": 9, "status": 1, "posted_date": "2016-02-02 07:55:28", "skills": [ { "id": 1, "value": "molestiae", } ] } I have tried various solutions from the stackoverflow questions, but none of them seem to work. I'm new to Laravel. If you guys need more info on the model,I can post it. Please help.
Go to your Skill model and set: protected $visible = ['id', 'value'];
Representing matrix in JSON
I am trying to have matrix style data in JSON, but it doesn't seem to work. Can anyone help me understand what am I doing wrong? { "took": 12, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 44, "max_score": 1, "hits": [ { "_index": "transactions", "_type": "transaction", "_id": "trans0007", "_score": 1, "_source": { "fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0], [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,1,0], [0,1,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], [1,0,0,1,0,0,1,0,0,1,0,1,0,1,1,1,0,1,0,0], [0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,1,1,0,0,0,0,0,0,0,1,0,0,0,0,0,1,1,0], [0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0], [0,0,1,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,1,0], [1,0,0,1,0,0,1,0,1,1,0,1,0,0,1,1,0,1,1,0], [1,0,0,1,0,0,1,0,1,1,0,1,0,1,0,1,0,1,1,0], [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,0,0,1,0,0], [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0], [0,0,1,1,0,0,1,0,0,1,1,0,0,0,0,0,0,0,0,0], [0,0,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,1,0,0], [0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0] ", "fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"], "fundColor": ["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"] } } ] } } Not sure what i am doing wrong. I am getting the following error message: > Parse error on line 19: ... "fundRelation": "[1,0,0,1,0,0,1,0,1, > -----------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[' at http://jsonlint.com/
Here problem is, you are trying to assign multi line string value to fundRelation which is not valid JSON. .... "fundRelation": "[1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0],[0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]", ... Or you can do something like this : { "took": 12, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 44, "max_score": 1, "hits": [ { "_index": "transactions", "_type": "transaction", "_id": "trans0007", "_score": 1, "_source": { "fundRelation": [ [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0], [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0] ], "fundName": ["Fund A","Fund B","Fund C","Fund D","Fund E","Fund F","Fund G","Fund H","Fund I","Fund J","Fund K","Fund L","Fund M","Fund N","Fund O","Fund P","Fund Q","Fund R","Fund S","Fund T"], "fundColor":["#9ACD32","#377DB8","#F5DEB3","#EE82EE","#40E0D0","#FF6347","#D8BFD8","#D2B48C","#4682B4","#00FF7F","#FFFAFA","#708090","#708090","#6A5ACD","#87CEEB","#A0522D","#FFF5EE","#2E8B57","#F4A460","#FA8072"] } } ] } }
You can't have multi line strings in javascript (except in the newish engines using the ` operator). You need to escape the end of each line of fundRelation by adding a \ to end end of each line. Alternatively, don't store the matrix data as a string. remove the quote at the beginning and end of the array and store it as a standard array
Looks like it's not ok with the " splitting in multiple lines. In any case, shouldn't that be like this: ... "fundRelation": [ [1,0,0,1,0,0,1,0,1,1,0,1,0,1,1,1,0,1,0,0], [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0], ... [0,0,1,1,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0] ] ... Note the extra enclosing brackets [...].
Mongo $and query returning no results
How do I approach writing a query to return all the records matching both match.id and player.name for the following collection? { "match": { "id": 1, "event": { "timestamp": "2015-06-03 15:02:22", "event": "round_stats", "round": 1, "player": { "name": "Jim", "userId": 45, "uniqueId": "BOT", "team": 2 }, "shots": 0, "hits": 0, "kills": 0, "headshots": 0, "tks": 0, "damage": 0, "assists": 0, "assists_tk": 0, "deaths": 0, "head": 0, "chest": 0, "stomach": 0, "leftArm": 0, "rightArm": 0, "leftLeg": 0, "rightLeg": 0, "generic": 0 } } } I've attempted it with both the following query statements, but had no luck -- they both return no results: db.warmod_events.find( { $and: [ { "match.id": 1}, { "player.name": 'Jim' } ] } ) db.warmod_events.find( { $and: [ { "match.id": 1}, { "event": { "player.name": "Jim" } } ] } ) I'm pretty new to Mongo and any guidance and explanation would help a bunch -- truthfully I've chosen to use Mongo for this project as the data I am working with is already presented in this form (the JSON) and, due to that, it seemed like a good opportunity to use and learn Mongo. I am referring to the documentation on the Mongo site currently. Thanks all
Try the following query: db.warmod_events.find({ "match.id": 1, "match.event.player.name": 'Jim' }) which will match documents where the match id is the same as the embedded document player name.
Nested JSON not working in d3.js
I am using nested JSON file in my data visualization using d3.js. I was referring to a previous Smilar Queastion. I created a jsfiddle of the answer of the question to see whether things are working. But i am confused why the code is not working. I have similar type of problem in my project. How can i solve that. Here is sample code I am using for printing the data in the form of a list d3.select("body").append("ul").selectAll("li") .data(data).enter().append("li").each(function() { var li = d3.select(this); li.append("p") .text(function(d) { return d.date; }); li.append("ul").selectAll("li") .data(function(d) { return d.hours; }) // second level data-join .enter().append("li") .text(function(d) { return d.hour + ": " + d.hits; }); });
It looks like the JSON wasn't formed correctly. I fixed the JSON like this - var data=[{ "date": "20120927", "hours": [ { "hour": 0, "hits": 823896 }, { "hour": 1, "hits": 654335 }, { "hour": 2, "hits": 548812 }, { "hour": 3, "hits": 512863 }, { "hour": 4, "hits": 500639 } ], "totalHits": "32,870,234", "maxHits": "2,119,767", "maxHour": 12, "minHits": "553,821", "minHour": 3 }, { "date": "20120928", "hours": [ { "hour": 0, "hits": 1235923 }, { "hour": 1, "hits": 654335 }, { "hour": 2, "hits": 1103849 }, { "hour": 3, "hits": 512863 }, { "hour": 4, "hits": 488506 } ], "totalHits": "32,870,234", "maxHits": "2,119,767", "maxHour": 12, "minHits": "553,821", "minHour": 3 }]; Also, I think there was a problem with the HTTP d3.js library not being loaded. I added an external HTTPS reference and it is working. Here is a working version of your fiddle - https://jsfiddle.net/ferlin_husky/wzuvob6m/