[hibernate-commits] Hibernate SVN: r17202 - in search/trunk/src: main/java/org/hibernate/search/bridge/builtin and 1 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Mon Jul 27 06:21:41 EDT 2009
Author: hardy.ferentschik
Date: 2009-07-27 06:21:41 -0400 (Mon, 27 Jul 2009)
New Revision: 17202
Added:
search/trunk/src/main/java/org/hibernate/search/bridge/builtin/CharacterBridge.java
Modified:
search/trunk/src/main/java/org/hibernate/search/bridge/BridgeFactory.java
search/trunk/src/main/java/org/hibernate/search/bridge/FieldBridge.java
search/trunk/src/test/java/org/hibernate/search/test/bridge/BridgeTest.java
search/trunk/src/test/java/org/hibernate/search/test/bridge/Cloud.java
Log:
HSEARCH-334
Applied Davide's patch
Modified: search/trunk/src/main/java/org/hibernate/search/bridge/BridgeFactory.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/bridge/BridgeFactory.java 2009-07-25 05:24:55 UTC (rev 17201)
+++ search/trunk/src/main/java/org/hibernate/search/bridge/BridgeFactory.java 2009-07-27 10:21:41 UTC (rev 17202)
@@ -22,6 +22,7 @@
import org.hibernate.search.bridge.builtin.BigDecimalBridge;
import org.hibernate.search.bridge.builtin.BigIntegerBridge;
import org.hibernate.search.bridge.builtin.BooleanBridge;
+import org.hibernate.search.bridge.builtin.CharacterBridge;
import org.hibernate.search.bridge.builtin.DateBridge;
import org.hibernate.search.bridge.builtin.DoubleBridge;
import org.hibernate.search.bridge.builtin.EnumBridge;
@@ -45,6 +46,8 @@
private BridgeFactory() {
}
+ public static final TwoWayFieldBridge CHARACTER = new TwoWayString2FieldBridgeAdaptor( new CharacterBridge() );
+
public static final TwoWayFieldBridge DOUBLE = new TwoWayString2FieldBridgeAdaptor( new DoubleBridge() );
public static final TwoWayFieldBridge FLOAT = new TwoWayString2FieldBridgeAdaptor( new FloatBridge() );
@@ -79,6 +82,8 @@
new TwoWayString2FieldBridgeAdaptor( DateBridge.DATE_MILLISECOND );
static {
+ builtInBridges.put( Character.class.getName(), CHARACTER );
+ builtInBridges.put( char.class.getName(), CHARACTER );
builtInBridges.put( Double.class.getName(), DOUBLE );
builtInBridges.put( double.class.getName(), DOUBLE );
builtInBridges.put( Float.class.getName(), FLOAT );
Modified: search/trunk/src/main/java/org/hibernate/search/bridge/FieldBridge.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/bridge/FieldBridge.java 2009-07-25 05:24:55 UTC (rev 17201)
+++ search/trunk/src/main/java/org/hibernate/search/bridge/FieldBridge.java 2009-07-27 10:21:41 UTC (rev 17202)
@@ -6,7 +6,7 @@
/**
* Link between a java property and a Lucene Document
* Usually a Java property will be linked to a Document Field.
- *
+ * <p/>
* All implementations need to be threadsafe.
*
* @author Emmanuel Bernard
@@ -15,11 +15,15 @@
/**
* Manipulate the document to index the given value.
- * A common implementation is to add a Field <code>name</code> to the given document following
- * the parameters (<code>store</code>, <code>index</code>, <code>boost</code>) if the
- * <code>value</code> is not null
- * @param luceneOptions Contains the parameters used for adding <code>value</code> to
- * the Lucene <code>document</code>.
+ * <p/>
+ * A common implementation is to add a Field with the given {@code name} to {@code document} following
+ * the parameters {@code luceneOptions} if the {@code value} is not {@code null}.
+ *
+ * @param name The field to add to the Lucene document
+ * @param value The actual value to index
+ * @param document The Lucene document into which we want to index the value.
+ * @param luceneOptions Contains the parameters used for adding {@code value} to
+ * the Lucene document.
*/
void set(String name, Object value, Document document, LuceneOptions luceneOptions);
}
Added: search/trunk/src/main/java/org/hibernate/search/bridge/builtin/CharacterBridge.java
===================================================================
--- search/trunk/src/main/java/org/hibernate/search/bridge/builtin/CharacterBridge.java (rev 0)
+++ search/trunk/src/main/java/org/hibernate/search/bridge/builtin/CharacterBridge.java 2009-07-27 10:21:41 UTC (rev 17202)
@@ -0,0 +1,28 @@
+// $Id:$
+package org.hibernate.search.bridge.builtin;
+
+import org.hibernate.util.StringHelper;
+
+/**
+ * Map a character element
+ *
+ * @author Davide D'Alto
+ */
+public class CharacterBridge implements org.hibernate.search.bridge.TwoWayStringBridge {
+
+ public Object stringToObject(String stringValue) {
+ if ( StringHelper.isEmpty( stringValue ) ) {
+ return null;
+ }
+ if ( stringValue.length() > 1 ) {
+ throw new IllegalArgumentException( "<" + stringValue + "> is not a char" );
+ }
+ return stringValue.charAt( 0 );
+ }
+
+ public String objectToString(Object object) {
+ return object == null
+ ? null
+ : object.toString();
+ }
+}
Property changes on: search/trunk/src/main/java/org/hibernate/search/bridge/builtin/CharacterBridge.java
___________________________________________________________________
Name: svn:keywords
+ Id
Modified: search/trunk/src/test/java/org/hibernate/search/test/bridge/BridgeTest.java
===================================================================
--- search/trunk/src/test/java/org/hibernate/search/test/bridge/BridgeTest.java 2009-07-25 05:24:55 UTC (rev 17201)
+++ search/trunk/src/test/java/org/hibernate/search/test/bridge/BridgeTest.java 2009-07-27 10:21:41 UTC (rev 17202)
@@ -1,179 +1,198 @@
//$Id$
package org.hibernate.search.test.bridge;
+import java.net.URI;
+import java.net.URL;
+import java.util.Calendar;
import java.util.Date;
+import java.util.GregorianCalendar;
import java.util.List;
-import java.util.GregorianCalendar;
-import java.util.Calendar;
import java.util.TimeZone;
-import java.net.URI;
-import java.net.URL;
+import org.apache.lucene.analysis.SimpleAnalyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.BooleanClause;
+import org.apache.lucene.search.BooleanQuery;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.TermQuery;
+
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
-
-import org.hibernate.search.test.SearchTestCase;
import org.hibernate.search.Environment;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
-import org.apache.lucene.search.Query;
-import org.apache.lucene.search.TermQuery;
-import org.apache.lucene.search.BooleanQuery;
-import org.apache.lucene.search.BooleanClause;
-import org.apache.lucene.queryParser.QueryParser;
-import org.apache.lucene.analysis.standard.StandardAnalyzer;
-import org.apache.lucene.analysis.SimpleAnalyzer;
-import org.apache.lucene.index.Term;
+import org.hibernate.search.test.SearchTestCase;
/**
* @author Emmanuel Bernard
*/
public class BridgeTest extends SearchTestCase {
- public void testDefaultAndNullBridges() throws Exception {
- Cloud cloud = new Cloud();
- cloud.setMyDate( null );
- cloud.setDouble1( null );
- cloud.setDouble2( 2.1d );
- cloud.setIntegerv1( null );
- cloud.setIntegerv2( 2 );
- cloud.setFloat1( null );
- cloud.setFloat2( 2.1f );
- cloud.setLong1( null );
- cloud.setLong2( 2l );
- cloud.setString(null);
+ public void testDefaultAndNullBridges() throws Exception {
+ Cloud cloud = new Cloud();
+ cloud.setMyDate( null );
+ cloud.setDouble1( null );
+ cloud.setDouble2( 2.1d );
+ cloud.setIntegerv1( null );
+ cloud.setIntegerv2( 2 );
+ cloud.setFloat1( null );
+ cloud.setFloat2( 2.1f );
+ cloud.setLong1( null );
+ cloud.setLong2( 2l );
+ cloud.setString( null );
cloud.setType( CloudType.DOG );
+ cloud.setChar1( null );
+ cloud.setChar2( 'P' );
cloud.setStorm( false );
cloud.setClazz( Cloud.class );
- cloud.setUri( new URI("http://www.hibernate.org") );
- cloud.setUrl( new URL("http://www.hibernate.org") );
+ cloud.setUri( new URI( "http://www.hibernate.org" ) );
+ cloud.setUrl( new URL( "http://www.hibernate.org" ) );
org.hibernate.Session s = openSession();
- Transaction tx = s.beginTransaction();
- s.persist(cloud);
- s.flush();
- tx.commit();
+ Transaction tx = s.beginTransaction();
+ s.persist( cloud );
+ s.flush();
+ tx.commit();
- tx = s.beginTransaction();
- FullTextSession session = Search.getFullTextSession(s);
- QueryParser parser = new QueryParser("id", new StandardAnalyzer() );
- Query query;
- List result;
+ tx = s.beginTransaction();
+ FullTextSession session = Search.getFullTextSession( s );
+ QueryParser parser = new QueryParser( "id", new StandardAnalyzer() );
+ Query query;
+ List result;
- query = parser.parse("double2:[2.1 TO 2.1] AND float2:[2.1 TO 2.1] " +
- "AND integerv2:[2 TO 2.1] AND long2:[2 TO 2.1] AND type:\"dog\" AND storm:false");
+ query = parser.parse(
+ "double2:[2.1 TO 2.1] AND float2:[2.1 TO 2.1] " +
+ "AND integerv2:[2 TO 2.1] AND long2:[2 TO 2.1] AND type:\"dog\" AND storm:false"
+ );
- result = session.createFullTextQuery(query).list();
- assertEquals( "find primitives and do not fail on null", 1, result.size() );
+ result = session.createFullTextQuery( query ).list();
+ assertEquals( "find primitives and do not fail on null", 1, result.size() );
- query = parser.parse("double1:[2.1 TO 2.1] OR float1:[2.1 TO 2.1] OR integerv1:[2 TO 2.1] OR long1:[2 TO 2.1]");
- result = session.createFullTextQuery(query).list();
- assertEquals( "null elements should not be stored", 0, result.size() ); //the query is dumb because restrictive
+ query = parser.parse( "double1:[2.1 TO 2.1] OR float1:[2.1 TO 2.1] OR integerv1:[2 TO 2.1] OR long1:[2 TO 2.1]" );
+ result = session.createFullTextQuery( query ).list();
+ assertEquals( "null elements should not be stored", 0, result.size() ); //the query is dumb because restrictive
- query = parser.parse("type:dog");
- result = session.createFullTextQuery(query).setProjection( "type" ).list();
- assertEquals( "Enum projection works", 1, result.size() ); //the query is dumb because restrictive
+ query = parser.parse( "type:dog" );
+ result = session.createFullTextQuery( query ).setProjection( "type" ).list();
+ assertEquals( "Enum projection works", 1, result.size() ); //the query is dumb because restrictive
- query = new TermQuery( new Term("clazz", Cloud.class.getName() ) );
- result = session.createFullTextQuery(query).setProjection( "clazz" ).list();
- assertEquals( "Clazz projection works", 1, result.size() );
- assertEquals( "Clazz projection works", Cloud.class.getName(), ( (Class) ((Object[])result.get(0))[0] ).getName() );
+ query = new TermQuery( new Term( "clazz", Cloud.class.getName() ) );
+ result = session.createFullTextQuery( query ).setProjection( "clazz" ).list();
+ assertEquals( "Clazz projection works", 1, result.size() );
+ assertEquals(
+ "Clazz projection works",
+ Cloud.class.getName(),
+ ( ( Class ) ( ( Object[] ) result.get( 0 ) )[0] ).getName()
+ );
BooleanQuery bQuery = new BooleanQuery();
- bQuery.add( new TermQuery( new Term("uri", "http://www.hibernate.org" ) ), BooleanClause.Occur.MUST );
- bQuery.add( new TermQuery( new Term("url", "http://www.hibernate.org" ) ), BooleanClause.Occur.MUST );
+ bQuery.add( new TermQuery( new Term( "uri", "http://www.hibernate.org" ) ), BooleanClause.Occur.MUST );
+ bQuery.add( new TermQuery( new Term( "url", "http://www.hibernate.org" ) ), BooleanClause.Occur.MUST );
- result = session.createFullTextQuery(bQuery).setProjection( "clazz" ).list();
- assertEquals( "Clazz projection works", 1, result.size() );
+ result = session.createFullTextQuery( bQuery ).setProjection( "clazz" ).list();
+ assertEquals( "Clazz projection works", 1, result.size() );
+ query = parser.parse( "char1:[" + String.valueOf( Character.MIN_VALUE ) + " TO " + String.valueOf( Character.MAX_VALUE ) + "]" );
+ result = session.createFullTextQuery( query ).setProjection( "char1" ).list();
+ assertEquals( "Null elements should not be stored, CharacterBridge is not working", 0, result.size() );
+
+ query = parser.parse( "char2:P" );
+ result = session.createFullTextQuery( query ).setProjection( "char2" ).list();
+ assertEquals( "Wrong results number, CharacterBridge is not working", 1, result.size() );
+ assertEquals( "Wrong result, CharacterBridge is not working", 'P', ( ( Object[] ) result.get( 0 ) )[0] );
+
s.delete( s.get( Cloud.class, cloud.getId() ) );
- tx.commit();
- s.close();
+ tx.commit();
+ s.close();
- }
+ }
- public void testCustomBridges() throws Exception {
- Cloud cloud = new Cloud();
- cloud.setCustomFieldBridge( "This is divided by 2");
- cloud.setCustomStringBridge( "This is div by 4");
- org.hibernate.Session s = openSession();
- Transaction tx = s.beginTransaction();
- s.persist(cloud);
- s.flush();
- tx.commit();
+ public void testCustomBridges() throws Exception {
+ Cloud cloud = new Cloud();
+ cloud.setCustomFieldBridge( "This is divided by 2" );
+ cloud.setCustomStringBridge( "This is div by 4" );
+ org.hibernate.Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ s.persist( cloud );
+ s.flush();
+ tx.commit();
- tx = s.beginTransaction();
- FullTextSession session = Search.getFullTextSession(s);
- QueryParser parser = new QueryParser("id", new SimpleAnalyzer() );
- Query query;
- List result;
+ tx = s.beginTransaction();
+ FullTextSession session = Search.getFullTextSession( s );
+ QueryParser parser = new QueryParser( "id", new SimpleAnalyzer() );
+ Query query;
+ List result;
- query = parser.parse("customFieldBridge:This AND customStringBridge:This");
- result = session.createFullTextQuery(query).list();
- assertEquals( "Properties not mapped", 1, result.size() );
+ query = parser.parse( "customFieldBridge:This AND customStringBridge:This" );
+ result = session.createFullTextQuery( query ).list();
+ assertEquals( "Properties not mapped", 1, result.size() );
- query = parser.parse("customFieldBridge:by AND customStringBridge:is");
- result = session.createFullTextQuery(query).list();
- assertEquals( "Custom types not taken into account", 0, result.size() );
+ query = parser.parse( "customFieldBridge:by AND customStringBridge:is" );
+ result = session.createFullTextQuery( query ).list();
+ assertEquals( "Custom types not taken into account", 0, result.size() );
- s.delete( s.get( Cloud.class, cloud.getId() ) );
- tx.commit();
- s.close();
+ s.delete( s.get( Cloud.class, cloud.getId() ) );
+ tx.commit();
+ s.close();
- }
+ }
- public void testDateBridge() throws Exception {
- Cloud cloud = new Cloud();
- Calendar c = GregorianCalendar.getInstance();
- c.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); //for the sake of tests
- c.set(2000, 11, 15, 3, 43, 2);
- c.set( Calendar.MILLISECOND, 5 );
+ public void testDateBridge() throws Exception {
+ Cloud cloud = new Cloud();
+ Calendar c = GregorianCalendar.getInstance();
+ c.setTimeZone( TimeZone.getTimeZone( "GMT" ) ); //for the sake of tests
+ c.set( 2000, 11, 15, 3, 43, 2 );
+ c.set( Calendar.MILLISECOND, 5 );
- Date date = new Date( c.getTimeInMillis() );
- cloud.setMyDate( date ); //5 millisecond
- cloud.setDateDay( date );
- cloud.setDateHour( date );
- cloud.setDateMillisecond( date );
- cloud.setDateMinute( date );
- cloud.setDateMonth( date );
- cloud.setDateSecond( date );
- cloud.setDateYear( date );
- org.hibernate.Session s = openSession();
- Transaction tx = s.beginTransaction();
- s.persist(cloud);
- s.flush();
- tx.commit();
+ Date date = new Date( c.getTimeInMillis() );
+ cloud.setMyDate( date ); //5 millisecond
+ cloud.setDateDay( date );
+ cloud.setDateHour( date );
+ cloud.setDateMillisecond( date );
+ cloud.setDateMinute( date );
+ cloud.setDateMonth( date );
+ cloud.setDateSecond( date );
+ cloud.setDateYear( date );
+ org.hibernate.Session s = openSession();
+ Transaction tx = s.beginTransaction();
+ s.persist( cloud );
+ s.flush();
+ tx.commit();
- tx = s.beginTransaction();
- FullTextSession session = Search.getFullTextSession(s);
- QueryParser parser = new QueryParser("id", new StandardAnalyzer() );
- Query query;
- List result;
+ tx = s.beginTransaction();
+ FullTextSession session = Search.getFullTextSession( s );
+ QueryParser parser = new QueryParser( "id", new StandardAnalyzer() );
+ Query query;
+ List result;
- query = parser.parse("myDate:[19900101 TO 20060101]"
- + " AND dateDay:[20001214 TO 2000121501]"
- + " AND dateMonth:[200012 TO 20001201]"
- + " AND dateYear:[2000 TO 200001]"
- + " AND dateHour:[20001214 TO 2000121503]"
- + " AND dateMinute:[20001214 TO 200012150343]"
- + " AND dateSecond:[20001214 TO 20001215034302]"
- + " AND dateMillisecond:[20001214 TO 20001215034302005]"
- );
- result = session.createFullTextQuery(query).list();
- assertEquals( "Date not found or not property truncated", 1, result.size() );
+ query = parser.parse(
+ "myDate:[19900101 TO 20060101]"
+ + " AND dateDay:[20001214 TO 2000121501]"
+ + " AND dateMonth:[200012 TO 20001201]"
+ + " AND dateYear:[2000 TO 200001]"
+ + " AND dateHour:[20001214 TO 2000121503]"
+ + " AND dateMinute:[20001214 TO 200012150343]"
+ + " AND dateSecond:[20001214 TO 20001215034302]"
+ + " AND dateMillisecond:[20001214 TO 20001215034302005]"
+ );
+ result = session.createFullTextQuery( query ).list();
+ assertEquals( "Date not found or not property truncated", 1, result.size() );
- s.delete( s.get( Cloud.class, cloud.getId() ) );
- tx.commit();
- s.close();
+ s.delete( s.get( Cloud.class, cloud.getId() ) );
+ tx.commit();
+ s.close();
- }
- protected Class[] getMappings() {
- return new Class[] {
- Cloud.class
- };
- }
+ }
+ protected Class[] getMappings() {
+ return new Class[] {
+ Cloud.class
+ };
+ }
- protected void configure(Configuration cfg) {
- super.configure( cfg );
- cfg.setProperty( Environment.ANALYZER_CLASS, SimpleAnalyzer.class.getName() );
- }
+
+ protected void configure(Configuration cfg) {
+ super.configure( cfg );
+ cfg.setProperty( Environment.ANALYZER_CLASS, SimpleAnalyzer.class.getName() );
+ }
}
Modified: search/trunk/src/test/java/org/hibernate/search/test/bridge/Cloud.java
===================================================================
--- search/trunk/src/test/java/org/hibernate/search/test/bridge/Cloud.java 2009-07-25 05:24:55 UTC (rev 17201)
+++ search/trunk/src/test/java/org/hibernate/search/test/bridge/Cloud.java 2009-07-27 10:21:41 UTC (rev 17202)
@@ -44,6 +44,8 @@
private Date dateMillisecond;
private String customFieldBridge;
private String customStringBridge;
+ private Character char1;
+ private char char2;
private CloudType type;
private boolean storm;
private Class clazz;
@@ -285,4 +287,22 @@
public void setStorm(boolean storm) {
this.storm = storm;
}
+
+ @Field(index = Index.TOKENIZED, store = Store.YES)
+ public Character getChar1() {
+ return char1;
+ }
+
+ public void setChar1(Character char1) {
+ this.char1 = char1;
+ }
+
+ @Field(index = Index.TOKENIZED, store = Store.YES)
+ public char getChar2() {
+ return char2;
+ }
+
+ public void setChar2(char char2) {
+ this.char2 = char2;
+ }
}
More information about the hibernate-commits
mailing list