'CustomPropertyMatchInformation does not match existing terms SharePoint Online Term Store CSOM

I am new to SharePoint CSOM. I have a Azure function app that updates a term set in SharePoint Online term store. The function app uses a local custom property defined on the terms to find them. I use the below code to search for the terms

protected async Task<TermCollection> GetTermsByCustomProperty(TermSet termSet, string propertyName, string propertyValue)
    {
        var matchedTerms = termSet.GetTermsWithCustomProperty(new CustomPropertyMatchInformation(termSet.Context)
        {
            CustomPropertyName = propertyName,
            CustomPropertyValue = propertyValue,
            TrimUnavailable = false
        });

        termSet.Context.Load(matchedTerms);
        await termSet.Context.ExecuteQueryRetryAsync();

        return matchedTerms;
    }

Sometimes the above code does not return any terms when I know that one or more terms exist with the propertyValue defined on them. This happens at random. Other times it will find the matching terms. I am not sure what I can try to find the cause of the issue. Any help will be highly appreciated.

Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source