Elasticsearch custom scoring function test null date values - elasticsearch

I can't find anywhere examples of how to test null values in ES custom scoring functions.
According to the doc the scripts are in groovy, according to the log the script is evaluated in painless, but even with that I'm left puzzled by some errors
"script":"doc['response_rate'].value ? (doc['response_rate'].value + 1) : 0",
"lang":"painless",
"caused_by":{
"type":"wrong_method_type_exception",
"reason":"cannot convert MethodHandle(Doubles)double to (Object)boolean"}}}]
This seems to say I'm trying to cas a double to boolean and raises, but I need to test for non-null values.
How should I write my scoring script ?
EDIT : I have understood that in painless I cannot use the ternary ? : operator, so I must write explicitely doc['xx'].value != null. However, this seems to produce weird results for dates that were indexed with null values. It would seem that in painless the value is NOT null (although it is indeed null in the json when I GET /_search it) and the following does not work
"script":"(doc['unavailable_until'].value != null) ? 1 : 0"
and always seem to return 0 (as if the null date was actually not null). I have seen some people reporting something some weird default date, in this case how can I compare this date to something like Date.now ?

I wonder why I couldn't find this page before...
Basically, one can just call .empty to check for null values. Works with dates too
"script":"doc['unavailable_until'].empty ? 1 : 0",

Related

Java 8 streams, find object by predicate with null checks

I have a list of objects which I want to select one with a specific value but only if its null and I having a hard time doing this with java 8 streams.
String id = invoice.getDeliveries().stream().filter(delivery -> delivery.getName().equals("ABC")).findFirst().orElse(null).getId;
This code does not work when for example getName() returns null because then .getId will throw an exception.
How can I write this so that the value of id will just become null if ABC is null or does not exist?
Not a java expert, but maybe this works:
Delivery delivery = invoice.getDeliveries().stream().filter(delivery -> delivery.getName().equals("ABC")).findFirst().orElse(null);
String id = delivery == null ? null : delivery.getId;
Just change delivery.getName().equals("ABC") with "ABC".equals(delivery.getName()) and then check for null.
P.S. There is a good practice, to placce literals (like ABC) first. E.g. IntelliJ IDEA inspections check this.

How to check if a json path/value entries exists in a huge json object in oracle sql?

Let's say the json object is like :
{
"title":"<title goes here>",
"version":"1.0.0",
"name":"<name goes here>",
"features": {
"feature-id1":{
"id":"090ccd9c-8930-4aa9-9aa3-f9c8e6747a27",
"name":"feature1",
"version":"1.0.0"
}
"feature-id2":{
"id":"932twe8e-2184-4er9-5qw3-g4e9w9821w71",
"name":"feature2",
"version":"1.0.0"
}
...
}
}
I want to check whether there exists a json path/value entries (or) a nested json object in the above json data.
I have tried with json_exists(json_column,'$.features?(#."feature-id1" == "{"id": "090ccd9c-8930-4aa9-9aa3-f9c8e6747a27","name":"feature1","version":"1.0.0"}")');
But this is giving me an error "JZN-00229: Missing parenthesis in paranthetical expression".
Can't we use json_exists() to check for json objects/arrays with values?
In Postgresql this can be achieved by using the '#>' operator : json_data->'features'->'feature-id1' #> 'provide the json value'
Is there a way to achieve the same in Oracle SQL? I'm using Oracle 19c with latest patch(19.15).
At least in Oracle (I don't know other database products - you mention Postgre and say it has capabilities that Oracle doesn't have), comparison filters like you are trying to apply only work for scalar values, not for nested objects.
If I understand what you are trying to do, then in Oracle you would do that with something like
json_exists(json_column,'$."features"."feature-id1"?(
#."id" == "090ccd9c-8930-4aa9-9aa3-f9c8e6747a27"
&& #."name" == "feature1"
&& #."version" == "1.0.0"
)'
)
although this is not exactly equivalent to what you were trying to do. Namely, this filter only ensures the existence of a key "feature-id1" with value a nested object that has at least the three keys "id", "name" and "version", with the prescribed values - but it may also have other keys. So the object value of "feature-id1" may not be equal to the prescribed object in your attempt, which has exactly three keys, no more. Will that work for you? Only you know.

ArangoDB IS_DATE AQL Function

I have a number of items in a collection that have a dateDue field on them. Some have this dateDue field set to an ISO8601 date, and others have null, and some others have a blank string. I want to do the following:
filter DATE_COMPARE(DATE_NOW(), dateDue, "years", "days")
However, if dateDue is null or a blank string this throws an error. I can add this to solve it:
filter w.dateDue != null and w.dateDue != ""
filter DATE_COMPARE(DATE_NOW(), dateDue, "years", "days")
But that only covers null values and blank strings. Sure this may be enough, but I would like to ensure that any record that does not have a valid date for the dateDue field is excluded. I was expecting to see an IS_DATE function for Arango, but I couldn't find one in the manual anywhere. Any ideas?
There is currently no IS_DATE function or something similar in AQL, so using filters as above is definitely one way to do it.
To make it a bit easier in the future, an IS_DATESTRING AQL function has been added in the 2.8 branch today. This will allow writing the filter as follows:
FILTER IS_DATESTRING(w.dateDue)
FILTER DATE_COMPARE(DATE_NOW(), w.dateDue, "years", "days")

Is there such thing as an empty date in C#

I am trying to input a date into an Oracle database. Sometime this value will be null. So in the function that I am writing I need a way to be able to pass in this null value. I am passing in this date as a parameter to stored proc. This parameter can be null. I am using the Oracle.DataAccess dll to get this thing to work. If it is indeed null, I am thinking of just throwing in a null variable. Do you think that would work??? Here is how I currently am setting up this scenario...
cmd.Parameters.Add("ACTIVE_DATEIn", DateTime.Parse(ActiveDate));
conn.Open();
outcome = cmd.ExecuteNonQuery();
Active Date is the possible null variable that I am going to pass in. Obviously you can't Convert a null value into a date Time. What would you guys suggest doing?
Since DateTime is a struct, it cannot contain a null value.
Some people use DateTime.MinValue to store a null value. A better approach would perhaps be to make ActiveDate a DateTime? instead.
Update:
You can also try something like:
cmd.Parameters.Add("ACTIVE_DATEIn", (ActiveDate == null ? OracleDate.Null : OracleDate.Parse(ActiveDate)));
You can make DateTime implement INullable by placing a ? after the type.
DateTime? date;
Which is ofcourse equivalent to
Nullable<DateTime> date;
You can use DateTime.MinValue instead of null, or make your DateTime object nullable.
You may need to use DBNull.Value in the case that your ActiveDate represents a null.

DBnull in Linq query causing problems

i am doing a query thus:
int numberInInterval = (from dsStatistics.J2RespondentRow item in jStats.J2Respondent
where item.EndTime > dtIntervalLower && item.EndTime <= dtIntervalUpper
select item).count();
there appear to be some dbnulls in the endtime column..
any way i can avoid these?
tried adding && where item.endtime != null.. and even != dbnull.value
do i have to do a second (first) query to grab all that arent null then run the above one?
im sure its super simple fix, but im still missing it.. as per
thanks
nat
I think you want to use item.EndTime.HasValue, and not item.EndTime == null.
The simplest way to do it is to use .GetValueOrDefault(...Some reasonable default...) on the value that can be null and it will avoid the error.
The way I typically do this in a T-SQL query is to use ISNULL on the date, and set the date to something like '12/31/2099' when it's null.
With Linq it could be something like this:
from t in MyTable
where
Convert.ToString((Convert.ToString(t.MyDateColumn) ?? "12/31/2099")) < MyTargetValue

Resources