2

I have an entity with documentid

@Indexed
@Analyzer(impl = EnglishAnalyzer.class)
@Entity
@Table(name = "Tag", schema = "cpsc")
public class Tag extends BaseObject {

public static final String NAME_FIELD = "name";

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "uuidGenerator")
@GenericGenerator(name = "uuidGenerator", strategy = "mypackage.UuidGenerator")
@Column(name = ID_FIELD)
@DocumentId
private Long id;

...

My generator will create an unique Long id and stores it to the database after save.

But how can i get documentid in lucene or set is the same as my entity id? it would be very useful to get a field with lucene document id.

The main reason why i need it is getting term from index for the entity for this i need the lucene id. Maybe there is another way to get terms hibernate-search way?

Thanks for any advice.

1 Answers1

1

A couple of things here. First of all, you don't have to specify @Id as well as @DocumentId. The JPA id is automatically taken as document id when you don't specify @DocumentId. It really only makes sense to use @DocumentId if you want explicitly use a different field as Lucene document id.

To answer your question - "But how can i get documentid in lucene or set is the same as my entity id" - they are already the same. And there is already a document id field which you for example can retrieve via projection. The projection constant to use is FullTextQuery.DOCUMENT_ID. See also http://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#projections.

Hardy
  • 18,659
  • 3
  • 49
  • 65
  • 1
    To add on the excellent answer, let's not confuse the "document id" used by Hibernate Search to identify the relation with the entity stored in the database with the "docId" used internally by Lucene. The Lucene docId simply is an ordinal relative to the segment (or full index) and will possibly change after writes. For example, a specific hit might be the fifth entry in the index, and then later be the sixth when another document is inserted. Using Projection allows you to read this value but keep in mind the read value is going to change. Use FullTextQuery.ID for a stable reference. – Sanne Sep 16 '14 at 11:07