-->
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: Ignoring Empty Fields
PostPosted: Tue Jan 17, 2012 7:56 pm 
Newbie

Joined: Thu Dec 08, 2011 5:43 pm
Posts: 7
How do I get a list of all fields that contain at least one term?

I'm creating an object that uses a classbridge to create custom fields to search on. This works pretty much perfectly, and I love it. When I create object A with custom fields fruit:apples and apple:gala, it works fine. When I delete object A, all references to fields "fruit" and "apple" are deleted. When I create object B with custom fields fruit:pears and pear:bartlett, the reference to field "apple" comes back! I don't really care that it's there, because... it doesn't really effect anything when searching. However, I want to display a list of all fields my users can search on, and displaying "apple" when none exist is behavior that I don't want.

Here's how I'm currently getting a list of fields:
Code:
public String[] getFields() throws Exception {
   SearchFactory sf = Search.getFullTextEntityManager(em).getSearchFactory();
   Collection<String> tmpFields = sf.getIndexReaderAccessor().open(JPAObject.class).getFieldNames(FieldOption.INDEXED);
   String[] fields = tmpFields.toArray(new String[tmpFields.size()]);
   return fields;
}


Thanks


Top
 Profile  
 
 Post subject: Re: Ignoring Empty Fields
PostPosted: Tue Jan 24, 2012 6:44 am 
Hibernate Team
Hibernate Team

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

I don't think you can rely on IndexReader#getFieldNames for your usecase. Even though there won't be a document matching a search in the field "apple" after you deleted the document containing this field, the field info file will still contain this information. At the very least until the next optimize when deleted documents are actually removed from the index.

Have you tried calling optimize prior to retrieving the list?

--Hardy


Top
 Profile  
 
 Post subject: Re: Ignoring Empty Fields
PostPosted: Wed Jan 25, 2012 11:08 am 
Newbie

Joined: Thu Dec 08, 2011 5:43 pm
Posts: 7
hardy.ferentschik wrote:
Have you tried calling optimize prior to retrieving the list?

I tried running optimize while doing my testing, and it didn't help. I tried running optimize in Luke when these empty fields existed and it didn't help. If it helps anyone track down how to really solve this, I'm using JPA to persist things. As far as I can tell, these fields are never being removed from my index, regardless of whether the object was deleted or not. To be clear, everything appears to delete correctly up until the very last deletion.

I actually did find a workaround though. It's kind of messy, but it works. I essentially cobbled this together from Luke's source, actually.

Code:
      SearchFactory sf = Search.getFullTextEntityManager(em).getSearchFactory();
      IndexReader ir = sf.getIndexReaderAccessor().open(myClass.class);
      
      Term currTerm;
      Set<String> fieldsHaveTerms = new HashSet<String>();
      
      TermEnum te = ir.terms();
      while(te.next()) {
         currTerm = te.term();
         if(currTerm.field().charAt(0) != '_') {
            fieldsHaveTerms.add(currTerm.field());
         }
      }
      
      List<String> fields = new ArrayList<String>(fieldsHaveTerms);
      Collections.sort(fields);
      
      sf.getIndexReaderAccessor().close(ir);
      return fields;


This gets me a list of all fields that have values.


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.