Hibernate Search没有返回任何结果

时间:2013-10-07 07:46:34

标签: hibernate search hibernate-search

我正在做一些关于hibernate搜索的实验。到目前为止,我已经能够创建索引和配置的东西。索引已创建,我已使用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查询但没有任何效果可以得到很高的评价。

1 个答案:

答案 0 :(得分:2)

对上述问题的回答是:

如果将@Field注释的Analyze属性设置为NO,它将不会被标记化(将保存为原样,区分大小写)。所以可搜索的字段应该设置为analyze = Analyze.YES但请注意这些字段的方面搜索不起作用!