2013-10-07 65 views
0

我正在做一些Hibernate搜索的實驗。到目前爲止,我已經能夠創建索引和配置的東西。索引創建,我已經使用盧克驗證它們。但是,當我嘗試搜索它不會返回任何結果。面向請求的作用非常好。Hibernate搜索沒有返回任何結果

實體

@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查詢,但沒有任何作品的任何幫助將高度重視。

回答

2

回答上面的問題是:

如果設置分析@Field標註的屬性,沒有也不會被標記化(將被保存,因爲它是區分大小寫)。所以應該設置可搜索字段analyze = Analyze.YES,但請注意在這些字段上搜索facet將無法正常工作!