increment nested JSONDBObject value in java - java

I have a BasicDBObject of following structure in JAVA
"category_id": "keyboard",
"ontology_id": "mobile",
"path": "#mobile#keyboard",
"date": "2013/008/01 12:30:00",
"hour": {
"12": {
"total_docs": 1
}
}
I need to increment total_docs value by 1 but i am not able to access that object. How should i proceed in this case

never mind, we just need to append the value in string and it's done

Related

Is there a way to extract all values by some key from a list of maps using SpEL?

Let's assume there's a map corresponding to the following structure:
{
"lists": [
{
"list": [
{
"letter": "a"
},
{
"letter": "b"
}
]
},
{
"list": [
{
"letter": "c"
},
{
"letter": "d"
}
]
}
]
}
There's an easy way to get all lists using SpEL (#root['lists']) or all letters of the first list ("#root['lists'][0]['list']") or the first letter of the first list ("#root['lists'][0]['list'][0]"). Also, there's a projection mechanism which allows using construction like "#root['lists'].![#this['list']]" to convert each item of lists to a result of the projection expression.
However, given all these possibilities, I still failed to come up with an expression allowing me to extract all letters from both lists. For the example above, I'd like to get
[
{
"letter": "a"
},
{
"letter": "b"
},
{
"letter": "c"
},
{
"letter": "d"
}
]
I tried to use the projection mechanism to achieve my goal but it didn't really help. The problem I see is that every time SpEL detects a list, it applies the projection expression to each element of the list so any structure of nested lists can't be changed this way.
The problem I solve can be easily solved using JsonPath, however, I assume I could get something wrong and didn't notice a way to achieve the same result using SpEL. I would be happy to listen to any ideas.

ShEx Validation - reason and appInfo are null in Result Shape Map

I am learning ShEx and using 'shexjava API' done by http://shexjava.lille.inria.fr/ for my project. I have schema, data graph and fixed shape map. When I validate using refine and recursive validation, I am getting ResultShapeMap but the reason and appInfo are null for NONCONFORMANT status. I do not understand why these two fields are null.
I have schema, dataGraph, shapeMap. This is code for validation.
ValidationAlgorithm vl = new RefineValidation(schema, dataGraph);
ResultShapeMap result = vl.validate(shapeMap);
Shape is,
{
"#context": "http://www.w3.org/ns/shex.jsonld",
"type": "Schema",
"shapes": [
{
"id": "http://example.com/ns#HouseShape",
"type": "Shape",
"expression": {
"type": "EachOf",
"expressions": [
{ "type": "TripleConstraint",
"predicate": "http://example.com/ns#number",
"valueExpr": { "type": "NodeConstraint",
"datatype": "http://www.w3.org/2001/XMLSchema#String"
}
},
{ "type": "TripleConstraint",
"predicate": "http://example.com/ns#size",
"valueExpr": { "type": "NodeConstraint",
"datatype": "http://www.w3.org/2001/XMLSchema#decimal"
}
}
]
}
}
]
}
Data is,
ex:House1 a ex:House ;
ex:number "11A" ;
ex:size 23 .
My Result is,
ResultShapeMap [
associations= [
ShapeAssociation [
nodeSelector=<example.com/ns#House>,
shapeSelector=<example.com/ns#HouseShape>,
status=NONCONFORMANT,
reason=null,
appInfo=null
]
]
]
I want to output the reason for not conforming. But it gives me null for that.
Could some one please help me.
The shexjava implementation currently does not support indicating a reason for failure.
This is because when a node does not satisfy a shape there may be several reasons.
If you want to learn ShEx, I would advise you to use ShapeDesigner
https://gitlab.inria.fr/jdusart/shexjapp/
which provides a graphical interface in which you can explore validation results.
In this particular case, it indicates that the validation fails because 23 is not a decimal (it's actually an integer) Screenshot of validation exploration result in ShapeDesigner
I do not know whether this is a bug, i.e. whether integrers should be considered to be also decimals in RDF.

Two kinds of search on a field?

I work on a project that uses elasticsearch 5.2. The code is in java and I use elasticsearch java client 5.2.
In this project, I have a field called hash, a 7-character code containing uppercase letters, lowercase letters, and numbers (in English language).
I want to do two searches on this field:
check the existence of a hash "ErTg1Qh" (case sensitive)
find hashes that sub string s contains in them (for example, sub string "tg" exist in the hash "ErTg1Qh").
For the hash field, I chose the keyword datatype.
I used matchQuery function for first search as below:
String hash = "ErTg1Qh";
QueryBuilders.matchQuery("hash", hash)
and queryStringQuery function for the second search as below:
queryString = "hash:*" + subString + "*";
QueryBuilders.queryStringQuery(queryString)
but, the second one does not work properly.
How can I handle these two kinds of search on a field?
One of your query requires to be case sensitive while the second one is case insensitive. So I'll suggest you to use sub field for the hash field. Your main field will be analyzed with lowercase analyzer and one will store raw data i.e. the exact hash. So your index will look like below:
PUT /test
{
"settings": {
"number_of_shards": "1",
"number_of_replicas": "0",
"analysis": {
"analyzer": {
"custom_lowercase": {
"filter": [
"lowercase"
],
"type": "custom",
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"hash": {
"type": "text",
"analyzer": "custom_lowercase",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
}
}
}
Query to check the existence of a hash "ErTg1Qh" (case sensitive)
POST /test/_doc/_search
{
"query": {
"match": {
"hash.keyword": "ErTg1Qh"
}
}
}
Query to find hashes that sub string s contains in them
POST /test/_doc/_search
{
"query": {
"query_string": {
"query": "*tg*"
}
}
}

Lambda Java - Iterate a Json Object. It looks like inside is duplicated

I have a json like this and I use forEach to go over family
{
"people": {
"family": [
{
"id": "123",
"member": [
{
"id": "11",
"salary": false
},
{
"id": "12",
"salary": false
}
]
},
{
"id": "124",
"member": [
{
"id": "11",
"salary": false
},
{
"id": "12",
"salary": false
}
]
}
]
}
}
For instance, if I set the salary in true in the first member id 123, when i go through the second element of family (id 124), salary is also in true.
If I change something in the second one id 124, the first id 123 has the same value. It looks like every family element it's the same object but not sure how I can find out that and also how to fix it.
The code is really big, but basically I'm doing something like this:
.forEach(family -> {
family.getId() // I get 123 and then 124
family.getMember().forEach(member -> {
System.out.println(member.getSalary()) // Show false for id 123 but then for id 124, show true when i would be false at this point
member.setSalary(true);
Any idea? not sure what else I can do.... Thanks
Check if objects in member collection for 123 and 124 are different, have different reference. It sounds like those objects has the same reference in those two collections. Try maybe to override equals and hashCode.
so I am assuming the corrected result should be:
false
false
false
false
this statement member.setSalary(true); is shouldn't be affected
"member" is should be in separate object it shouldn't refer to same reference (pretty weird)
can u post the full block of code?

Java - Dynamic JSON creation with selected option

I need to prepare JSON object like below, based on the user selected option dynamically. What is best approach to dynamically set value of "selected" key in JAVA. I have if-else and Switch in my mind but I am looking for better approach.
[{
"name": "By Distance",
"selected": "FALSE"
},
{
"name": "By Rating",
"selected": "TRUE"
},
{
"name": "By Cost",
"selected": "FALSE"
}
]

Categories

Resources