休眠搜索&"join&"在TextFullSearch上 [英] Hibernate search "join" on TextFullSearch

查看:76
本文介绍了休眠搜索&"join&"在TextFullSearch上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个实体:广告和类别;

I have 2 entities : Ad and Category;

我在Ad实体上有一个FullTextSearch(lucene),我想在广告与类别"之间建立一个联接"甜菜,或者以某种方式在Ad.category.id上添加一个where子句.

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 .

我的添加课程:

@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;
   ....
}

和类别:

@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;

我在Ad上使用了IndexedEmbedded,我认为应该将@field放在类别的if字段上.

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?

推荐答案

首先,请注意,您选择使用非标准属性名称(至少在JavaBeans约定方面)可能会导致Hibernate ORM和Hibernate出现问题.搜索,可能无法正确检测到属性.我不确定会不会,但是我只是警告您:如果不起作用,请尝试重命名属性.

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.

回到您的问题,您已经通过添加 @IndexedEmbedded 建立了链接.如果您在搜索查询中仅需要类别ID,则可以简单地使用 @IndexedEmbedded(includeEmbeddedObjectId = true,depth = 0)而不添加任何字段. depth = 0 是可选的,将仅确保除类别ID外没有嵌入任何内容.

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.

然后,您只需要在 Ad 上的查询中添加一个子句:

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 );

如果您希望字段名称不同,则必须在 __ Id 字段上添加 @DocumentId(name ="yourIdName")批注 Category 类.然后像这样查询:

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 );

这篇关于休眠搜索&amp;"join&amp;"在TextFullSearch上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆