Criteria Builder query LIKE in json field - spring

I am trying to build a query with Criteria Builder(LIKE), to look for a string in JSONARRAY field like this:
[
{
"family_class": "Electric",
"family_name": "lightBulb"
},
{
"family_class": "Others",
"family_name": "Oil"
}
]
one option would be to look for the family_name attribute, or maybe check if it contains the string there.
if (residues != null && residues.length > 0) {
List<Predicate> predicates = new ArrayList<Predicate>();
for (String residue : residues) {
predicates.add(cb.like(root.get("jsonColumn"), residue.toLowerCase()));
}
cr.select(root).where(predicates.toArray(new Predicate[] {}));
Query<SyncCollectionPoints> q = sess.createQuery(cr);
List<SyncCollectionPoints> result= q.getResultList();
This is the error i get:
Unrecognized token 'oil': was expecting ('true', 'false' or 'null')
All i want is to return the lines that have that string in the jsonColumn field.

I got it to work like this:
#Formula(value = "lower(jsonColumn::text)")
private String residuesToSearch;
just a simple cast did the trick

Related

WPGraphQL "where" is not case-sensitive

Hi there I am using the WPGraphQL Plugin, however, I am having trouble with the query clause where:
query getCaseStudy {
caseStudies(where: {name : "something-like-this" }) {
nodes {
databaseId
}
}
}
the above code returns a valid and correct response, however, when I passed on the parameter name, like this:
query getCaseStudy {
caseStudies(where: {name : "something-like-this-" }) { //additional special characters
nodes {
databaseId
}
}
}
The results were still the same and the response was valid. Is this how the WPGraphQL works? I want to make it case-sensitive; is there a way to do it?

More Like This Query Not Getting Serialized - NEST

I am trying to create an Elasticsearch MLT query using NEST's object initializer syntax. However, the final query when serialized, is ONLY missing the MLT part of it. Every other query is present though.
When inspecting the query object, the MLT is present. It's just not getting serialized.
I wonder what I may be doing wrong.
I also noticed that when I add Fields it works. But I don't believe fields is a mandatory property here that when it is not set, then the MLT query is ignored.
The MLT query is initialized like this;
new MoreLikeThisQuery
{
Like = new[]
{
new Like(new MLTDocProvider
{
Id = parameters.Id
}),
}
}
MLTDocProvider implements the ILikeDocument interface.
I expect the serialized query to contain the MLT part, but it is the only part that is missing.
This looks like a bug in the conditionless behaviour of more like this query in NEST; I've opened an issue to address. In the meantime, you can get the desired behaviour by marking the MoreLikeThisQuery as verbatim, which will override NEST's conditionless behaviour
var client = new ElasticClient();
var parameters = new
{
Id = 1
};
var searchRequest = new SearchRequest<Document>
{
Query = new MoreLikeThisQuery
{
Like = new[]
{
new Like(new MLTDocProvider
{
Id = parameters.Id
}),
},
IsVerbatim = true
}
};
var searchResponse = client.Search<Document>(searchRequest);
which serializes as
{
"query": {
"more_like_this": {
"like": [
{
"_id": 1
}
]
}
}
}

Spring MongoDB : querying documents with two equal fields

I want to query that returns document that two fields of it are equal
I found mongodb raw query from this question :
db.coll.find({ $where : "this.field1 == this.field2" } );
How can I perform it with spring criteria:
criteria = criteria.andOperator(
Criteria.where("successfulSent").is("true"),
Criteria.where("this.fieldOne == this.fieldTwo"));
but its not working beacuse generated query become :
{ ... "$and" : [ { "successfulSent" : "true"} , { "this.fieldOne == this.fieldOne " : { }}]}
You can try on this way:
Criteria.where("$where").is("this.field1 == this.field2")
Query toString() will be:
Query: { "$where" : "this.cts == this.uts"}, Fields: null, Sort: null

Json.NET LINQ query for single JProperty on JArray of JObjects

I have a JArray with this configuration
[
{
"name" : "text",
"age" : 32
},
{
"name" : "text2",
"age" : 33
},
]
and I want to use LINQ query to select a JArray containing JObjects with just the key value of a specified given key.
For example:
GetCollectionOfPropertiesByKey("name");
this would result in:
[
{
"name" : "text"
},
{
"name" : "text2"
},
]
using the same logic,
GetCollectionOfPropertiesByKey("age");
would result in:
[
{
"age" : 32
},
{
"age" : 33
},
]
You didn't specify whether you needed case-invariant filtering for properties by name, so here is an extension method with an optional StringComparison argument:
public static class JsonExtensions
{
public static JArray GetCollectionOfPropertiesByKey(this JArray array, string key, StringComparison comparison = StringComparison.Ordinal)
{
if (array == null)
return null;
var query = array
// Filter out array items that are not objects
.OfType<JObject>()
// Select the value JToken with the specified key using the specified StringComparison
.Select(o => o.GetValue(key, comparison))
// Filter for null (property was not found)
.Where(v => v != null)
// And select a new JObject containing just the JPropery parent of the selected value
.Select(v => new JObject(v.Parent));
return new JArray(query);
}
}
If a case-insensitive match is required, pass StringComparison.OrdinalIgnoreCase.
Notes:
The query filters for all items in the input array that are of type JObject, then uses JObject.GetValue(String, StringComparison) to get the corresponding value by key name.
Then, if a value is found, it constructs a new JObject using the parent of the selected value, which should be a JProperty.
If case insensitivity is never required, you could simplify the query a bit by using JObject.Property(String) to get the JProperty by key name, and then constructing a JObject containing it, like so:
var query = array
// Filter out array items that are not objects
.OfType<JObject>()
// Select the JProperty with the specified key
.Select(o => o.Property(key))
// Filter for null (property was not found)
.Where(p => p != null)
// And select a new JObject with just this property
.Select(p => new JObject(p));
Sample working .Net fiddle.

MongoDB and Guid in Linq Where clause

I store an object in mongodb which contains a Guid. Mongodb converts the Guid to a binary value
{
"_id" : ObjectId("52cf4a467b302a4797db23e8"),
"name" : "test",
"guid" : new BinData(3, "qZ8PQdmDv0+K500wnj6skA=="),
}
I get an empty result set when I use the Guid in a Linq expression and the count is always 0.
var queryable = _database.GetCollection<MyObject>("myname").AsQueryable();
var guid = new Guid("410f9fa9-83d9-4fbf-8ae7-4d309e3eac90");
var count = queryable.Where(x => x.Guid == guid).Count();
The reason is that Linq generates the following request
count: "member_variables", query: { panelid: "410f9fa9-83d9-4fbf-8ae7-4d309e3eac90" }
but the request should be
count: "member_variables", query: { panelid: new BinData(3, "qZ8PQdmDv0+K500wnj6skA==") }
I tried various Linq expressions, but none worked.
queryable.Where(x => x.Guid.ToString() == guid.ToString())
throws the exception: "Unable to determine the serialization information for the expression: x.Guid.ToString()."
queryable.Where(x => x.Guid == new BsonBinaryData(guid));
throws the exception: "Unsupported where clause: ((BsonBinaryData)x.Guid == UuidLegacy:0x24f7ceb84f06d143b6426e8f01cb7825)."
How can I request the documents?
Note: I need to use IQuerable and can't use Mongodb queries.

Resources