Elasticsearch: Return empty value from script field - elasticsearch

I'm using Elasticsearch, and I have a script field written in Groovy. It needs to replace the value of myField for some values, but I want to let most of the values pass through unchanged. The script below works except for documents that don't have the field at all. How can I make the script field value empty?
if (some criteria) {
return ...; // modified value
}
// This returns 0 if myField doesn't exist
return doc['myField'].value
I can check the empty condition, but I haven't found a way to return an empty value:
if (doc['myField'].empty)
return ???

It's perfectly ok to return null. I think the problem is more in the sequencing of your different test conditions. I've tried to reproduce your issue and I'm able to return null with the following script:
"script_fields": {
"my_script_field": {
"script": "if (doc.myField.empty) { return null } else if (some criteria) { return modifiedValue } else { return doc.myField.value }"
}
}
First check for fields with empty values and return null.
Then, you're guaranteed to have non-empty and existing fields. So you can check for your criteria and return whatever modified field you want.
Finally, you can return the unmodified values for non-empty fields which do not meet your specified criteria.
The script looks like this:
if (doc.myField.empty) {
return null
} else if (some criteria) {
return modifiedValue
} else {
return doc.myField.value
}

Related

How can I modify a graphQL query based off of the variable passed to it?

I have built this query to request data from the Pokeapi graphQL server here
https://beta.pokeapi.co/graphql/console/
query getPokemon (
$typesList: [Int!],
) {
pokemon_v2_pokemon(
where: {
pokemon_v2_pokemontypes: { type_id: { _in: $typesList } }
}
) {
id
name
pokemon_v2_pokemontypes {
type_id
pokemon_v2_type {
name
}
}
}
}
How can I modify the query to return all results when the $typesList list is empty? I would like the query to return a filtered results list when $typesList has a list of ids, but it should return all values when the $typesList list is empty.
Is this something that is possible in graphQL?
Thank you
If you controlled the server you could write the resolver to function in the manner you're looking for but since you don't your only option is to wrap your use of the query with something that sets the default typesList variable to all possible types.

How to do evaluation in TYPO3 FlexForms and prevent saving invalid input

I would like to check a FlexForm field with the additional behaviour: If the entered value is not correct, it should not be possible to save the form. This is a similar behaviour as the "required" eval function, which refuses to save empty fields.
The code for evaluation already exists (but I am not adding entire code):
class UsernameEvaluation
{
public function evaluateFieldValue($value, $is_in, &$set)
{
if ($value) {
$errorCode = StudipPerson::checkUsername($value);
// if wrong username, should not be possible to save form
if ($errorCode != StudipPerson::USERNAME_ERROR_OK) {
$set = false;
}
}
return $value;
}
}
Even though invalid data is entered and I checked with a debugger that $set was set to false, the value is saved.

Laravel 5.7 many to many sync() not working

I have a intermediary table in which I want to save sbj_type_id and difficulty_level_id so I have setup this:
$difficulty_level = DifficultyLevel::find(5);
if ($difficulty_level->sbj_types()->sync($request->hard, false)) {
dd('ok');
}
else {
dd('not ok');
}
Here is my DifficultyLevel.php:
public function sbj_types() {
return $this->belongsToMany('App\SbjType');
}
and here is my SbjType.php:
public function difficulty_levels() {
return $this->hasMany('App\DifficultyLevel');
}
In the above code I have dd('ok') it's returning ok but the database table is empty.
Try to change
return $this->hasMany('App\DifficultyLevel');
to
return $this->belongsToMany('App\DifficultyLevel');
The sync() method takes an array with the id's of the records you want to sync as argument to which you can optionally add intermediate table values. While sync($request->hard, false) doesn't seem to throw an exception in your case, I don't see how this would work.
Try for example:
$difficulty_level->sbj_types()->sync([1,2,3]);
where 1,2,3 are the id's of the sbj_types.
You can read more about syncing here.

How do you return GraphQL elements that are empty or have no value?

What's the recommended way to return the value of an element with no value? Do you exclude it from the selection list or set it to null/none? Irrespective, what's the best way to code this from the client's perspective?
try {
display (obj.name, obj.val);
} catch(e)
{
log(e);
}
or
if obj.name && obj.val
display(obj.name, obj.val);

LINQ Statement dictionary first key's value

I have a dictionary
private Dictionary<string, string> ScannerMessages;
and then I have a call for this
public bool equalMessages()
{
lock (lockObj)
{
return (ScannerMessages.Values.ToList().Distinct().Count() < ScannerMessages.Values.ToList().Count) ? true : false;
}
}
And it returns if the messages are equal. I also need to add to the end of that clause a second check to verify that the values are not null or empty. I was trying to do something like this but I am not sure where to go with the LINQ statement (not the greatest at LINQ)...
public bool equalMessages()
{
lock (lockObj)
{
return ((ScannerMessages.Values.ToList().Distinct().Count() < ScannerMessages.Values.ToList().Count) && (ScannerMessages.Keys.First() *get the value here and check it !null or string.empty) ? true : false;
}
}
To verify all the values are not null or empty:
ScannerMessages.Values.All(s => !string.IsNullOrEmpty(s))
It will return true if all values are not null and not empty.
If you want to get the non-null values you can do this:
ScannerMessages.Values.Where(v=>!string.IsNullOrEmpty(v)).ToList()
or just to get the count
ScannerMessages.Values.Where(v=>!string.IsNullOrEmpty(v)).Count()
or
ScannerMessages[ScannerMessages.Keys.First()].Where(v=>!string.IsNullOrEmpty(v)).Count()

Resources