FlexSearch / FlexLucene

IKVM Build scripts
Apache License 2.0
16 stars 5 forks source link

Writing a custom TokenFilter using FlexLucene #8

Closed bahaabeih closed 8 years ago

bahaabeih commented 8 years ago

I have been using FlexLucene for while now with great success. I wrote a custom TokenFilter along with a custom TokenFilterFactory to be used in an analysis chain using CustomAnalyzer as follows (Java version, which works fine):

...
CustomAnalyzer myCustomAnalyzer = CustomAnalyzer.Builder()
                        .WithTokenizer("keyword")
                        .AddTokenFilter(Class.forName("MyCompany.MyProduct.CustomFilterFactory"))
                        .Build();
...

This is the .NET version, which doesn't work:

...
CustomAnalyzer myCustomAnalyzer = CustomAnalyzer.Builder()
                        .WithTokenizer("keyword")
                        .AddTokenFilter(Class.forName("MyCompany.MyProduct.CustomFilterFactory"))
                        .Build();
...

I get the following exception messages:

Exception thrown: 'ClassNotFoundException' in IKVM.Runtime.dll
Exception thrown: 'java.lang.ClassNotFoundException' in IKVM.Runtime.dll

Is there a way to get this to work?

vladnega commented 8 years ago

Hi @bahaabeih!

Thank you for your interest.

I suppose that in the .NET version, you are creating your CustomFilterFactory in .NET (not in Java, then converting to .NET).

If this is the case, then the following code should work:

CustomAnalyzer myCustomAnalyzer = CustomAnalyzer.Builder()
                        .WithTokenizer("keyword")
                        //.AddTokenFilter(Class.forName("MyCompany.MyProduct.CustomFilterFactory"))
                        .AddTokenFilter(Class.forName(typeof(MyCompany.MyProduct.CustomFilterFactory).AssemblyQualifiedName))
                        .Build();

Note that I am using the AssemblyQualifiedName as opposed to using the FullName of the CustomFilterFactory class. This is how IKVM likes it.

Here is the implementation of the CustomFilterFactory, just for good measure:

namespace MyCompany.MyProduct
{
    public class CustomFilterFactory : TokenFilterFactory
    {
        public CustomFilterFactory(Map m) : base(m) {}

        public override TokenStream Create(TokenStream ts)
        {
            throw new NotImplementedException();
        }
    }
}

Let me know if it works.

bahaabeih commented 8 years ago

Hello @vladnega. Thanks for the response. It worked like a charm!