0

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!

Jonathan
  • 403
  • 2
  • 7
  • 16

1 Answers1

0

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.

Sanne
  • 6,027
  • 19
  • 34
  • So how do I index the dynamic fields ?For example: I have two records, Record A has a hard-code field "Name" and a dynamic field "Custom Field1". Record B has a hard code field "Name" and a dynamic field "Custom Field2". so how do I index for the dynamic fields 'Custom Field1' and 'Custom Field2', beacuse the users can cunstom the field for each Record . It's 'Field_A' for record_1, but maybe it's 'Field_B' for record_2. Do you understand what i mean? – Jonathan Oct 08 '12 at 09:09
  • Right, forgot about the indexing aspect. Updated my answer. – Sanne Oct 08 '12 at 22:45
  • Sanne , thank you for your answer, can you have a look this question? http://stackoverflow.com/questions/12650379/does-lucene-support-searching-between-multiple-index-documents – Jonathan Oct 09 '12 at 07:34