-->
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: get all possible Fields programmatically
PostPosted: Tue Dec 15, 2009 12:20 pm 
Regular
Regular

Joined: Thu Nov 26, 2009 8:45 am
Posts: 78
I have a index with a lot of fields in it. I want to search over all these fields using the following query:

Code:
query = MultiFieldQueryParser.parse(query0, fields, boolArr,new StopAnalyzer());

fields here is an array of strings which are representing my fields now, but i currently hardcoded them all in my code. is there a possibility to get the fieldnames from the lucene/hibernate search api? (already seen the IndexReader.getFieldNames() method, but how can i get the IndexReader from my fullTextSession (only currently accessible object)).


Top
 Profile  
 
 Post subject: Re: get all possible Fields programmatically
PostPosted: Tue Dec 15, 2009 1:00 pm 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Code:
DirectoryProvider provider = searchFactory.getDirectoryProviders(MyClass.class)[0];
ReaderProvider readerProvider = searchFactory.getReaderProvider();
IndexReader reader = readerProvider.openReader(provider);

try {
    //do read-only operations on the reader
}
finally {
    readerProvider.closeReader(reader);
}


Check also the online documentation for "Using an IndexReader"

--Hardy


Top
 Profile  
 
 Post subject: Re: get all possible Fields programmatically
PostPosted: Wed Dec 16, 2009 8:40 am 
Regular
Regular

Joined: Thu Nov 26, 2009 8:45 am
Posts: 78
hardy.ferentschik wrote:

Code:
DirectoryProvider provider = searchFactory.getDirectoryProviders(MyClass.class)[0];



Thx for your reply, but this seems to be my old problem again, i have to determine which class i want to get the fields of.
If i wanna get the fields of all my indexed classes i have to get a provider for each of this classes (hardcoded by hand)?


Top
 Profile  
 
 Post subject: Re: get all possible Fields programmatically
PostPosted: Wed Dec 16, 2009 9:36 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
There is one way to get a list of all indexed classes, but it involves a cast:

Code:
SearchFactoryImpl factoryImpl = (SearchFactoryImpl) fulltextSession.getSearchFactory();
Set<Class<?>> indexedClasses = factoryImpl.getDocumentBuildersIndexedEntities.keySet();


I agree we should offer more metadata. I creates a jira issue for that HSEARCH-436

--Hardy


Top
 Profile  
 
 Post subject: Re: get all possible Fields programmatically
PostPosted: Wed Feb 17, 2010 6:21 am 
Regular
Regular

Joined: Thu Nov 26, 2009 8:45 am
Posts: 78
hi there,

after using the following code so far, i recognize today, that i get less fields, if i have more than one class in the Classes Array:

Code:
      ArrayList<String> retVal = new ArrayList<String>();
      SearchFactory sFactory = fSession.getSearchFactory();
      ArrayList<Class> classesList = new ArrayList<Class>();

                ....filling the arraylist....

      Class[] classes = (Class[])classesList.toArray(new Class[classesList.size()]);
      DirectoryProvider provider = null;
      ReaderProvider readerProvider = fSession.getSearchFactory().getReaderProvider();
      IndexReader indexReader = null;
      
      try{
         for(Class c : classes){
            if(sFactory != null){
               provider = sFactory.getDirectoryProviders(c)[0];
               if(provider != null){
                  indexReader = readerProvider.openReader(provider);
                  Collection tmpFields = indexReader.getFieldNames(FieldOption.ALL);
                  for(Object o : tmpFields){
                     if(o instanceof String){
                        String s = (String)o;
                        if(!retVal.contains(s)){
                           retVal.add(s);
                        }
                     }
                  }
                  readerProvider.closeReader(indexReader);
               } else {
                  myLogger.error("Error appeared");
               }
            } else {
               myLogger.error("Searchfactory = null");
            }
         }
      } catch(Exception e){
         myLogger.error("Error", e);
      }

                return retval;


For example, if i had just Class A in the array i get back 150 results. If i have Classes A,B,C in the array i get back about 50. Could anybody tell me, what i'm doing wrong here?


Top
 Profile  
 
 Post subject: Re: get all possible Fields programmatically
PostPosted: Wed Feb 17, 2010 6:50 am 
Regular
Regular

Joined: Thu Nov 26, 2009 8:45 am
Posts: 78
Found it:

Code:
DirectoryProvider[] provArr = sFactory.getDirectoryProviders(c);


returns null for a specific class, so my iteration breaks.


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.