November Happy Hour will be moved to Thursday December 5th.

Filter with GetPropertyValue(..) for booleans

Vote:
 

I want to search the pages with GetPropertyValue(..) but I don't get any result with the below:

var results = SearchClient.Instance.Search()
            .Filter(p => p.GetPropertyValue("IsEmployee").Match("true"))
            .GetContentResult();


Is there a way to filter with the GetPropertyValue(..) for booleans like above?

#227544
Sep 07, 2020 15:08
- Sep 08, 2020 5:17
Or is there any alternative way to filter by passing a string as the property name?
Vote:
 

It would be better to access the property strongly typed, instead og using «magic strings»?

Why do no need the get  the value like this?

I think this approach is preferred, given IsEmployee is a boolean property:

var results = SearchClient.Instance.Search<Person>()
            .Filter(p => p.IsEmployee.Match(true))
            .GetContentResult();
#227570
Edited, Sep 08, 2020 7:06
- Sep 08, 2020 7:24
I always prefer to access the properties directly (Strongly typed) as above. But in my case, this IsEmployee() is an external method which is indexed using the Search conventions as below,
SearchClient.Instance.Conventions.ForInstancesOf
Vote:
 

I can't see the your entire convention in the comment, but if IsEmployee() is an extension method, just:

var results = SearchClient.Instance.Search()
            .Filter(p => p.IsEmployee().Match(true))
            .GetContentResult();

..if the return type is bool.

#227572
Edited, Sep 08, 2020 7:28
- Sep 08, 2020 7:31
Sorry, I think the above comment I made has some character limitations. So you cannot see all the comment text I made.
I agree with you. But I do not have access to that IsEmployee() external method. So It would be better if I could access the IsEmployee field in the Episerver Find by passing just a string as the property/field name.
Tomas Hensrud Gulla - Sep 08, 2020 7:52
How is this external method defined, so that you cannot access it?
Vote:
 

Hi!

It's not possible to filter on anything that is not indexed, so GetPropertyValue will not work for you. Sounds a bit weird that you cannot access the extension method if it's registered with SearchClient.Instance.Conventions.ForInstancesOf.

However, you can define your own extension method with whatever logic you want to return that boolean value and then include that field in the index:

public static bool IsEmployeeCustom(this Person person)
{
    return person.GetPropertyValue("IsEmployee") == "True";
}
SearchClient.Instance.Conventions.ForInstancesOf<Person>()
    .IncludeField(x => x.IsEmployeeCustom());

Then to filter:

SearchClient.Instance.Search<Person>()
   .Filter(x => x.IsEmployeeCustom().Match(true));

Don't forget to re-index the persons. :)

#227575
Sep 08, 2020 8:19
Vote:
 

The issue seems like indexing, Have you re-indexed your search after the changes?

#227578
Sep 08, 2020 9:54
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.