-->
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: Hibernate Search: filter on Collection Bridge
PostPosted: Thu Jan 07, 2010 11:40 pm 
Newbie

Joined: Wed Mar 24, 2004 3:01 pm
Posts: 6
Hi,

I have a custom Bridge for my Set, that generates a String like this when indexed by Lucene: "value1 value2 value3". I'm trying to use a Filter @Factory to filter the result inside the String returned by my custom Bridge, like this:
- Returned by Bridge: "value1 value2 value3"
- Filter the results that contains only: "value2", for example.

How can I do that? I'm trying the code bellow, and everything is working fine, but my custom Filter @Factory.

- Bridge:
Code:
public class NegotiationTypeBridge implements TwoWayFieldBridge
{

    @Override
    public void set(String name, Object value, Document document, LuceneOptions options)
    {
        if (!(value instanceof Set<?>))
        {
            throw new IllegalArgumentException("The object must be a Set.");
        }

        StringBuilder negotiations = new StringBuilder("");
        final String token = " ";

        @SuppressWarnings("unchecked")
        Set<NegotiationType> negotiationTypes = (Set<NegotiationType>) value;
        for (NegotiationType negotiationType : negotiationTypes)
        {
            negotiations.append(negotiationType.getDescription());
            negotiations.append(token);
        }

        Field field = new Field(name, negotiations.toString().trim(), options.getStore(),
                options.getIndex());
        if (options.getBoost() != null)
        {
            field.setBoost(options.getBoost());
        }
        document.add(field);
    }

    public Object get(String name, Document document)
    {
        Field field = (Field) document.getField("negotiationTypes");
        return field.stringValue();
    }

    public String objectToString(Object object)
    {
        String negotiationTypes = (String) object;
        return negotiationTypes;
    }
   
}


Filter @Factory:
Code:
public class NegotiationTypeFilterFactory
{
    private String negotiationType;

    public void setNegotiationType(String negotiationType)
    {
        this.negotiationType = negotiationType;
    }

    @Factory
    public Filter buildFilter()
    {
        Term term = new Term("negotiationTypes", negotiationType);
        TermQuery query = new TermQuery(term);
        Filter filter = new QueryWrapperFilter(query);
        filter = new CachingWrapperFilter(filter);
        return filter;
    }

    @Key
    public FilterKey getKey()
    {
        StandardFilterKey key = new StandardFilterKey();
        key.addParameter(negotiationType);
        return key;
    }
}


Thank you!

_________________
Maikel A. Arabori - Curitiba/PR - Brazil


Top
 Profile  
 
 Post subject: Re: Hibernate Search: filter on Collection Bridge
PostPosted: Mon Jan 11, 2010 11:30 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
On the first look your approach looks ok. Can you specify your problem in more detail? How does the underlying entity model look like?

--Hardy


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.