Hibernate search “join” on TextFullSearch

落爺英雄遲暮 提交于 2019-12-11 14:17:35

问题


I have 2 entities : Ad and Category;

I have a FullTextSearch (lucene) on Ad entity and I want to make a "join" beetween Ad And Category or to add somehow a where clause on Ad.category.id .

My Add class :

@Entity
@Indexed
@Table(name = "adds")
public class Ad implements Serializable {
@Id
@SequenceGenerator(name="AdSQ", sequenceName="AdSQ", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "AdSQ")
@Column(name = "id")
@JsonProperty("Id")
private Long __Id;


@OneToOne(fetch = FetchType.LAZY)
@IndexedEmbedded
@JoinColumn(name="idCategory", foreignKey = @ForeignKey(name = "FK_AD_CATEGORY"))
@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})
Category category; 

@Column(name = "price")
@JsonProperty("Price")
@Field
private Double _Price;
   ....
}

And Category :

@Entity
@Table(name = "categories"/*, indexes = {@Index(columnList="id",name = "index_category")}*/)
public class Category implements Serializable{
    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator="CategorySQ")
    @SequenceGenerator(name="CategorySQ", sequenceName="CategorySQ", allocationSize = 1)
    @JsonProperty("Id")
    @Column(name="id")
    private Long __Id;

I used IndexedEmbedded on Ad and I think I should put @field on if field of category.

How should I make the link ? I need to use a special type of query, analyzer?


回答1:


First, be aware that you choice to use non-standard property names (at least with respect to the JavaBeans conventions) may cause some trouble with Hibernate ORM and Hibernate Search, which might not detect the properties correctly. I'm not sure it will, but I'm just warning you: if it does not work, try renaming the properties.

Back to your question, you already made the link by adding an @IndexedEmbedded. If you only need the category ID in your search queries, you may simply use @IndexedEmbedded(includeEmbeddedObjectId = true, depth = 0) without adding any field. depth = 0 is optional and will simply ensure that nothing except the category ID is embedded.

Then you will only have to add a clause to your query on Ad:

Query categoryQuery = queryBuilder.keyword().onField( "category.__Id" ).matching( <some ID> ).createQuery()
Query booleanJunction = queryBuilder.bool()
    .must( categoryQuery )
    .must( <some other query> )
    .must( <some other query> )
    .createQuery()
FullTextQuery fullTextQuery = fullTextEntityManager.createQuery( booleanJunction, Ad.class );

If you want the name of the field to be different, you will have to add a @DocumentId(name = "yourIdName") annotation on the __Id field of the Category class. Then query like this:

Query categoryQuery = queryBuilder.keyword().onField( "category.yourIdName" ).matching( <some ID> ).createQuery()
Query booleanJunction = queryBuilder.bool()
    .must( categoryQuery )
    .must( <some other query> )
    .must( <some other query> )
    .createQuery()
FullTextQuery fullTextQuery = fullTextEntityManager.createQuery( booleanJunction, Ad.class );


来源:https://stackoverflow.com/questions/50852565/hibernate-search-join-on-textfullsearch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!