Hibernate Search不返回任何结果

我正在使用休眠搜索进行一些实验。到目前为止,我已经能够创建索引和配置内容。索引已创建,我已经使用luke对其进行了验证。但是当我尝试搜索时不会返回任何结果。作为分面要求的地方效果很好。

实体

    @Entity
    @Table(name = "user_profile")
    @Root(name = "candidate")
    @XmlRootElement
    @Indexed
    @Analyzer(impl = StandardAnalyzer.class)
    public class ProfileBean implements Serializable, DataModel {

   private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    @Element
    @DocumentId
    private Integer id;


    @NotNull
    @Length(max = 25)
    @Element(required=false)
    @Column(name = "first_name")
    @Field(index = Index.YES,analyze = Analyze.YES, store = Store.YES)
    private String firstName;


    @Column(name = "last_name")
    @NotNull
    @Length(max = 25)
    @Element(required=false)
    @Field(index = Index.YES,analyze = Analyze.YES, store = Store.YES)
    private String lastName;

    @Column(name = "email")
    @Length(max = 25)
    @Element
    @Field(index = Index.YES,analyze = Analyze.YES, store = Store.YES)
    private String email;

    @Column(name = "city")
    @NotNull
    @Length(max = 30)
    @Element(required=false)
    @Field(index = Index.YES,analyze = Analyze.NO, store = Store.YES)
    private String city;

    @Column(name = "country")
    @NotNull
    @Length(max = 25)
    @Element(required=false)
    @Field(index = Index.YES,analyze = Analyze.NO, store = Store.YES)
    private String country;

    @Column(name = "occupation")
    @Length(max = 25)
    @Element(required=false)
       @Field(index = Index.YES,analyze = Analyze.NO, store = Store.YES)
    private String occupation;
}

索引码

            FullTextSession fts =
   org.hibernate.search.Search.getFullTextSession(getSession());

        List<ProfileBean> profiles = super.listAll();
for (ProfileBean item : profiles) {
   fts.index(item); //manually index an item instance
}

方面搜索代码(效果很好)

    FullTextSession fts =
   org.hibernate.search.Search.getFullTextSession(getSession());


        QueryBuilder builder = fts.getSearchFactory()
                .buildQueryBuilder().forEntity(ProfileBean.class).get();

                FacetingRequest cityFacetingRequest = builder.facet()
                .name("cityFaceting").onField("city").discrete()
                .orderedBy(FacetSortOrder.COUNT_DESC).includeZeroCounts(false)
                .createFacetingRequest();

                Query luceneQuery = builder.all().createQuery();
        FullTextQuery fullTextQuery = fts.createFullTextQuery(luceneQuery, ProfileBean.class);
        FacetManager facetManager = fullTextQuery.getFacetManager();
        facetManager.enableFaceting(cityFacetingRequest);

                List<Facet> facets = facetManager.getFacets("cityFaceting");
        for (Facet f : facets) {
            System.out.println(f.getValue() + " (" + f.getCount() + ")");
            List<ProfileBean> profiles = fts.createFullTextQuery(
                    f.getFacetQuery(),ProfileBean.class).list();
            for (final ProfileBean p : profiles) {
                System.out.println(p.getFirstName() + " (" + p.getLastName()
                        + ")");

            }
        }

搜索不起作用的代码

      FullTextSession fts =
   org.hibernate.search.Search.getFullTextSession(getSession());
                QueryBuilder builder = fts.getSearchFactory()
                .buildQueryBuilder().forEntity(ProfileBean.class).get();

                Query query = builder.keyword().
                onFields("occupation", "city", "country").
                matching("engineer").createQuery();

                FullTextQuery fullTextQuery = fts.createFullTextQuery(query, ProfileBean.class);

                List<ProfileBean> profiles = fullTextQuery.list();

                for(ProfileBean bean: profiles){
                    System.out.println("First Name: "+bean.getFirstName()+" ,Last Name:"+bean.getLastName()+" ,Occupation:"+bean.getOccupation());
                }

我尝试了所有类型的Lucene查询,但是没有任何效果会得到高度重视。

上述问题的答案是:

如果将@Field批注的Analyze属性设置为NO,它将不会被标记化(将按原样保存,区分大小写)。因此,应将可搜索字段设置为analytics = Analyze.YES。但是请注意,在这些字段上进行分面搜索将无法进行!

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章