1
votes

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?
            }
        }
    }
1
The indexing should be quite easy with a custom bridge, but I am not sure how you want to search. If you make fieldName the Lucene Document field name, you have the problem that you don't know which fields to search on. Also depending on the type of the field value you might need different analyzers. For example a number should be indexed and analyzed differently to a string. To get a better answer you should describe your use case a little better, in particular how you are planning to build the search queries (aka determine which fields to search on)Hardy
Hardy, thank you for your answer! there is the user case,: user can search by the fields that's Name,Age and Score. So those fields should be indexed into Lucene Document(Age and Score are dynamic,if user change 'Age' to 'Sex' , So we need to update the index). If indexing the custom field useing Hibernate search , how do i write the indexing annotation for the dynamic field?Jonathan
The key point is how to index the fieldName in Class CustomField.We can easy to index for 'id' property, bucuase it's hard code field. but how to index the dynamic property 'fieldName'?Jonathan

1 Answers

1
votes

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.