-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 
Author Message
 Post subject: Hibernate Search Facet Search
PostPosted: Thu Jun 16, 2011 12:10 pm 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
Hi all,

I am little bit new with Hibernate Search. I have a new application running in Jboss AS6 and I am trying to create a faceting search request.

I have checked the documentation but I do not understand how can I do what I want. I have a menu on the left where i can see my facets and on the right i have the results of the general search (like Amazon). I am using and EJB 3.0 with a DAO where I have my hibernate search querys.

Well, I want is to enable and disable facets when the user click on the left menu but I have several problems.

Has anybody an example of this? Where can I check it out?

I've found this https://gist.github.com/869145 , and why is necessary to do this:

//FIXME jpa.FTQuery does not have the facet methods, need to do some nasty unwrapping
ftQuery = em.unwrap( FullTextSession.class )
.createFullTextQuery( query, Car.class );

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Hibernate Search Facet Search EJB 3.0 @PersistenceContext
PostPosted: Fri Jun 17, 2011 5:01 pm 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
Hi all,

I've been trying and I have a problem. How do you create the Hiberntate Session?

I want to create a fulltextquery from a fullTextSession because I always use the same query. I am showing the facets of my query at my web and when the user clicks on them I want to show the results of this facet.

// create a fulltext query
QueryBuilder builder = queryBuilder( Cd.class );
Query luceneQuery = builder.all().createQuery(); // match all query
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery, Cd.class );

It is possible to use a fulltextquery define before? is there any problem with the session?

to sum up:
1.- search (create fulltextquery). Show the results of the query and facets
2.- when the user clicks on any facet, i want to apply this facet to my fulltextquery define before and obtain the results.

Thanks in advance.


Top
 Profile  
 
 Post subject: Re: Hibernate Search Facet Search
PostPosted: Sat Jun 18, 2011 10:57 am 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
I think that i did it!

do you think that this is ok?

Well, now i have to use that in my ejb 3.0 and make it work with my web. Any suggestions?

public class App {
private EntityManager em;
private EntityManagerFactory emf;

//gettets and setters

private void createEM() {
emf = Persistence.createEntityManagerFactory("manager1");
em = emf.createEntityManager(); // Retrieve an application managed entity manager
}

private FullTextSession prepareFullTextSession() {
Session session = (Session) em.unwrap(Session.class);
FullTextSession fullTextSession = Search.getFullTextSession(session);
return fullTextSession;
}

private FullTextQuery buildHibernateQuery(FullTextSession fullTextSession, String q) {

QueryBuilder qb = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(PersonEntity.class).get();

FacetingRequest nameFacetingRequest = qb.facet()
.name( "nameFaceting" )
.onField( "firstname")
.discrete()
.orderedBy( FacetSortOrder.COUNT_DESC )
.includeZeroCounts( false )
.maxFacetCount( 5 )
.createFacetingRequest();

org.apache.lucene.search.Query query = qb.keyword().wildcard().onField("firstname").matching("*"+q+"*").createQuery();

FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, PersonEntity.class);

FacetManager facetManager = fullTextQuery.getFacetManager();

facetManager.enableFaceting( nameFacetingRequest );

return fullTextQuery;
}

private List<PersonEntity> executeSearch(FullTextQuery f) {
// execute search
@SuppressWarnings("unchecked")
List<PersonEntity> result = f.list();
return result;
}

private List<Facet> getFacets(FullTextQuery fullTextQuery){
List<Facet> facets = fullTextQuery.getFacetManager().getFacets( "nameFaceting" );

return facets;
}

private void applyFacets(FullTextQuery fullTextQuery, int pos){
List<Facet> facets = fullTextQuery.getFacetManager().getFacets( "nameFaceting" );

fullTextQuery.getFacetManager().getFacetGroup( "nameFaceting" ).selectFacets( facets.get( pos ) );
}

private void disableFacets(FullTextQuery fullTextQuery){

fullTextQuery.getFacetManager().getFacetGroup( "nameFaceting" ).clearSelectedFacets();
}

public static void main(String[] args) {
System.out.println("Hibernate Search!");

App app = new App();

app.createEM();

FullTextSession sesion = app.prepareFullTextSession();


FullTextQuery f = app.buildHibernateQuery(sesion, "u");

List<PersonEntity> lista = app.executeSearch(f);

for(PersonEntity p : lista){
System.out.println(p.toString());
}

List<Facet> facets = app.getFacets(f);

for(Facet fa : facets){
System.out.println("Valor:" + fa.getValue() + " count:" + fa.getCount());
}

app.applyFacets(f, 1);
lista = app.executeSearch(f);

for(PersonEntity p : lista){
System.out.println(p.toString());
}

app.disableFacets(f);

lista = app.executeSearch(f);
for(PersonEntity p : lista){
System.out.println(p.toString());
}

app.applyFacets(f, 2);
lista = app.executeSearch(f);

for(PersonEntity p : lista){
System.out.println(p.toString());
}

// Work with the EM
app.getEm().close();
app.getEmf().close(); // close at application end
}
}


Top
 Profile  
 
 Post subject: Re: Hibernate Search Facet Search
PostPosted: Mon Jun 20, 2011 7:59 pm 
Newbie

Joined: Mon Nov 22, 2010 7:55 pm
Posts: 6
Hi,

Were you able to implement this on the web? If could you could please point me to some pointers, that will be great. I Was wondering if you could get the object inside each facets and display them.

For example, on the web, how would you build your url to get the list of faceted object?

Thanks,


Top
 Profile  
 
 Post subject: Re: Hibernate Search Facet Search
PostPosted: Tue Jun 21, 2011 3:14 am 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
Hi,

First you get the facets and show them in your web:

private List<Facet> getFacets(FullTextQuery fullTextQuery){
List<Facet> facets = fullTextQuery.getFacetManager().getFacets( "nameFaceting" );

return facets;
}

each facet has a count and a value.

then all you need to get the results of applying a facet is the pos and the faceting name:

private List<PersonEntity> applyFacets(FullTextQuery fullTextQuery, int pos){
List<Facet> facets = fullTextQuery.getFacetManager().getFacets( "nameFaceting" );

fullTextQuery.getFacetManager().getFacetGroup( "nameFaceting" ).selectFacets( facets.get( pos ) );

List<PersonEntity> result = f.list();
}

bye


Top
 Profile  
 
 Post subject: Re: Hibernate Search Facet Search
PostPosted: Tue Jun 21, 2011 6:13 pm 
Newbie

Joined: Mon Nov 22, 2010 7:55 pm
Posts: 6
Hi,

Thank you very much for the info.I really appreciate it and it is working. I am trying to display the results on the web now.

Let you know how that goes.

Best Regards,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 6 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.