-->
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: BridgeException while searching on Date field
PostPosted: Mon Jan 10, 2011 4:48 am 
Newbie

Joined: Mon Jan 10, 2011 4:40 am
Posts: 2
Hi, I can't search Date fields:

Method in DAO (note: comments in queryBuilder call):
Code:
public List<T> search(String searchString, String sortField, String sortOrder, Integer offset, Integer limit)
{      
   FullTextEntityManager fullTextEntityManager = this.getFullTextEntityManager();

   SearchFactory searchFactory = fullTextEntityManager.getSearchFactory();
   QueryBuilder queryBuilder = searchFactory.buildQueryBuilder().forEntity(this.persistentClass).get();

   org.apache.lucene.search.Query query = queryBuilder
        .keyword()
        .wildcard() // i also tried removing this line
        .onField(sortField) // anyway, let's say sortField is "updatedAt"
        .matching("*"+searchString+"*")
        .createQuery();
      
   FullTextQuery fullTextQuery  = fullTextEntityManager.createFullTextQuery(query, this.persistentClass);

   Boolean isReversed = false;
   if(sortOrder.equals("desc")) {
      isReversed = true;
   }
      
   fullTextQuery.setSort(new Sort(new SortField(sortField, SortField.STRING, isReversed)));
   fullTextQuery.setFirstResult(offset);
   fullTextQuery.setMaxResults(limit);
   
   return fullTextQuery.getResultList();
}


When it runs, I get this error:
Code:
org.hibernate.search.bridge.BridgeException: Exception while calling bridge#objectToString
   class: com.mycompany.system.model.MyModel
   field bridge: updatedAt
   at org.hibernate.search.bridge.util.ContextualExceptionBridge.buildBridgeException(ContextualExceptionBridge.java:77)
   at org.hibernate.search.bridge.util.ContextualException2WayBridge.objectToString(ContextualException2WayBridge.java:76)
   at org.hibernate.search.engine.DocumentBuilderIndexedEntity.objectToString(DocumentBuilderIndexedEntity.java:304)
   at org.hibernate.search.engine.DocumentBuilderIndexedEntity.objectToString(DocumentBuilderIndexedEntity.java:596)
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.buildSearchTerm(ConnectedMultiFieldsTermQueryBuilder.java:133)
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:92)
   at org.hibernate.search.query.dsl.impl.ConnectedMultiFieldsTermQueryBuilder.createQuery(ConnectedMultiFieldsTermQueryBuilder.java:73)
   at com.mycompany.web.dao.impl.GenericDaoJpa.search(GenericDaoJpa.java:235)
   (rest of stack omitted...)


MyModel's updatedAt field is already indexed:
Code:
@Column(name="updated_at", nullable=false)
@Field(index=Index.UN_TOKENIZED, store=Store.YES)
@DateBridge(resolution=Resolution.SECOND)
private Date updatedAt = new Date();


Can anyone help? :D Thanks!


Top
 Profile  
 
 Post subject: Re: BridgeException while searching on Date field
PostPosted: Mon Jan 10, 2011 6:34 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
I think your problem is that you are searching on a date field. One advantage of the query DSL is that it automatically converts indexed objects into their string representation using the corresponding field bridge. This way you can pass an actual Date instance into matching() without first converting it into a string (remember all queries in Lucene are in the end string based). In your case you actually have a search string and hence this conversion should not occur. You can achieve this via ignoreFieldBridge():
Code:
   org.apache.lucene.search.Query query = queryBuilder
        .keyword()
        .wildcard() // i also tried removing this line
        .onField(sortField) // anyway, let's say sortField is "updatedAt"
        .matching("*"+searchString+"*")
        .ignoreFieldBridge()
        .createQuery();

That said, I am not sure how useful it is to have a wildcard query on a date bridge, especially one with a leading wildcard. Maybe a range query would be more suited?

--Hardy


Top
 Profile  
 
 Post subject: Re: BridgeException while searching on Date field
PostPosted: Mon Jan 10, 2011 6:52 am 
Newbie

Joined: Mon Jan 10, 2011 4:40 am
Posts: 2
Thanks Hardy, that did the trick I just had to put .ignoreFieldBridge() above .matching instead :D

The reason I'm searching Date fields is because I'm using DataTables and when I sort the thing (in this case by a Date field), the search string also gets passed. I don't want to modify the JS behind it as to not have an additional layer of complexity and so I'm trying to make ends meet on the Hibernate side in which it is doing quite well. At least for now it can work that way.

You've been helpful, more power to you guys!


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.