简体   繁体   English

在休眠搜索中过滤 IndexedEmbedded 实体 6

[英]Filter IndexedEmbedded entity in hiberenate search 6

Let's say i have a Indexed entity User that IndexedEmbedded a list of Entity Role.假设我有一个 IndexedEmbedded 实体角色列表的索引实体用户。 In that list we have also past Roles (soft deleted), i wanna index User, end limit roles list to only active roles in my index.在那个列表中我们也有过去的角色(软删除),我想索引用户,结束限制角色列表到我的索引中的活动角色。

Kind of:的种类:

@Entity
@Indexed
public class User{
  @FullTextField
  String name
  @IndexedEmbedded
  List<Role> roles
}

public class Role{
  @FullTextField
  String name
  String status //wanna filter status == "DELETED"
}

There is something like RoutingBridge in indexed annotation?索引注释中有类似RoutingBridge的东西吗?

First, a warning: you'd probably want to consider cleaning up associations upon soft-deletion of a role (remove the role from users), because this situation is likely to affect other parts of your application.首先,警告:您可能需要考虑在软删除角色(从用户中删除角色)时清理关联,因为这种情况可能会影响应用程序的其他部分。

With that out of the way, here's a more useful answer...有了这个,这里有一个更有用的答案......


No, RoutingBridge won't help here.不, RoutingBridge在这里无济于事。

There's no built-in feature for what you're trying to achieve ( yet ).对于您要实现的目标,还没有内置功能()。

The simplest solution I can think of is adding a "derived" getter that does the filtering.我能想到的最简单的解决方案是添加一个执行过滤的“派生”吸气剂。

@Entity
@Indexed
public class User{
  @FullTextField
  private String name;
  @ManyToMany
  private List<Role> roles;


  @javax.persistence.Transient
  @IndexedEmbedded(name = "roles")
  // Tell Hibernate Search that changing the status of a Role might
  // require reindexing
  @IndexingDependency(derivedFrom = @ObjectPath( 
      @PropertyValue(propertyName = "roles"), @PropertyValue(propertyName = "status")
  ))
  // Tell Hibernate Search how to find users to reindex
  // when a Role changes (its name or status)
  @AssociationInverseSide(inversePath = @ObjectPath(
      @PropertyValue(propertyName = "users")
  ))
  public List<Role> getNonDeletedRoles() {
    return roles.stream().filter(r -> !"DELETED".equals(r.getStatus()))
        .collect(Collectors.toList());
  }
}

@Entity
public class Role{
  @FullTextField
  private String name;
  private String status;
  // You will need this!
  @ManyToMany(mappedBy = "roles")
  private List<User> users;
}

If for some reason you cannot afford to model the association Role => User, you will have to give up on reindexing users when a role changes:如果出于某种原因你不能承受 model 关联 Role => User,当角色改变时你将不得不放弃重新索引用户:

@Entity
@Indexed
public class User{
  @FullTextField
  private String name;
  @ManyToMany
  private List<Role> roles;

  @javax.persistence.Transient
  @IndexedEmbedded(name = "roles")
  @IndexingDependency(reindexOnUpdate = ReindexOnUpdate.SHALLOW,
      derivedFrom = @ObjectPath(@PropertyValue(propertyName = "roles")))
  public List<Role> getNonDeletedRoles() {
    return roles.stream().filter(r -> !"DELETED".equals(r.getStatus()))
        .collect(Collectors.toList());
  }
}

@Entity
public class Role{
  @FullTextField
  private String name;
  private String status;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM