Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
集合上的Hibernate搜索(Lucene)筛选器_Lucene_Hibernate Search - Fatal编程技术网

集合上的Hibernate搜索(Lucene)筛选器

集合上的Hibernate搜索(Lucene)筛选器,lucene,hibernate-search,Lucene,Hibernate Search,我在用Hibernate搜索过滤器实现布尔逻辑时遇到了一个问题。 有些人可以成为团体的一部分。每个组都有一个来自状态目录的状态 我需要筛选组1中状态为2的所有用户。为此,我对这两个子句都使用了带有Occurse.MUST的布尔查询,但在筛选结果中包含了具有Grop列表的人员,其中一个为1,组的一个状态为2,例如: person | group | status 105 (1) 3 105 2 3 105 3 (2) 188

我在用Hibernate搜索过滤器实现布尔逻辑时遇到了一个问题。 有些人可以成为团体的一部分。每个组都有一个来自状态目录的状态

我需要筛选组1中状态为2的所有用户。为此,我对这两个子句都使用了带有Occurse.MUST的布尔查询,但在筛选结果中包含了具有Grop列表的人员,其中一个为1,组的一个状态为2,例如:

person | group | status
105      (1)       3
105       2        3
105       3       (2)

188      (1)       3
188       7       (2)

197      (1)       4
197       8        5
197       9       (2)
用户105、188和197不必包括在过滤结果中。正确的方法是什么

过滤器:

BooleanQuery bq = new BooleanQuery();
TermQuery tqGroup = new TermQuery(new Term("groupPersons.id.groupId", "1"));
TermQuery tqStatus = new TermQuery(new Term("groupPersons.status.id", "2"));
bq.add(tqGroup, BooleanClause.Occur.MUST);
bq.add(tqStatus, BooleanClause.Occur.MUST);
filter = new QueryWrapperFilter(bq);
个人和实体:

...
private List<GroupPerson> groupPersons = new ArrayList<GroupPerson>(0);

@IndexedEmbedded
@OneToMany(fetch = FetchType.LAZY, mappedBy = "person")
public List<GroupPerson> getGroupPersons() {
    return this.groupPersons;
}
...

@EmbeddedId
@AttributeOverrides({
        @AttributeOverride(name = "groupId", column = @Column(name = "group_id", nullable = false)),
        @AttributeOverride(name = "personId", column = @Column(name = "person_id", nullable = false)) })
@NotNull
@DocumentId
@FieldBridge(impl = GroupPersonIdBridge.class) 
public GroupPersonId getId() {
    return this.id;
}

...

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "status_id",nullable = false)
@IndexedEmbedded
@NotNull
public Status getStatus() {
     return this.Status;
}
组织机构人员桥:

public Object get(String name, Document document) {
    GroupPersonId id = new GroupPersonId();
    Field field = document.getField( name + ".groupId" );
    id.setGroupId(Long.parseLong(field.stringValue()));
    field = document.getField( name + ".personId" );
    id.setPersonId(Long.parseLong(field.stringValue()));
    return id;
  }

  public String objectToString(Object object) {
     GroupPersonId id = (GroupPersonId) object;
    StringBuilder sb = new StringBuilder();
    sb.append( id.getGroupId() )
    .append(" ")
    .append(id.getPersonId());
    return sb.toString();
  }


  public void set(String name,Object value,Document document,LuceneOptions luceneOptions) {
    GroupPersonId id = (GroupPersonId)value;
    Store store = luceneOptions.getStore();
    Index index = luceneOptions.getIndex();
    TermVector termVector = luceneOptions.getTermVector();
    Float boost = luceneOptions.getBoost();
    //store each property in a unique field
    Field field = new Field(name + ".groupId", id.getGroupId() + "", store, index, termVector);
    field.setBoost( boost );
    document.add( field );

    field = new Field(name + ".personId", id.getPersonId() + "", store, index, termVector);
    field.setBoost( boost );
    document.add( field );
    //store the unique string representation in the named field
    field = new Field( name,
    objectToString( id ),
    store, index, termVector );
    field.setBoost( boost );
    document.add( field );
  } 

Hibernate search的版本是4.5.1。最终版

问题在于Lucene
文档
没有关联。当您使用
@IndexedEmbedded
时,您有效地将所有关联展平到单个Lucene
文档中(这是添加到Lucene索引并在搜索时检索的内容)。
文档
可以多次添加同名的A字段。以您的示例为例,id为105的
人员的
文档将包含以下字段名到字段值对:

+-------------------------+-------------+
|       field name        | field value |
+-------------------------+-------------+
| groupPersons.id.groupId |           1 |
| groupPersons.id.groupId |           2 |
| groupPersons.id.groupId |           3 |
| groupPersons.status.id  |           3 |
| groupPersons.status.id  |           3 |
| groupPersons.status.id  |           2 |
+-------------------------+-------------+
如果您现在查看您的查询,您就会明白为什么第105人是匹配的。两个布尔查询都匹配


你如何解决这个问题?你需要确保有一些独特的搜索。实现这一点的一种方法是使用自定义桥将组和状态索引到单个字段中。然后您可以编写一个只针对该字段的查询。

对于具有相同用例的人,下面是使用classBridge的解决方案:

public class CustomClassBridge implements FieldBridge, Serializable {
public final static String SEPARATOR = "-";

@Override
public void set(String name, Object value, Document document, LuceneOptions luceneOptions) {
    GroupPerson gp = (GroupPerson)value;
    String fieldValue = gp.getId().getGroupId() + SEPARATOR + gp.getStatus().getId();
    Field field = new Field(name, fieldValue, luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector());
    field.setBoost(luceneOptions.getBoost());
    document.add(field);
}
}
在类级别向GroupPerson实体添加注释:

@ClassBridge(name="groupStatus",index=Index.YES, analyze=Analyze.NO, store=Store.YES, impl = CustomClassBridge.class)
最后在过滤器中:

TermQuery tq = new TermQuery(new Term("groupPersons.groupStatus", 1 + CustomClassBridge.SEPARATOR + 2));

在读了更多关于为什么会出现这种特定行为的文章后,我和你哈代得出了相同的结论。我使用不同的解决方案将列表作为参数传递给过滤器。但我更喜欢你提出的解决方案。谢谢