espirio
espirio

Reputation: 189

Search with Hibernate Search

I have designed a dialog where the user can search in the database. The user enters the text you want and then you want to search in one or in all tables.

The tables have String, Double, Boolean, Date and your own objects as fields. The search works for String and Number fields, but in the date fields I have problems.

Search Example: I want to search for "2015".

I have two examples for Querys:

Example 1:

org.apache.lucene.search.Query luceneQuery = qb
          .keyword().wildcard()
          .onFields( fields )
          .matching( "2015" )
          .createQuery();

This query I get an empty result.

Example 2:

org.apache.lucene.search.Query luceneQuery = qb
          .keyword()
          .onFields( fields )
          .matching( "2015" )
          .createQuery();

Here I always get back all the entities that have a date.

For the date fields I have written a custom FieldBridge.

import java.util.Calendar;
import java.util.Date;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.TwoWayFieldBridge;

public class DateStringFieldBridge implements TwoWayFieldBridge
{
  private Calendar dateValue;
  private String   dateString;

  @Override
  public void set( final String name, final Object value, final Document document, final LuceneOptions luceneOptions )
  {
    if ( value != null )
    {
      final Calendar cal = Calendar.getInstance();
      cal.setTime( (Date) value );
      dateValue = cal;

      final String day =
          cal.get( Calendar.DAY_OF_MONTH ) < 10 ? 0 + "" + cal.get( Calendar.DAY_OF_MONTH ) : "" + cal.get( Calendar.DAY_OF_MONTH );
      final int monthValue = cal.get( Calendar.MONTH ) + 1;
      final String month = monthValue < 10 ? 0 + "" + monthValue : "" + monthValue;
      final String year = "" + cal.get( Calendar.YEAR );
      final String hour =
          cal.get( Calendar.HOUR_OF_DAY ) < 10 ? 0 + "" + cal.get( Calendar.HOUR_OF_DAY ) : "" + cal.get( Calendar.HOUR_OF_DAY );
      final String minute = cal.get( Calendar.MINUTE ) < 10 ? 0 + "" + cal.get( Calendar.MINUTE ) : "" + cal.get( Calendar.MINUTE );

      dateString = day + "." + month + "." + year + " " + hour + ":" + minute;
      if ( dateValue != null && luceneOptions != null && document != null )
        addStringField( name, dateString, document, luceneOptions );
    }
  }

  private void addStringField( final String fieldName, final String fieldValue, final Document document, final LuceneOptions luceneOptions )
  {
    final Field field =
        new Field( fieldName, fieldValue, luceneOptions.getStore(), luceneOptions.getIndex(), luceneOptions.getTermVector() );
    field.setBoost( luceneOptions.getBoost() );

    document.add( field );
  }

  @Override
  public Object get( final String arg0, final Document arg1 )
  {
    return dateValue.getTime();
  }

  @Override
  public String objectToString( final Object arg0 )
  {
    return dateValue == null ? "" : dateString;
  }

}

How do I get the correct result?

Upvotes: 1

Views: 623

Answers (1)

espirio
espirio

Reputation: 189

I found the mistake. The method objectToString( final Object arg0 ) in my FieldBridge was wrong.

The passed variable is the search value. I thought that it is the value of the field.

Solution for String searchValue:

  @Override
  public String objectToString( final Object searchValue )
  {
    return searchValue.toString();
  }

Upvotes: 1

Related Questions