Developer forum

Forum » CMS - Standard features » New Index - Create boolean field based on input empty or not

New Index - Create boolean field based on input empty or not

Nuno Aguiar
Reply

Hi,

 

Is there a way to (through the UI) create a boolean field based on another (string) field being emtpy or not?

 

I tried to create a Boolean field (analyzed) from a String field (as source) in the hope that empty/null would convert to False and everything else to True. I got nothing :)

I know I can do this with an IndexBuilderExtender, but sounds like a basic flag I would expect to get with the steps above. Can anyone confirm if that's the case or not?

 

Best Regards,

Nuno Aguiar


Replies

 
Dmitriy Benyuk
Reply
This post has been marked as an answer

Hi Nuno,

it seems it is not possible to do this in the UI. But you could try to write your index builder extender by implementing the "Dynamicweb.Indexing.IIndexBuilderExtender" interface
and inside the ExtendDocument method you could access the all fields and their values before they are going to the index, so you could overwrite them with your needed bool values depending on the string value it has (when you use some String field as a Source for your custom bool field).

Regards, Dmitrij

Votes for this answer: 1
 
Nuno Aguiar
Reply

Hi Dmitriy,

 

Yep, we'll do that.

 

Wouldn't it make sense though to wrap it in a TryParse statement?

if(Boolean.TryParse({{ValueFromSourceField}}, out isBool))
{
  // If we get in here, we know that isBool is a valid boolean, 
  // either "True" or "False".
}
else {
  // treat it as "False"
}

This would ensure we could parse some strings to Boolean more easilly.

 

Best Regards,

Nuno Aguiar

 
Dmitriy Benyuk
Reply

Hi Nuno,
it is up to you, but as you said that if the string is null or emprty is false value then I suppose the code to be like:

if(!doc.ContainsKey(SourceField))
{
//also add check if the document contains YourBoolField
//if not then add it, after that you can set the value
  doc[YourBoolField] = false;
}else{

if(string.IsNullOrEmpty(doc[SourceField])
{
//also add check if the document contains YourBoolField
//if not then add it, after that you can set the value
  doc[YourBoolField] = false;
}
else {
  doc[YourBoolField] = true;
}

}

 

 
Nuno Aguiar
Reply

Hi Dmitriy,

 

Yep I see. And you are right, it's not that straight forward to determine that a filled-in field should be set to true or false.

 

It's fixed anyway. Thanks,

 

Nuno

 
Nicolai Pedersen
Reply

Btw, it never makes sense to Analyze fields that are not strings...

 
Nuno Aguiar
Reply

Hi Nicolai,

 

Yes, point taken. I was assuming an Analyzer would have to be triggered to determine whether or not an empty string were to be considered "False" or not, but the standard Analyzer consider strings only.

 

What I may end up developing is an Analyzer that considers a set of rules we typically use to determine Boolean values based on a string field. When that happens the field would have to be analyzed, but the field type would not be "Boolean" but our "BooleanFromStringAnalyzer", right?

 

Best Regards,

Nuno

 
Nicolai Pedersen
Reply

No.

Do not use analyzers on fields that are not strings in the index. I repeat. Do not do that... An analyzer breaks a STRING into terms/tokens and normalize them - and that makes no sense on boolean fields. Anyways, the analyzed flag is disregarded if you define your field as anything else than string.

You can create an analyzer that will be used on a string field on the index. Based on the text in the product field, you index either "True" or "False" in that string field.

 
Nuno Aguiar
Reply

Hi Nicolai,

 

Ok, I see the problem with my logic. I was trying to develop a generic method to take a fields and index a boolean value associated with it to work as flags. Example:

  • Has Image - if an image field is not empty and not null
  • Has Video - if a custom field for video is not empty and not null

 

An index builder extender will be the way to go on this.

 

Thanks,

Nuno Aguiar

 
Nicolai Pedersen
Reply
This post has been marked as an answer

ok, looked into this and found this.

The index does not support a System.Boolean field (even though our UI for index setup supports it). That means that even if you define a field as boolean, it is saved as a string (if the original value is a string).

So, I just made this change. If you define a field of system.boolean and index a string field into that, it will store the string "false" if the field value is null/empty, 'false', '0', 'off' or 'no' - otherwise the field stores the value "true". So in you case you could create a field in the index saying "ContainsValue" as System.Boolean and if the field has a value, it is set to true otherwise false. Out in 8.9.1

Votes for this answer: 1
 
Nuno Aguiar
Reply

Hi Nicolai,

 

Wow, thanks this is perfect. I'll wait for 8.9.1 then. I love to have as little custom development as possible on projects.

 

Best Regards,

Nuno Aguiar

 

You must be logged in to post in the forum