-->
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.  [ 3 posts ] 
Author Message
 Post subject: Search across all fields
PostPosted: Thu Oct 21, 2010 6:34 am 
Beginner
Beginner

Joined: Tue Sep 28, 2010 5:14 am
Posts: 25
Hi,

I am looking for doing a generic search on all the fields that are available (something similiar to what google does). Is there any way to default include all possible fields in the document rather than mentioning explicitly which field to look for in all the documents.

Like in the example below, while creating a searchQuery I mention it like "firstName:TEST"; . This means if I had 20 fields in document, I would have to do this for all fields, which I want to avoid.

public static List<Contact> search() {
//Building the Lucene query

String searchQuery = "firstName:TEST"; //query string
QueryParser parser = new QueryParser(
"firstName", //default field
new StandardAnalyzer() //analyzer used
);

org.apache.lucene.search.Query luceneQuery;
try {
luceneQuery = parser.parse(searchQuery); //build Lucene query
}
catch (ParseException e) {
throw new RuntimeException("Unable to parse query: " + searchQuery, e);
}

Session session = SessionHolder.getSession();
FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession(session);
Query query = ftSession.createFullTextQuery(luceneQuery, Contact.class);

@SuppressWarnings("unchecked")
final List<Contact> results = query.list(); //execute the query
System.out.println("results.size() " + results.size());
for (Iterator iterator = results.iterator(); iterator.hasNext();) {
Contact name = (Contact) iterator.next();
System.out.println(name.getFirstName() + " || "+ name.getLastName() + " || " + name.getEmail());
}
return results;
}


Thanks in advance.

--Manoj


Top
 Profile  
 
 Post subject: Re: Search across all fields
PostPosted: Thu Oct 21, 2010 7:58 am 
Newbie

Joined: Thu Jul 01, 2010 12:45 pm
Posts: 11
Just don't include a field specifically. Also use a MultiFieldQueryParser instead of just a QueryParser.

Code:
FullTextSession fullTextSession = Search.getFullTextSession(HibernateUtil.getSessionFactory().getCurrentSession());
String fields[] = {"firstName", "lastName"}; //etc, add more fields as needed
MultiFieldQueryParser parser = new MultiFieldQueryParser(
            Version.LUCENE_29, fields, fullTextSession.getSearchFactory()
                  .getAnalyzer(Contact.class));


And your Lucene query should look like: "TEST"


Top
 Profile  
 
 Post subject: Re: Search across all fields
PostPosted: Thu Oct 21, 2010 9:21 am 
Beginner
Beginner

Joined: Tue Sep 28, 2010 5:14 am
Posts: 25
Thanks mate. It was really helpfull.

--Manoj


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