-->
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 multiple fields for the same string
PostPosted: Mon Dec 28, 2009 6:05 pm 
Beginner
Beginner

Joined: Mon Dec 21, 2009 3:43 pm
Posts: 24
How do I search multiple fields for a given string?

I.E. if I have the following to fields indexed
Code:
parents.parentName
parents.childName


I have the following code to search in one field, but reading the api I am not sure how to search both fields with one search because when construct a QueryParser you have to give it a field. Can you give it mulitple fields?
Code:
      try {
         FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);
   
                        // Problem this only searches the parents.parentName field
         QueryParser parser = new QueryParser("parents.parentName", new SimpleAnalyzer() );

         System.out.println("Searching for string: " + search);
         
         Query luceneQuery = parser.parse( search );
   
         javax.persistence.Query fullTextQuery =
            fullTextEntityManager.createFullTextQuery( luceneQuery );
   
         @SuppressWarnings("unchecked")
         List<Top> result = (List<Top>) fullTextQuery.getResultList(); //return a list of managed objects
         
         if (result == null) {
            System.out.println("ERROR: query returned null list");
         } else if (result.size() < 1) {
            System.out.println("ERROR: query returned list of size: " + result.size());
         } else {
            System.out.println("PASS: query returned list of size: " + result.size());
            
            for (Top t : result) {
               System.out.println("Found in top id: " + t.getId());
            }
         }
      } catch (ParseException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
      }


Top
 Profile  
 
 Post subject: Re: Search multiple fields for the same string
PostPosted: Mon Dec 28, 2009 6:19 pm 
Pro
Pro

Joined: Wed Oct 03, 2007 2:31 pm
Posts: 205
You can use MultiFieldQueryParser if you want to search across multiple fields. Basically it will look something like

Code:
new MultiFieldQueryParser( new String[]{"fieldA", "fieldB"}, new StandardAnalyzer());


Hope that helps.


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.