Reputation: 2207
I have the following hibernate search mapping :
public class Courrier {
@IndexedEmbedded(depth = 2)
@OneToMany(mappedBy = "courrier", cascade = CascadeType.ALL)
@OrderBy("id")
private List<CourrierClassification> classifications;
The indexation is ok but the field names are the same for each entry of the list :
classifications.id
classifications.id
...
Is there a way to append the element index to the field name so that i can differenciate the entries ( the order matters )
classifications1.id
classifications2.id
....
Upvotes: 0
Views: 347
Reputation: 2207
I used a FieldBridge to add fields with custom names to the documents. it works.
@Field
@FieldBridge(impl = ClassificationFieldBridge.class)
@OneToMany(mappedBy = "courrier", cascade = CascadeType.ALL)
@OrderBy("id")
private List<CourrierClassification> classifications;
The field bridge Impl :
public class ClassificationFieldBridge implements FieldBridge {
@Override
public void set(String name, Object value, Document luceneDocument, LuceneOptions luceneOptions) {
......
List<CourrierClassification> listClassifications = (List<CourrierClassification>) value;
int i = 1;
for (CourrierClassification courrierClassification : listClassifications) {
ValeurGenerique valeurGenerique = courrierClassification.getValeur();
Field field = new Field(name + i + ".valeur.rechercheLuceneNonTokenise", valeurGenerique.getRechercheLucene(), Field.Store.NO, Field.Index.NOT_ANALYZED);
luceneDocument.add(field);
Upvotes: 0
Reputation: 19119
No you cannot. You cannot keep the order of the list elements. Also, if the fields names would differ as you suggest, how would you know which fields to target in your query?
You could write a custom bridge in which case you can add arbitrary field names. I am just mentioning this for completeness. I would not recommend to encode list order into the field names. Maybe you could explain your use case and what and how you want to query? There might be other ways to achieve what you are after.
Last but not least, unless there is a cyclic reference in your classes, aka CourrierClassification
references Courrier
again, there is no need for the depth
attribute.
Upvotes: 3