搜索字段在List @IndexedEmbedded中

huangapple go评论69阅读模式
英文:

search field in List @IndexedEmbedded

问题

我有一个实体,其中包含一个实体列表,我在此列表中添加了@IndexedEmbedded以便能够进行搜索。所以我想搜索所有具有ID 123且属性x等于给定值的实体。我尝试了下面的代码,但出现了以下错误:

> 无法在com.xx.xx.AEntity中找到字段bEntity.x

@Entity
@Indexed
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class AEntity implements Serializable {

    private static final long serialVersionUID = 1L;
    
    @Id
    @Field
    private String id;

    @IndexedEmbedded
    @ElementCollection
    private Set<BEntity> bEntity;
    
}


@Entity
@IdClass(BPK.class)
public class BEntity implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Field(name = "x_number")
    private String x;

    @Id
    @Field(name = "y_number")
    private String y;
    
}

QueryBuilder queryBuilder = getFullTextEntityManager().getSearchFactory().buildQueryBuilder().forEntity(AEntity.class).get();    
Query query = queryBuilder.bool()
            .must(queryBuilder.keyword().onField("id").matching("123").createQuery())
            .must(queryBuilder.keyword().onField("bEntity.x").matching(str).createQuery())
            .createQuery();
英文:

I have an entity that has a list of entities, i added @IndexedEmbedded in this list to be able to search into it. So i want to search all entities that has the id 123 and the attribute x equals a given value. I tried with the code bellow but i have this error

> Unable to find field bEntity.x in com.xx.xx.AEntity

@Entity
@Indexed
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public class AEntity implements Serializable {

	private static final long serialVersionUID = 1L;
    
    @Id
    @Field
    private String id;

	@IndexedEmbedded
	@ElementCollection
	private Set&lt;BEntity&gt; bEntity;
	
}


@Entity
@IdClass(BPK.class)
public class BEntity implements Serializable {

	private static final long serialVersionUID = 1L;

	@Id
	@Field(name = &quot;x_number&quot;)
	private String x;

	@Id
	@Field(name = &quot;y_number&quot;)
	private String y;
	
}

QueryBuilder queryBuilder = getFullTextEntityManager().getSearchFactory().buildQueryBuilder().forEntity(AEntity.class).get();    
Query query = queryBuilder.bool()
			.must(queryBuilder.keyword().onField(&quot;id&quot;).matching(&quot;123&quot;).createQuery())
			.must(queryBuilder.keyword().onField(&quot;bEntity.x&quot;).matching(str).createQuery())
            .createQuery();

答案1

得分: 0

你的字段被称为 bEntity.x_number,而不是 bEntity.x

    @Field(name = "x_number")

这就是为什么你会收到这个错误消息。

在你的查询中使用 bEntity.x_number 而不是 bEntity.x,问题应该会解决。

英文:

Your field is called bEntity.x_number, not bEntity.x:

    @Field(name = &quot;x_number&quot;)

That's why you're getting this error.

Use bEntity.x_number instead of bEntity.x in your query and you should be fine.

huangapple
  • 本文由 发表于 2020年8月4日 19:30:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63245981.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定