Reputation: 1
I am doing some experiments with hibernate search. So far i am been able to create indexes and configurations stuff. Indexes are created and i already verified them using luke. But when i try to search it doesn't return any result. Where as faceting request works very well.
Entity
@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;
}
Indexing Code
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
}
Faceting Search Code (Which Works very well)
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()
+ ")");
}
}
Searching Code which is not working
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());
}
I tried all types of Lucene Queries but nothing works any help would be highly regarded.
Upvotes: 0
Views: 1636
Reputation: 1
Answer to above question is:
if you set Analyze attribute of @Field annotation to NO it won't be tokenized(would be saved as it is, case sensitive). So searchable fields should be set analyze = Analyze.YES but Please Note facet search on these fields won't work!
Upvotes: 3