Im using an ocr api to scan the image. here's my get request
let request = http.request(options, function(res) {
res.setEncoding('utf8');
res.on('data', function (data) {
let JSONresponce = JSON.parse(data);
console.log(JSONresponce.ParsedResults)
});
});
request.on('error', function(e) {
console.log('Cant scan the image');
});
request.end();
Here's the json structure i'm supposed to get
{
"ParsedResults" : [
{
"TextOverlay" : {
"Lines" : [
{
"Words": [
{
"WordText": "Word 1",
"Left": 106,
"Top": 91,
"Height": 9,
"Width": 11
},
{
"WordText": "Word 2",
"Left": 121,
"Top": 90,
"Height": 13,
"Width": 51
}
.
.
.
More Words
],
"MaxHeight": 13,
"MinTop": 90
},
.
.
.
.
More Lines
],
"HasOverlay" : true,
"Message" : null
},
"FileParseExitCode" : "1",
"ParsedText" : "This is a sample parsed result",
"ErrorMessage" : null,
"ErrorDetails" : null
},
{
"TextOverlay" : null,
"FileParseExitCode" : -10,
"ParsedText" : null,
"ErrorMessage" : "...error message (if any)",
"ErrorDetails" : "...detailed error message (if any)"
}
.
.
.
],
"OCRExitCode" : "2",
"IsErroredOnProcessing" : false,
"ErrorMessage" : null,
"ErrorDetails" : null
"SearchablePDFURL": "https://....." (if requested, otherwise null)
"ProcessingTimeInMilliseconds" : "3000"
}
Since im logging only ParsedResults this is what im getting
[
{
TextOverlay: {
Lines: [],
HasOverlay: false,
Message: 'Text overlay is not provided as it is not requested'
},
TextOrientation: '0',
FileParseExitCode: 1,
ParsedText: 'hello world',
ErrorMessage: '',
ErrorDetails: ''
}
]
But the only thing i need is ParsedText field. And when i try to get it either via
JSONresponce.ParsedResults.ParsedText
or
JSONresponce.ParsedResults['ParsedText']
Im getting either just undefined response or an error msg with same meaning.
Is there a way to get the ParsedText field ?
The JSON object is a array with one item. (Notice the [] characters) You need JSONresponce.ParsedResults[0]['ParsedText'] to get the first (and only) item from the array.
Related
I have below simple table with title and code column from elasticsearch like below
{ title: 'test1', code: 'ZjS3d3k8z',... },
{ title: 'test2 with more words', code: 'AjS3d2k1z',... }
I am trying to filter title and code by using wildcard as shown below,
{
"sort": [
{
"creation_date": {
"order": "desc"
}
},
"_score"
],
"query": {
"bool": {
"must": [
{
"match": {
"account_id": 100
}
}
],
"should": [
{
"wildcard": {
"code": "?test2 with"
}
},
{
"wildcard": {
"title": "*test2 with*"
}
}
],
"minimum_number_should_match": 1
}
}
}
The above query works with expected results /fails with no results on below case
> 'test1' <---- works(return expected result) when i try to search single word
> 'test2' <---- works(return expected result) when i try to search single word
> 'test2 with'<---- fails(return empty result) when i try to search more than one word with space'
> 'test2 with more words' <---- fails(return empty result) when i try to search full title
When i try search code like below, It always return empty result!!!
"should": [
{
"wildcard": {
"code": "?ZjS3d3k8z"
}
},
{
"wildcard": {
"title": "*ZjS3d3k8z*"
}
}
]
I want to search title/code with partial/full value from millions of records using elasticsearch. Kindly advise.
For wildcard perform search on "keyword" field instead of "text" . For text data type, string is analyzed, broken into tokens while keyword field are stored as it is.
"test2 with more words" is stored as ["test2","with","more","words"] and your wild card input is being matched against these tokens hence no doc is returned.
{
"query": {
"bool": {
"should": [
{
"wildcard": {
"code.keyword": "*test2 with*"
}
},
{
"wildcard": {
"title.keyword": "*test2 with*"
}
}
],
"minimum_should_match": 1
}
}
}
Mappings created with dynamic template have keyword sub field for each text type. If you have created the mapping explicitly then you need to add the keyword subfield
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "text"
}
}
}
Wild cards have poor performance, there are better alternatives.
Match/Match_phrase/Match_phrase_prefix
if you are searching for whole tokens like "test2 with". You can simply a match query, all documents with contain tokens "test2" and "with" will be returned
"should": [
{
"match": {
"code": {
"query": "test2 with",
"operator": "and"
}
}
},
{
"match": {
"title": {
"query": "test2 with",
"operator": "and"
}
}
}
]
If order of tokens matter, you can use match_phrase.
If you want to search for partial tokens, use match_phrase_prefix.
Prefix match is only done on last token in search input ex. "test2 w"
Edge N grams
The edge_ngram tokenizer first breaks text down into words whenever it
encounters one of a list of specified characters, then it emits
N-grams of each word where the start of the N-gram is anchored to the
beginning of the word.
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "my_tokenizer"
}
},
"tokenizer": {
"my_tokenizer": {
"type": "edge_ngram",
"min_gram": 2,
"max_gram": 10,
"token_chars": [
"letter",
"digit"
]
}
}
}
}
}
Tokens generated:
"tokens" : [
{
"token" : "te",
"start_offset" : 0,
"end_offset" : 2,
"type" : "word",
"position" : 0
},
{
"token" : "tes",
"start_offset" : 0,
"end_offset" : 3,
"type" : "word",
"position" : 1
},
{
"token" : "test",
"start_offset" : 0,
"end_offset" : 4,
"type" : "word",
"position" : 2
},
{
"token" : "test2",
"start_offset" : 0,
"end_offset" : 5,
"type" : "word",
"position" : 3
},
{
"token" : "wi",
"start_offset" : 6,
"end_offset" : 8,
"type" : "word",
"position" : 4
},
{
"token" : "wit",
"start_offset" : 6,
"end_offset" : 9,
"type" : "word",
"position" : 5
},
{
"token" : "with",
"start_offset" : 6,
"end_offset" : 10,
"type" : "word",
"position" : 6
}
]
this will let you perform search like "tes wit" i.e any token can be partial unlike match_phrase_prefix which only does prefix search on last token
N Grams
The ngram tokenizer first breaks text down into words whenever it
encounters one of a list of specified characters, then it emits
N-grams of each word of the specified length.
N-grams are like a sliding window that moves across the word - a
continuous sequence of characters of the specified length
You can search for partial token even in middle "est" will match test
Suggesters
They provide search as you type functionality
You can choose any among these based on your requirement
I'm trying to update objects within an array in a document. The objects are not subdocuments and therefore don't have a unique ID. I've tried looking at various methods to do this but it just doesn't seem to have any effect (as in, nothing happens - no errors, but the document doesn't update.
Probably easier just to show the code. First, here's an example of a document in this collection:
{
_id: 123,
//other fields go here
submissions: [
{
submission: 1,
downloaded: null, //This should be an ISODate when populated
assessor: null, //This should be an ObjectID when populated
feedbackUploaded: null, //This should also be an ISODate
path: '401Test1.doc',
assessorNotes: null //This should be a string when populated
},
{
submission: 2,
downloaded: null,
assessor: null,
feedbackUploaded: null,
path: '401Test2.doc',
assessorNotes: null
}
]
}
And now my mongoose query (within an express route):
const unit = req.body.unit; //e.g. 123
const submission = req.body.submission // e.g. 2
const date = Date.now();
Unit.update({ "_id": unit, "submissions.submission": submission }, {
$set: {
'submissions.assessorNotes': req.body.assessorComments,
'submissions.feedbackUploaded': date,
'submissions.assessor': req.body.assessor
}, function(err){
//callback
}
})
I've also tried this using $ notation in the $set command i.e. 'submissions.$.assessorNotes': req.body.assessorComments' but this doesn't seem to work either.
Where am I going wrong?!
Cheers!
Chris
Just tested this with the positional operator as mentioned above by TomG in a shell.
> db.foo.find().pretty()
{
"_id" : 123,
"submissions" : [
{
"submission" : 1,
"downloaded" : null,
"assessor" : null,
"feedbackUploaded" : null,
"path" : "401Test1.doc",
"assessorNotes" : null
},
{
"submission" : 2,
"downloaded" : null,
"assessor" : null,
"feedbackUploaded" : null,
"path" : "401Test2.doc",
"assessorNotes" : null
}
]
}
Then I ran the update command:
db.foo.update(
{ _id: 123, "submissions.submission": 2 },
{ $set: {
"submissions.$.assessorNotes": "testAssessorNotes",
"submissions.$.feedbackUploaded": new Date(),
"submissions.$.assessor": "testAssessor"
} }
)
Results are as follows:
{
"_id" : 123,
"submissions" : [
{
"submission" : 1,
"downloaded" : null,
"assessor" : null,
"feedbackUploaded" : null,
"path" : "401Test1.doc",
"assessorNotes" : null
},
{
"submission" : 2,
"downloaded" : null,
"assessor" : "testAssessor",
"feedbackUploaded" : ISODate("2016-10-26T15:33:09.573Z"),
"path" : "401Test2.doc",
"assessorNotes" : "testAssessorNotes"
}
]
}
Edit: My hunch is that the error could also be with the update in Mongoose, you could try changing both to the positional operators, as well as using findOneAndUpdate if it fits your needs instead of update.
Please try this query for Mongoose:
Unit.update(
{ "_id": unit, "submissions.submission": submission },
{ $set: {
'submissions.$.assessorNotes': req.body.assessorComments,
'submissions.$.feedbackUploaded': date,
'submissions.$.assessor': req.body.assessor
} }, function(err, result) {
//callback
}
});
I'm trying to parse JSON from Jenkins's API using request
request({method: 'GET', url}, function(error, response, body) {
console.log(body.toString());
var output_json = JSON.parse(body.toString());
console.log(output_json);
}
After I parse the JSON usingJSON.parse(), few values in the tags are lost.
Console output of text output before parsing JSON
{
"_class" : "My.FreeProject",
"actions" : [
{
},
{
"_class" : "FreeProject.Property",
"parameterDefinitions" : [
{
"_class" : "org.choice.Parameter",
"defaultParameterValue" : {
"_class" : "Property",
"value" : "master19.7.0"
},
"description" : "",
"name" : "BUILD_TAG",
"type" : "ChoiceParameter"
},
{
"_class" : "Parameter",
"defaultParameterValue" : {
"_class" : "Value",
"value" : ""
},
"description" : "Random Text",
"name" : "MY_TEST",
"type" : "StringParameterDefinition"
},
{
"_class" : "org.myclass",
"defaultParameterValue" : {
"_class" : "org.newclass"
},
"description" : "",
"name" : "TESTING",
"type" : "NodeParameterDefinition"
}
]
},
{
Console output of text output after parsing JSON
{ _class: 'My.FreeProject',
actions:
[ {},
{ _class: 'FreeProject.Property',
parameterDefinitions: [Object] },
{},
{},
{},
{},
{},
{},
{},
{},
{ _class: 'com.myclass' } ],
So after parsing JSON, I'm losing some of the text values. Is there a way I could retrieve all the information of the JSON from Jenkins? Thanks
It doesn't look like anything is missing. The value of parameterDefinitions is just collapsed. Either there is a toggle you can click on to expand it, or use console.dir instead.
Example from the Chrome console. Note how it shows [Array[1]] instead of [[[[]]]] after parsing the string into an object. However, the values is still four nested arrays.
I'm working with the JsTree Ajax lazy load feature and to display the data I am using JsTree-grid. However, I am having the issue with display the data using the JsTree-grid on the second column. I've the following JSON data that I'm passing through the PHP using the JsTree ajax lazy load feature:
[
{
"id": 157749,
"parent": "Sheet1.worksheet1webs44lx8GbsHu9igMc2vM_qWJqhePuo257PKZm_6Uo",
"text": "Script 1: Login",
"data": {
"status": "Fail"
}
},
{
"id": 104511,
"parent": "Sheet1.worksheet1webs44lx8GbsHu9igMc2vM_qWJqhePuo257PKZm_6Uo",
"text": "skip",
"data": {
"status": "Pass"
}
}
]
In the Javascript I've the following code:
$('#jstree').jstree({
plugins: ['grid'],
'grid': {
'columns': [
{
'width': 50,
'header': 'Nodes'
},
{
'width': 30,
'header': 'Status',
'value': function (node) {
return (node.status);
}
}
]
},
'core' : {
'data' : {
"url" : function (node) {
return node.id === '#' ? 'node' : 'tree/' + node.id;
},
'data' : function (node) {
return { 'id' : file.id, 'title' : file.title };
},
"dataType" : "json"
}
}
});
Note: When I console log the node on here: 'value': function (node) { console.log(node); } I got the following result on the console:
Object { status: "Fail" }
Object { status: "Pass" }
I would like to display the status Pass or Fail on the second JsTree-grid column. However, the JsTree-grid isn't displaying the data and I'm even not getting any error on the console. Please, can someone help me regarding what I want to achieve.
I see two issues here:
"parent": "Sheet1.worksheet1webs44lx8GbsHu9igMc2vM_qWJqhePuo257PKZm_6Uo"; your json doesn't have any parents for these nodes
you don't need function here - 'value': function (node) { return (node.status); }, simply use 'value': 'status'.
After fixing these two issues it works, check demo - Fiddle.
I am trying to create this:
[
{
"id":"1",
},
{
"id":"3",
},
{
"id":"5",
},
{
"id":"6",
},
{
"id":"9",
},
]
Person = {
"id" : Id
};
PersonArray.push(Person);
tempPersonJson['PersonList'] = JSON.stringify(PersonArray);
This is my output:
List = "[{\"id\":\"12\"},{\"id\":\"10\"},{\"id\":\"9\"},{\"id\":\"8\"},{\"id\":\"7\"},{\"id\":\"6\"},{\"id\":\"5\"},{\"id\":\"4\"},{\"id\":\"3\"},{\"id\":\"2\"},{\"id\":\"1\"},{\"id\":\"12\"},{\"id\":\"10\"},{\"id\":\"9\"},{\"id\":\"8\"},{\"id\":\"7\"},{\"id\":\"6\"},{\"id\":\"5\"},{\"id\":\"4\"},{\"id\":\"3\"},{\"id\":\"2\"},{\"id\":\"1\"}]";
API complains with this:
Call Body
Expected body: Content-Type is application/json but body is not a parseable JSONParse error on line 21: ...value": true },] ---------------------^ Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got ']'
Watchout for malformed JSON. Expected data media type ('null') does not match real media type ('application/json').
Update, JSON sent to server:
[{"id":"12"},{"id":"10"},{"id":"9"},{"id":"8"},{"id":"7"},{"id":"6"},{"id":"5"},{"id":"4"},{"id":"3"},{"id":"2"},{"id":"1"}]
So just taking a stab at it...>I created a jsfiddle for you....fixing some missing variables.
var Person = {
"id" : '1'
};
var PersonArray =[];
PersonArray.push(Person);
var tempPersonJson = [];
tempPersonJson['PersonList'] = JSON.stringify(PersonArray);
console.log('tp',tempPersonJson);
Working fiddle click here