-->
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.  [ 2 posts ] 
Author Message
 Post subject: [Search] Find out in which fields the query string was found
PostPosted: Tue Oct 06, 2009 5:55 am 
Newbie

Joined: Tue Oct 06, 2009 4:19 am
Posts: 1
I provide a search over all indexed fields of my class Project. Now I want to show the search result with score and the information in which fields the search string was found.
I can't find any means to get these fields. I tried using the Explanation object which indeed shows the participating fields in the description string. But naturally, I do not want to parse this string to get the fields.
Am I missing something?

Search code:
Code:
FullTextQuery hibQuery = fullTextSession.createFullTextQuery(query, Project.class);
hibQuery.setProjection(FullTextQuery.THIS, FullTextQuery.SCORE, FullTextQuery.EXPLANATION);
List result = hibQuery.list();


Top
 Profile  
 
 Post subject: Re: [Search] Find out in which fields the query string was found
PostPosted: Tue Oct 06, 2009 9:22 am 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
It looks as though the Explaination object doesn't provide a way to get to the field, which is somewhat annoying. I came across someone doing something similar in the lucene mailing list which may be worth looking at:

Code:
       query = query.rewrite(this.getReader());
       QueryScorer scorer = new QueryScorer(query);
       String found = null;
       float maxScore = 0;
       for(Field f: (List<Field>) doc.getFields()) {
           String text = f.stringValue();
           scorer.startFragment(new TextFragment(new StringBuffer(text),0,text.length()));
           TokenStream tok = analyzer
                   .tokenStream(f.name(),new StringReader(text));
           System.out.println("Field: " + f + ":: " +f.name() + ": " + f.stringValue());
           Token t=new Token();
           while(tok!=null && (t=tok.next(t))!=null) {
               float s = scorer.getTokenScore(t);
           }

           float score = scorer.getFragmentScore();
           if(score > maxScore) {
               maxScore = score;
               found = text;
           }
       }


I haven't tried this but I can give it a go and see if it works.

Not sure if I've helped much...I'll search my lucene mail to find more of a concrete example.

Cheers


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.