Book Image

Lucene 4 Cookbook

By : Edwood Ng, Vineeth Mohan
Book Image

Lucene 4 Cookbook

By: Edwood Ng, Vineeth Mohan

Overview of this book

Table of Contents (16 chapters)
Lucene 4 Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Defining custom attributes


The built-in Attribute classes should suffice for most of the implementation, but if you encounter a situation where you need a custom attribute, you may do so by creating your own Attribute class. In this section, we will create an Attribute interface and then an implementation class extending AttributeImpl and a TokenFilter that will set the attribute value. Our example will be a simple exercise where we determine the gender for a token based on specific words. This is purely for illustration purpose only, so this is by no means a useful implementation for any application.

How to do it…

First, let's start with an interface:

public interface GenderAttribute extends Attribute {

    public static enum Gender {Male, Female, Undefined};

    public void setGender(Gender gender);

    public Gender getGender();
}

Let's now apply an implementation class extending from AttributeImpl:

public class GenderAttributeImpl extends AttributeImpl implements GenderAttribute {

  ...