Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: [Hibernate Search] Or query for a field
PostPosted: Fri Jun 15, 2012 6:52 am 
Beginner
Beginner

Joined: Thu Apr 14, 2005 4:29 am
Posts: 28
Hi,

I have a little Hibernate Search problem. I have a class Person with the follwoing content:

Code:
@Entity
@Indexed
@Boost(2f)
public class Person {
..
    @Column(nullable = false, length = 200)
    @Field(index = Index.YES, store = Store.NO, boost = @Boost(2f))
    private String name;

    @Column(nullable = false, length = 20)
    @Enumerated(EnumType.STRING)
    @Field(index = Index.YES, store = Store.NO, boost = @Boost(1.5f))
    @FieldBridge(impl = EnumBridge.class)
    private Country country;

    @Column(nullable = false)
    @Field(index = Index.YES, store = Store.NO, boost = @Boost(1.5f))
    @NumericField(precisionStep = 1)
    private Integer year;


Country is a simple enum:
Code:
public enum Country {
    BELGIUM(),
    IRELAND(),
   ..;
   
   Country()
    {       
    }
   
   ..


Now I want to write the following search method:
Code:
    public List<Person> findPersons(String name, List<Country> countries, Integer yearFrom, Integer yearTo) {
        QueryBuilder builder = getQueryBuilder();
       
        // create lucene query
        BooleanJunction<?> bool = builder.bool();
       
        // name
        if (StringUtils.isNotBlank(name)) {
            bool.must(builder.keyword().onField("name").boostedTo(3).matching(name).createQuery());
        }
       
        // country
        if (null != countries) {
            // FIXME this works not in all cases
            StringBuilder value = new StringBuilder();
            for (Country country : countries) {
                value.append(country).append(" ");
            }
            bool.must(builder.phrase().onField("country").sentence(value.toString()).createQuery());
        }
       
        // year
        if (yearFrom != null || yearTo != null)
        {
            bool.must(builder.range().onField("year").from(yearFrom).to(yearTo).createQuery());
        }
           
        Query luceneQuery = bool.createQuery();
      
      // ..


All search parameters must be fulfilled if the parameter is not null. Now I have problems with the field country.
If the countries list contains only one Country it works, otherwise not.

My solutions with
Code:
builder.phrase().onField("country").sentence(value.toString()).createQuery()
looks not good and doesn't work for multiple countries.
Is there no way with
Code:
builder.keyword().onField("country").matching(..)
and an OR relation? What's the best way?

Every help is very appreciated!

Thanks a lot,
Detlev


Top
 Profile  
 
 Post subject: Re: [Hibernate Search] Or query for a field
PostPosted: Fri Jun 15, 2012 2:09 pm 
Beginner
Beginner

Joined: Thu Apr 14, 2005 4:29 am
Posts: 28
I found the solution (friday blackout):

Code:
        if (null != countries) {
            StringBuilder value = new StringBuilder();
            for (Country country : countries) {
                value.append(country).append(" ");
            }
            bool.must(builder.keyword().onField("country").ignoreFieldBridge().matching(value.toString()).createQuery());
        }       


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 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.