-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: [Hibernate Search] Using Named Filters in Hibernate search
PostPosted: Mon Nov 12, 2007 7:52 pm 
Newbie

Joined: Mon Nov 12, 2007 7:49 pm
Posts: 7
Location: Atlanta GA
I am trying to use some named filters (org.hibernate.annotations.Filter) in hibernate search. But when I try to filter out a negative object (supposed to be filtered out) which I just persisted in in the unit test, the named filter fails to do so. The hibernate search still finds the one just persisted in. However, there are some negative records originally in the table which are successfully filtered out. I am wondering what the reason could be. Anybody ever used regular named filter in Hibernate search?


Top
 Profile  
 
 Post subject: Nobody ever used it this way?
PostPosted: Mon Nov 19, 2007 8:10 pm 
Newbie

Joined: Mon Nov 12, 2007 7:49 pm
Posts: 7
Location: Atlanta GA
Nobody ever used named filters in Hibernate Search? I am not sure about this, because Hibernate Search has its own filter class. But I want to implement some filter more powerful than what the Hibernate search filter can do. So, I have to use named filters instead. Is there anyone who has experience on this? Or, a better solution is welcome. I appreciate your help.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 11:14 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
I did not understand completely your initial question. Could you be a bit more specific, for example giving some snippet of code that lead to the problem. I'm especially interested by your transaction boundaries

_________________
Emmanuel


Top
 Profile  
 
 Post subject: A simple example
PostPosted: Wed Nov 21, 2007 12:01 pm 
Newbie

Joined: Mon Nov 12, 2007 7:49 pm
Posts: 7
Location: Atlanta GA
For Example,

We define a simple filter as follows:

@FilterDef( name="FilterA",
parameters = { @org.hibernate.annotations.ParamDef(
name = "ParameterA", type = "string" )
}
)

@Filter(name = "FilterA",
condition = <SQL>)


In the Unit test

public void testA(){

//fill the "objectA" with negative data (supposed to be filtered out)
...

EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
try {
daoA.persist(ObjectA, UNITTEST);
transaction.commit();

//The result still includes the negative data just persisted in
List results = dao.search(...);

//Assertion
...

}catch(Exception e) {
e.printStackTrace();
transaction.rollback();
fail("Exception thrown.");
}finally {
if(! transaction.isActive())
transaction.begin();
daoA.delete(ObjectA, UNITTEST);
transaction.commit();
}
}


The Method "search(...)"

public List search( ...) throws ParseException
{
List result = null;
Session session = (Session) entityManager.getDelegate();
try {
Filter filter = session.enableFilter("FilterA");
filter.setParameter("ParameterA", ...);
result = hibernateSearch(...);

}finally{
session.disableFilter("FilterA");
}
return result;
}


The method "hibernateSearch(...)"

public List hibernateSearch( ...) throws ParseException{
List result = ...;

FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.createFullTextEntityManager(entityManager);
String[] fields = {...};

MultiFieldQueryParser parser = new MultiFieldQueryParser( fields, new StandardAnalyzer());
parser.setDefaultOperator(MultiFieldQueryParser.OR_OPERATOR);
org.apache.lucene.search.Query query = parser.parse(<A Criterion>);
FullTextQuery hiQuery = fullTextEntityManager.createFullTextQuery( query, A.class );
result = hiQuery.getResultList()

return result;
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 21, 2007 7:39 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Ahh,
I understand now.
I thought you where talking about the Hibernate Search filter feature.

I think what you need to do is to implement the same filter strategy for both Hibernate core filters and Hibernate Search filters.
Here is the link to the Hibernate Search filter feature.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: about hibernate search filter
PostPosted: Mon Nov 26, 2007 11:53 am 
Newbie

Joined: Mon Nov 12, 2007 7:49 pm
Posts: 7
Location: Atlanta GA
Thanks , emmanuel.

But the problem is not as easy as you think. I tried Hibernate search filter before. It looks like that hibernate search filters still stay in property level in a class according to the doc. We need more powerful filters than those. The named filters can do what we want in the implementation. But the problem is that if they can be used with Hibernate search as I have shown in the code. Do you understand what I mean here?

I use a named filter and hibernate search together. The strange thing is that some negative records which are in the table can be filtered out but not the negative data we just persisted in. I don't know what causes this.



Dan


Top
 Profile  
 
 Post subject: Re: about hibernate search filter
PostPosted: Mon Nov 26, 2007 5:19 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
danyuan wrote:
But the problem is not as easy as you think. I tried Hibernate search filter before. It looks like that hibernate search filters still stay in property level in a class according to the doc. We need more powerful filters than those. The named filters can do what we want in the implementation. But the problem is that if they can be used with Hibernate search as I have shown in the code. Do you understand what I mean here?


Actually no :)

Especially, I don't know what you mean by "still stay in property level in a class according to the doc". The Hibernate Search filter is of you own implementation, you can do the hell you want in it

_________________
Emmanuel


Top
 Profile  
 
 Post subject: An Example
PostPosted: Mon Nov 26, 2007 6:24 pm 
Newbie

Joined: Mon Nov 12, 2007 7:49 pm
Posts: 7
Location: Atlanta GA
Let me show you an example,

I have a named filter defined as follows:

@Filter(name = "FilterA",
condition = "0 < (select count(*) from Customer_Account ca where ca.customer_id = customer_id "+ "and ca.auction_id = (select af.auction_id from auction_facility af where af.auction_cd = :auctionCd))")


The condition of this filter is a little complex.

My question is if it is possible to implement this filter using hibernate search filter. I am not sure about this because I can only see some simple examples in the document. The condition of this named filter is related to two tables. Do you have any idea about this?

Thanks

Dan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 26, 2007 8:16 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
yes technically you can do that:
the hsearch filter would query the database to get more or less the query you're using (probably for all accounts upfront though)
But this might not be the best strategy.

Another solution is to write a custom fieldBridge at the account class level that stores a precomputed value in your index. you will then be able to filter from it more efficiently.

_________________
Emmanuel


Top
 Profile  
 
 Post subject: FieldBridge
PostPosted: Tue Nov 27, 2007 3:47 pm 
Newbie

Joined: Mon Nov 12, 2007 7:49 pm
Posts: 7
Location: Atlanta GA
Well, so glad to hear a better solution. I read someting about the custom fieldbridge in the document. But the example there is not quite close to what I want to solve here. Can you point me to some other documents which are more related to my case?

Thanks

Dan


Top
 Profile  
 
 Post subject: same problem with filters defined in xml file
PostPosted: Wed Jan 09, 2008 11:08 am 
Newbie

Joined: Wed Jan 09, 2008 10:51 am
Posts: 4
I have several filters defined in my mapping xml file and they don't seem to work in FullTextQueries. It would be very tedious and error prone to maintain both, normal and fulltext filters. Is there any workaround for this?

When I turn on SQL logging I can actually see the filter clauses in the statement. Why is the sql statement correct but the result is not filtered?


Top
 Profile  
 
 Post subject: Re: same problem with filters defined in xml file
PostPosted: Wed Jan 09, 2008 7:31 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
WhiteAndNerdy wrote:
I have several filters defined in my mapping xml file and they don't seem to work in FullTextQueries. It would be very tedious and error prone to maintain both, normal and fulltext filters. Is there any workaround for this?


It's really 2 different beasts working at different levels. So mutualizing is not conceptually easy.

Quote:
When I turn on SQL logging I can actually see the filter clauses in the statement. Why is the sql statement correct but the result is not filtered?


That, I don't really understand :)

_________________
Emmanuel


Top
 Profile  
 
 Post subject: solution
PostPosted: Thu Jan 10, 2008 10:31 am 
Newbie

Joined: Wed Jan 09, 2008 10:51 am
Posts: 4
If you restrict the query to a single class, e.g.

FullTextQuery query = fullTextSession.createFullTextQuery(luceneQuery, MyClass.class);

the filters are included in the select statement. The reason why the filters didn't work was due to my test setup. The objects were generated and searched in the same session, so the result probably came from the first level cache. But then it is still strange that an sql statement was sent to the db anyway.

In a separate session the filters do work :)


If you do a global search over multiple classes, the objects are retrieved by Session.get(id), which ignores filters. You can see that in the logged sql statement.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 10, 2008 1:56 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
right, when using a single return class, HSearch uses a query which is filtered by Hibernate Core, but not if the returned class are multiple.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Mon Feb 04, 2008 9:57 am 
Newbie

Joined: Wed Jan 09, 2008 10:51 am
Posts: 4
To get the hibernate filters working I iterate over my classes and collect the search results. I just don't wont to maintain the filter logic in two places because it's tedious and errorprone. But by doing so I lose the overall search ranking.
I find this behavior a bit opaque. It basically means, that the application of filters depends on the number of classes involved. Is this intended or is it just a consequence of implementation? I've never seen it mentioned in the documentation. Will this behavior be maintained in the future?

Another problem I have is the resutlSize. FullTextQuery.getResultSize() doesn't work for me because it will just look in the index, but I need the filters again. Is there a way to set a "count(*)" projection in the FullTextQuery? The api doc says
Quote:
No projection (criteria.setProjection() ) allowed, the root entity must be the only returned type No where restriction can be defined either.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 16 posts ]  Go to page 1, 2  Next

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.