0
votes

how to index and search for custom fields using Lucene or hibernate search. i cannot find a way to index the custom field. they are dynamic.

'custom fields' in here means they can be editabled by user,those fields are not hard code.

Any help will be thankful!

1

1 Answers

0
votes

Query of Custom Fields

Just use the projection API:

FullTextQuery hibernateQuery = fullTextSession
     .createFullTextQuery(luceneQuery)
     .setProjection("myField1", "myField2");
List results = hibernateQuery.list();

Using projections you get to read any field as long as it's STORED.

If it matches some property name of your indexed entities it will be materialized after being converted to the appropriate type (if you have a TwoWayFieldBridge); if not you will get the String value.

If for some reason you need to bypass this conversion or just want to have fun decoding the raw Lucene Document you can open an IndexReaderdirectly.

Indexing Custom Fields

When defining a FieldBridge you get to add as many fields as you like to the indexed Document, and you can name each of them as you like. The method parameter name is a hint - useful for example to scope the field name - but you can ignore it.

An example FieldBridge implementation writing multiple fields is the DateSplitBridge in the documentation.