-->
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: Distinct results in hibernate search lucene
PostPosted: Tue Jan 17, 2012 12:08 pm 
Regular
Regular

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

I was wondering if it was possible to use distinct in a lucene query. My lucene query is formed from several fields...


Code:
   
private FullTextQuery createSearchQuery(String q, String fields[]) throws BuscadorException
   {
       FullTextQuery ftq = null;
      
       try
        {
           Analyzer analyzer = Search.getFullTextSession(session).getSearchFactory().getAnalyzer("capsFilterTitle");
           
            BooleanQuery[] Querys = new BooleanQuery[campos.length];
           
            int cont = 0;
            while(cont < fields.length)
            {
                BooleanQuery andQuery = new BooleanQuery();

                TokenStream tokenStream = analyzer.tokenStream(fields[cont], new StringReader(q));
               
                CharTermAttribute charTermAttribute = tokenStream.getAttribute(CharTermAttribute.class);
   
                while (tokenStream.incrementToken())
                {
                    String term = charTermAttribute.toString();
                    andQuery.add(new TermQuery(new Term(fields[cont], term)), Occur.MUST);
                }
   
                Querys[cont] = andQuery;
               
                cont++;
            }
           
            BooleanQuery query = new BooleanQuery();
           
            // construir una query por cada campo y enlazarla con OR.
            cont = 0;
            while(cont < campos.length)
            {
                query.add(Querys[cont], Occur.SHOULD);
                cont++;
            }

            ftq = Search.getFullTextSession(session).createFullTextQuery(query, Entity.class);
        }
        catch (Exception e)
           ..............
       
        return ftq;
   }


thanks in advance


Top
 Profile  
 
 Post subject: Re: Distinct results in hibernate search lucene
PostPosted: Tue Jan 17, 2012 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
no Lucene doesn't support distinct or other relational operators. You'll have to implement something like that by loading all matches in a Set.
I didn't fully understand your code. It looks like you're doing the same query that you would get by using Lucene's QueryParser ?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Distinct results in hibernate search lucene
PostPosted: Wed Jan 18, 2012 5:04 am 
Regular
Regular

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

Firstly, my query transforms the queryString with my Analyzer (delete stopwords and puts the text in lowercase). Then the queryString is tokenized.

Then for every token and every field i construct the final query.

Example:
queryString = Hibernate Search

(field1=hibernate and field1=search) OR (field2=hibernate and field2=search)

If anybody knows other way to do that..i would like to know...i don't know too much about hibernate search and i just try to do my best...

thanks in advance,


Top
 Profile  
 
 Post subject: Re: Distinct results in hibernate search lucene
PostPosted: Wed Jan 18, 2012 5:13 am 
Regular
Regular

Joined: Thu Jun 16, 2011 12:03 pm
Posts: 94
I am really having a problem...

I would like to make a level search. In my model i have a book that has several expressions, and each of these expresions has several manifestations.

Book
-> List expresion
-> List Manifestation

Book, Expresion and Manifestation extends from Entity. Book has a list of Expresion. Expresion has a List of Manifestation.

How could i search on parent class Entity in Lucene and have the next results:

1.- if the result if a book, i want all expresions and all manifestations.
2.- if the result if a expresion, i want all manifestations
3.- if the result if a manifestation, i just want this manifestation.

I know that maybe this is crazy, but my boss ask me to do that...

Thanks in advance,

Hibernator,

PD: i have been thinking about indexing the Expression and Manifestation list (@IndexEmbbeded) but i don't know how to obtain all childs. Maybe I am wrong, and i just have to manage manually all the results...any idea? I just saw projection http://docs.jboss.org/hibernate/search/4.1/reference/en-US/html_single/#projections


Last edited by hibernator_11 on Wed Jan 18, 2012 5:26 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Distinct results in hibernate search lucene
PostPosted: Wed Jan 18, 2012 5:24 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Just search for the "Entity"s which match the query, then for each element of the resultset look at the type and load the related elements: all entities loaded via Hibernate Search are still managed by Hibernate, so you can load attached collections from them, there is no need to specify the collections you need at query time.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Distinct results in hibernate search lucene
PostPosted: Wed Jan 18, 2012 5:42 am 
Regular
Regular

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

What i understand about that is that i search on Entity and then i loop all my results to add the childs, oks?

I thought about that but what about if i want to paginate? i don't think this would work...

thanks in advance,


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.