Jonathan
Jonathan

Reputation: 413

How to index and search dynamic field names and values

I have two Classes.

public Class Student
{
  //primary key
  private String id;
  private String name;//name = Jonathan
  ....
  private List<CustomField> customFields;
}

public Class CustomField 
{
  //primary key
  private String id;

  private String fieldName;
  ....
  private String fieldValue;
}

customFields are defined by user, can be any fields and values.For example: there are two objects in customFields list. they are [id = 001,fieldName = age,value = 30] [id=002,fieldName = score,value = 90]. (Also user can add/update some necessary field in the customFields list,the customFields is dynamic)

So if the class 'Student' send to my web page , it will display:Name:[Jonathan] Age:[30] Score:[90] on web page.

User case: user can search by the fields that's Name,Age and Score. So those fields should be indexed into Lucene Document. If indexing the custom field useing Hibernate search , how do i write the indexing annotation for the dynamic field?

So I need to index and search the dynamic customFields using Hibernate Search,Ho can i implelement it? Do you know what i mean?

Edit:

public class CustomFieldBriddge implements FieldBridge
{

    public void set(String name, Object value, Document document, LuceneOptions luceneOptions)
    {
        Field.Store store           = luceneOptions.getStore();
        Field.Index index           = luceneOptions.getIndex();
        Field.TermVector termVector = luceneOptions.getTermVector();
        Float boost                 = luceneOptions.getBoost();
        if (value != null)
        {
            List<CustomField> customFields              = (List) value;
            for (CustomField customField : customFields)
            {

                String fieldName = customField.getFiledName();
                String fieldValue = customField.getFiledValue();
                Field field = new Field(fieldName, fieldValue, store.YES, index.NOT_ANALYZED,
                    termVector);    // is the field will index into Lucene document one by one?and it can be searched out? right?
                field.setBoost(boost);
                document.add(field); // is this operation will index the field into Lucuene Document?
            }
        }
    }

Upvotes: 1

Views: 3331

Answers (1)

Hardy
Hardy

Reputation: 19119

Have a look how to implement a custom FieldBridge - http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#d0e4019

Using a custom field bridge you basically will get the list of CustomField instances together with the Lucene Document passed to you. It is then up to you to create Fieldables. In your case CustomField#fieldName becomes the Lucene Document field name and CustomField#fieldValue the field value. Have a look at the DateSplitBridge example.

Upvotes: 1

Related Questions