Querying an embedded list in OrientDB - java

I have a document in my OrientDB database (version 1.0.1), with a structure largely like this:
{
"timestamp": "...",
"duration": 665,
"testcases": [
{
"testName": "test01",
"type": "ignore",
"filename": "tests/test1.js"
},
{
"iterations": 1,
"runningTime": 45,
"testName": "test02",
"type": "pass",
"filename": "tests/test1.js"
},
...
{
"testName": "test05",
"type": "ignore",
"filename": "tests/test1.js"
}
]
}
How can I query across the entire list, eg. if I want to find all documents that contain a testcase with the type "ignore"?
I've attempted the following query
select from testresult where testcases['type'] = 'ignore'
but this results in a NumberFormatException.
select from testresult where testcases[0]['type'] = 'ignore'
works, but obviously only looks at the first list element of each document.
select from testresult where testcases contains(type = 'ignore')
Doesn't provide any results, but the query is accepted as valid.
Update:
The following query works as intended, if the testcases are stored as separate documents instead of as an embedded list.
select from testresult where testcases contains (type = 'ignore')

I know it's an old question but I had the same problem and just stubled upon an answer here:
https://www.mail-archive.com/orient-database#googlegroups.com/msg00662.html
The following should work. It does in my very similiar use case.
select from testresult where 'ignore' in testcases.type

I have similar problem and end up with :
select * from testresult
let $tmp = (select from
(select expand(testcases) from testresult )
where
value.type = 'ignore')
where
testcases in $tmp.value
this will give you all the testresult documents that contains at least one testcase which type is ignore. This query works on embedded lists. Take care that expand function is available in OrientDB >= 1.4.0.
The inner query :
select from (select expand(testcases) from testresult) where value.type='ignore'
select only the different testcases with a type = 'ignore'. The result are testcases. To have the whole document we match those testcases with the ones contained in each document (testcases in $tmp.value).
I don't know if there is a simpler way to query embedded list...

Try
select from testresult where testcases traverse ( type = 'ignore' )
Check the traverse operator ( https://groups.google.com/forum/?fromgroups#!topic/orient-database/zoBGmIg85o4 ) to know how to use the fetchplan or put any() instead of testcases just after "where".
For example we have a class called Country which has an embeddedlist property with some of its isoCodes. If we attempt the following query :
select name,description,isoCodes,status from Country where isoCodes traverse ( value = 'GB' OR value = 'IT' )
Orientdb Rest interface provides :
{
"result": [{
"#type": "d", "#version": 0,
"name": "Italy",
"isoCodes": [
{
"#type": "d", "#version": 0,
"code": "iso3166-A2",
"value": "IT"
},
{
"#type": "d", "#version": 0,
"code": "iso3166-A3",
"value": "ITA"
}],
"status": [
{
"#type": "d", "#version": 0,
"status": "1",
"startingDate": "2012-04-24"
}]
}, {
"#type": "d", "#version": 0,
"name": "United Kingdom",
"isoCodes": [
{
"#type": "d", "#version": 0,
"code": "iso3166-A2",
"value": "GB"
},
{
"#type": "d", "#version": 0,
"code": "iso3166-A3",
"value": "GBR"
}],
"status": [
{
"#type": "d", "#version": 0,
"status": "1",
"startingDate": "2012-04-24"
}]
}
]
}
Hope it helps!!.
Regards.

Related

Elasticsearch Multimatch substring not working

So I have a record with following field :
"fullName" : "Virat Kohli"
I have written the following multi_match query that should fetch this record :
GET _search
{
"query": {
"multi_match": {
"query": "*kohli*",
"fields": [
"fullName^1.0",
"team^1.0"
],
"type": "phrase_prefix",
"operator": "OR",
"slop": 0,
"prefix_length": 0,
"max_expansions": 50,
"zero_terms_query": "NONE",
"auto_generate_synonyms_phrase_query": true,
"fuzzy_transpositions": true,
"boost": 1
}
}
}
This works fine.
But when I remove the letter 'k' from query and change it to :
"query": "*ohli*"
It doesn't fetch any record.
Any reason why this is happening? How can I modify the query to get the record returned with the above modification?
first let me explain you why your existing query didn't work and then the solution of it.
Problem : you are using the multi_match query with type phrase_prefix and as explained in the documentation it makes a prefix query on the last search term and in your case you have only 1 search term so on that Elasticsearch will perform the phrase query.
And prefix query works on the exact tokens, and you are using standard analyzer mostly, default for text fields so for fullName field it will have virat and kohli and your search term also generates kohli(notice smallcase k) as standard analyzer also lowercase the tokens, above you can check with the explain API output in your first request as shown below.
"_explanation": {
"value": 0.2876821,
"description": "max of:",
"details": [
{
"value": 0.2876821,
"description": "weight(fullName:kohli in 0) [PerFieldSimilarity], result of:",
"details": [
{
(note he search term in the weight)
Solution
As you are trying to use the wildcard in your query, best solution is to use the wildcard query against your field as shown below to get results in both case.
{
"query": {
"wildcard": {
"fullName": {
"value": "*ohli",
"boost": 1.0,
"rewrite": "constant_score"
}
}
}
}
And SR
"hits": [
{
"_shard": "[match_query][0]",
"_node": "BKVyHFTiSCeq4zzD-ZqMbA",
"_index": "match_query",
"_type": "_doc",
"_id": "1",
"_score": 1.0,
"_source": {
"id": 2,
"fullName": "Virat Kohli",
"team": [
"Royal Challengers Bangalore",
"India"
]
},
"_explanation": {
"value": 1.0,
"description": "fullName:*ohli",
"details": []
}
}
]

Json response to be deserialized in Apex salesforce lightning

I am getting the below Json Response by hitting a RestAPI.
**
[
{
"index": 0,
"object": {
"attributes": {
"ID": [
{
"type": "configuration/entityTypes/Customer/attributes/ID",
"ov": true,
"value": "5",
"uri": "entities/attributes/ID/2Cqf5xG2"
},
{
"type": "configuration/entityTypes/Customer/attributes/ID",
"ov": true,
"value": "3028",
"uri": "entities/attributes/ID/fto38ZRR"
}
],
"IgpId": [
{
"type": "configuration/entityTypes/Customer/attributes/IgpId",
"ov": true,
"value": "1911",
"uri": "entities/attributes/IgpId/20ft8omYM"
}
],
"IgpName": [
{
"type": "configuration/entityTypes/Customer/attributes/IgpName",
"ov": true,
"value": "Dr. heath",
"uri": "entities/attributes/IgpName/20ft8oe1q"
}
],
"IgpID": [
{
"type": "configuration/entityTypes/Customer/attributes/IgpID",
"ov": true,
"value": "1872",
"uri": "entities/attributes/IgpID/20ft8oiI6"
}
],
"IgpNAME": [
{
"type": "configuration/entityTypes/Customer/attributes/IgpNAME",
"ov": true,
"value": "Dr Jc crook",
"uri": "entities/attributes/IgpNAME/20ft8oqoc"
}
]
}
},
"successful": true
}
]
**
I am trying to save the values for CoachID (3028), IgpID(1872), IgpId(1911), IgpName(Dr. heath), IgpNAME(Dr Jc crook) in String variables in APex class.
I have used a map to do so but unable to get the proper values. I wanted to get the above values in a List & return that list from function.
Can anyone help me on this? I am not supposed to do this using wrapper.
Regards,
Mohit
It can't be done as straight "JSON.deserialize" call because some fields are reserved keywords in Apex. How strict the wrapper ban is? I wouldn't want to type this stuff freehand but you can go to https://json2apex.herokuapp.com/, tick checkbox about explicit parser (again, since you have reserved keywords it'd happen anyway) and well, you have a ready class & cool stab at unit test / example how to use the code. It's battle-tested, if you can't use that then your project has bigger problems ;)
If you really really have to do it alone and with maps it'll be something like https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_class_System_Json.htm, example for deserializeUntyped

How to fetch different type of documents from a single collection - Mongodb - MongoTemplate

I have a collection with different type of documents. Have different types of network tables & different type of entries of the tables in the single collection. depending of type document is different.I am trying to fetch these document based on some filter queries. As i have different documents i cant create a pojo with fixed fields because fields can vary according to the document so i can query like this. I first want to fetch tables & later their entries based on table selection. Table document does not have "entry" key so i am using query like this - {type:{$exists:true},entry:{$exists:false}}
return mongoTemplate.find(query, NetworkTable.class);
Have tried using Document.class instead of NetworkTable.class. Have tried with BasicQuery & Criteria but its not working. Query used to fetch tables only - Its working on mongodb shell.
//BasicQuery query = new BasicQuery("{role:{$exists:true}}{type:{$exists:true},entry:{$exists:false}}");
Query query = new Query();
query.addCriteria(Criteria.where("entry").exists(flase));
return mongoTemplate.find(query, Document.class,"user"); //user is collection name
Its giving below error when i use Document.class instead of NetworkTable.class -
{
"timestamp": 1591963169373,
"status": 500,
"error": "Internal Server Error",
"exception": "org.springframework.data.mapping.model.MappingInstantiationException",
"message": "Failed to instantiate org.springframework.data.mongodb.core.mapping.Document using constructor NO_CONSTRUCTOR with arguments ",
}
Need quick help to resolve this.
Documents-
{
"_id": {
"$oid": "5ee2313e3694087f868576e2"
},
"_class": "org.json.simple.JSONObject",
"_href": "https://1.1.1.1/config/xyz/2/table/ABC",
"_display_name": " Table \"ABC\",
"_state": "ok",
"name": "ABC",
"description": "",
"type": "type1",
"xyz": "2",
"_short_name": "\"ABC\" "
},
{
"_id": {
"$oid": "5ee2313f3694087f868576e4"
},
"_class": "org.json.simple.JSONObject",
"_href": "https://1.1.1.1/config/xyz/2/table/ABC/entry/1",
"_display_name": "Entry 1",
"apply-destination-number-manipulation": false,
"type": "destination-tgid",
"xyz": "2",
"_children": [
],
"entry": "1",
"_state": "ok",
"apply-traffic-group": false,
"apply-source-number-manipulation": false,
"name": "ABC",
"action": "reject",
"destination-sip-adjacency": {
"_href": ""
},
"match-tgid": "any",
"destination-tgid-manipulation": "none",
"source-tgid-manipulation": "none",
"_short_name": "1"
},
{
"_id": {
"$oid": "5ee2313f3694087f86812344"
},
"_class": "org.json.simple.JSONObject",
"_display_name": "Entry 1",
"_href": "https://1.1.1.1/config/xyz/2/table/LLKORTELCSCS/entry/1",
"_short_name": "1",
"_state": "ok",
"action": "complete",
"apply-destination-number-manipulation": false,
"apply-source-number-manipulation": false,
"apply-traffic-group": false,
"xyz": "2",
"cost": 1,
"destination-adjacency": {
"_display_name": " \"LLKORTELCSCS_A\"",
"_href": "https://1.1.1.1/config/adjacency/sip/LLKORTELCSCS_A",
"_short_name": "LLKORTELCSCS_A"
},
"destination-tgid-manipulation": "none",
"entry": "1",
"name": "LLKORTELCSCS",
"route-on-cost": "set-cost",
"source-tgid-manipulation": "none",
"type": "least-cost",
"weight": 5
}

tExtractJSONField From tFileInputJSON - Talent Open Studio

I am very new to Talend Open Studio for DI. I am trying to read data from the below JSON File :
{
"data": [
{
"id": "X999_Y999",
"from": {
"name": "Tom Brady", "id": "X12"
},
"message": "Looking forward to 2010!",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X999/posts/Y999"
},
{
"name": "Like",
"link": "http://www.facebook.com/X999/posts/Y999"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
},
{
"id": "X998_Y998",
"from": {
"name": "Peyton Manning", "id": "X18"
},
"message": "Where's my contract?",
"actions": [
{
"name": "Comment",
"link": "http://www.facebook.com/X998/posts/Y998"
},
{
"name": "Like",
"link": "http://www.facebook.com/X998/posts/Y998"
}
],
"type": "status",
"created_time": "2010-08-02T21:27:44+0000",
"updated_time": "2010-08-02T21:27:44+0000"
}
]
}
I want to load three attributes into my table ( id, actions_name and actions_link). So, in the first step (tFileInputJSON) - I tried to do a Loop Json query as below:
Here, am able to extract the rows as I needed. But, then I used a tExtractJSONField to extract individual fields under "actions" for each "id" using XPath expressions as below:
I tried several other ways to extract the fields but could not do this. Also, not able to find any correct post in stack overflow and talent forums very relevant to my question. Could somebody please help?
Arrange the job like ,
tFileInputJSON is like,
tExtractJSONFields is like,
Then you will get output as,

Update Embedded Documents in array without Repeating in MongoDb using Java

I have the following structure in my document:
{
"_id": 11111,
"user": "user#mail.com",
"sites": [
{
"sitename": "site1",
"url": "site2.com",
"keywords": [],
},
{
"sitename": "site2",
"url": "site2.com",
"keywords": [],
},
{
"sitename": "site2",
"url": "site2.com",
"keywords": [],
},
]
},{
"_id": 2222,
"user": "user2#mail.com",
"sites": []
}
I'm going to find the document by ID(id:11111).there is multiple documents. Then I want to add new site to sites array in the document that contain 1111 _id. if sitename already have, the site should not add. How can I achieve this ?
Updated Answer :
You can check that the _id is '11111', then check that the sitename doesn't exist and is not equal to the new sitename. And then you can $push the embedded document into the array.
db.collection.update(
{
_id:11111,
'sites.sitename' : {$exists:false},
'sites.sitename' : {$ne:'site4'}
},
{
$push:
{
'sites':
{
"sitename": "site4",
"url": "site4.com",
"keywords": []
}
}
},
{
multi:true
}
)
something like
db.collection.update({_id:11111, 'sites.sitename' : {$exists:false}},
{$set : {'sites.$.sitename':'newsite.com'}},{multi:true})
Edit
if you want to add a sub-document for sitename
var sub = {'a':'a','b':'b'};
db.collection.update({_id:11111, 'sites.sitename' : {$exists:false}},
{$set : {'sites.$.sitename':sub}},{multi:true})

Categories

Resources