2013-10-07 5 views
0

Je fais quelques expériences avec la recherche d'hibernation. Jusqu'à présent, j'ai été capable de créer des index et des configurations. Les index sont créés et je les ai déjà vérifiés en utilisant luke. Mais quand j'essaie de chercher, il ne renvoie aucun résultat. Où comme demande de facettage fonctionne très bien.Hibernate Search ne renvoie aucun résultat

Entité

@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; 
} 

Code indexation

  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 
} 

Facettage Recherche de code (qui fonctionne très bien)

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() 
         + ")"); 

      } 
     } 

Code Recherche qui ne fonctionne pas

 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()); 
       } 

J'ai essayé tous les types de requêtes Lucene, mais rien ne fonctionne que toute aide serait très appréciée.

Répondre

2

Réponse à la question ci-dessus est:

si vous définissez l'attribut d'annotation Analyser @field sur NO, il ne sera pas segmenté (serait sauvé car il est sensible à la casse). Donc, les champs interrogeables doivent être configurés analyser = Analyze.YES mais veuillez noter que la recherche par facette sur ces champs ne fonctionnera pas!

Questions connexes