0

我正在用休眠搜索做一些实验。到目前为止,我能够创建索引和配置的东西。索引已创建,我已经使用 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 查询,但没有任何帮助会受到高度重视。

4

1 回答 1

3

上述问题的答案是:

如果将 @Field 注释的 Analyze 属性设置为 NO,则不会对其进行标记(将按原样保存,区分大小写)。因此,可搜索字段应设置为 analyze = Analyze.YES 但请注意,对这些字段的分面搜索将不起作用!

于 2013-10-07T09:49:00.573 回答