-->
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: Duplicate results appearing in Hibernate Search query
PostPosted: Thu Jul 03, 2008 5:57 pm 
Newbie

Joined: Wed Jul 02, 2008 11:57 am
Posts: 2
Hello,

I'm a bit new to Hibernate and Hibernate Search, and I'm having a problem getting my full text search to work across fields in the database.

My table looks like this:

Code:

create table Entry (
  entry_id bigint unsigned not null auto_increment,
  category_id smallint unsigned not null,
  entry_title varchar(300) not null,
  entry_question text not null,
  entry_answer text not null,
  entry_hitcount bigint unsigned not null,
  contact_id smallint unsigned not null default 0,
  primary key(entry_id)
) engine = MyISAM;


(The important fields are entry_title, entry_question, entry_answer, everything else works)

I've configured the appropriate listeners in my Hibernate.cfg file, so entries are getting indexed upon insertion, but when I try to search the entries with the code below, I sometimes get duplicate results.

Code:
    private Collection<Entry> query() {
        MultiFieldQueryParser mpq = new MultiFieldQueryParser(FIELDS, new StandardAnalyzer());
        Query q;
        try {
            q = mpq.parse(query);
            begin();
            org.hibernate.Query hQ = session.createFullTextQuery(q, Entry.class);
            Collection<Entry> results = hQ.list();
            end();
            return results;
        } catch (ParseException ex) {
            ex.printStackTrace();
            return null;
        }


    }


For example: If I create a new entry through the application with the values

entry_title: Test title
entry_question: Pandora or Imeem?
entry_question: this is a pretty nifty question. No music for you!

and search for "test" I get two results back, even if that's the only entry in the database.

What would be the correct way to perform an AND search across those 3 fields?

Thanks in advance! (Hope it's not a duplicate question, I swear I searched!)

_________________
Joe


Top
 Profile  
 
 Post subject: Quick update
PostPosted: Thu Jul 03, 2008 7:28 pm 
Newbie

Joined: Wed Jul 02, 2008 11:57 am
Posts: 2
After reading more of Lucene's documentation, it looks like it's producing the query I want. The toString method on BooleanQuery returns this when I search across the fields for "test":

+(entryTitle:test entryQuestion:test entryAnswer:test)

and it turns out it only produces duplicate results if the words I search for only appear in one field. So, if entry_title looks like "Zesty entry title" and I search for "zesty", I get duplicates. If I search for terms that appear in other fields, then no duplicates occur.

Here's the new method I'm using to search:

Code:

    private Collection<Entry> query() {
        Query q;
        BooleanQuery bq = new BooleanQuery();
        MultiFieldQueryParser mpq = new MultiFieldQueryParser(FIELDS, new StandardAnalyzer());
        mpq.setDefaultOperator(MultiFieldQueryParser.AND_OPERATOR);
        for (String word : words) {
            try {
                q = mpq.parse(word);
                bq.add(q, BooleanClause.Occur.MUST);
            } catch (ParseException ex) {
                ex.printStackTrace();
            }
        }
        System.out.println(bq.toString());
        begin();
        Collection<Entry> ent = session.createFullTextQuery(bq, Entry.class).list();
        Set<Entry> s = new LinkedHashSet<Entry>();
        s.addAll(ent);
        end();
        return ent;
    }


In the meantime, I've worked around it by overriding .equals on Entry and adding the collections to a set. God help me.

_________________
Joe


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.