代码之家  ›  专栏  ›  技术社区  ›  greengold

休眠搜索结果太少

  •  0
  • greengold  · 技术社区  · 6 年前

    我有个亲戚: Document2 ->* DocVersion 意思是文档有多个版本,要清楚。

    DocVersion 基于独特的 Document2 s场。

    实体:

    @Indexed
    @Entity
    public class DocVersion implements Serializable {
    
        ...
        @Id
        @Column(name = "version_id")
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        private long id;
        ...
    
        @IndexedEmbedded
        @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
        @JoinColumn(name = "doc_uuid")
        private Document2 document2;
    
        @Field
        private String versionVed;
    
        private String versionOKM;
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            DocVersion version = (DocVersion) o;
            return id == version.id &&
                Objects.equals(versionVed, version.versionVed) &&
                Objects.equals(versionOKM, version.versionOKM);
        }
    
        @Override
        public int hashCode() {
    
            return Objects.hash(id, versionVed, versionOKM);
        }
    }
    
    
    @Indexed
    @Entity
    public class Document2 implements Serializable {
    
        @Id
        @GeneratedValue(strategy = GenerationType.SEQUENCE)
        @Column(name = "doc_uuid")
        private long id;
    
        @Field
        @Analyzer(impl = WhitespaceAnalyzer.class)
        @Column(name = "uuid")
        private String UUID;
    
        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Document2 document2 = (Document2) o;
            return Objects.equals(UUID, document2.UUID);
        }
    
        @Override
        public int hashCode() {
    
            return Objects.hash(UUID);
        }
    
    }
    

            QueryBuilder b = fullTextEntityManager.getSearchFactory()
                    .buildQueryBuilder()
                    .forEntity(DocVersion.class)
                    .get();
    
            BooleanQuery.Builder builder = new BooleanQuery.Builder();
    
    builder.add(b.simpleQueryString().onField("document2.UUID").matching(val)
                            .createQuery(), MUST);
    
    List<DocVersion> results = fullTextEntityManager.createFullTextQuery(builder.build(), DocVersion.class).getResultList();
    

    文档版本 为什么我和Lucene只有一个?如何解决这个问题?

    休眠搜索:5.9.3.Final

    2 回复  |  直到 6 年前
        1
  •  0
  •   yrodiere    6 年前

    检查:

    1. 你的设置正确吗 DocVersion.document2 DocVersion . 忘记设置双向关联的两边是一个常见的错误(例如,可能您只是调用了 document2.getVersions().add(version) ,但不是 version.setDocument2(document2) ).
    2. 自上次更改Hibernate搜索映射后,您重新编制了索引。你可以用 mass indexer

    此外,这是一个可能导致意外行为的坏主意:

    @Field
    @Analyzer(impl = WhitespaceAnalyzer.class)
    @Column(name = "uuid")
    private String UUID;
    

    UUID字段不适合全文搜索,因为您可能需要精确匹配。只需禁用分析:

    @Field(analyze = Analyze.NO)
    @Column(name = "uuid")
    private String UUID;
    
        2
  •  0
  •   greengold    6 年前

    虽然所有的回复都是有效的,值得考虑的人去周围与这种问题,对我来说,造成这种“行为不端”是数据库中的一个索引叶与空值,hibernate搜索没有掌握不良数据,因此我错过了结果。