Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

HSEARCH-1256 Improve error handling and error message in ConnectedMul…

…tiFieldsTermQueryBuilder#buildSearchTerm
  • Loading branch information...
commit 4490defc6758350a4ecc4c8507feb1a4b6152fea 1 parent 093ea34
@Sanne Sanne authored
View
5 ...ch-engine/src/main/java/org/hibernate/search/engine/spi/DocumentBuilderIndexedEntity.java
@@ -752,10 +752,7 @@ public String objectToString(String fieldName, Object value, ConversionContext c
else if ( StringBridge.class.isAssignableFrom( bridgeClass ) ) {
return objectToString( (StringBridge) bridge, fieldName, value, conversionContext );
}
- throw new SearchException(
- "FieldBridge " + bridgeClass + " does not have a objectToString method: field "
- + fieldName + " in " + getBeanXClass()
- );
+ throw log.fieldBridgeNotTwoWay( bridgeClass, fieldName, getBeanXClass() );
}
}
throw new SearchException( "Unable to find field " + fieldName + " in " + getBeanXClass() );
View
6 hibernate-search-engine/src/main/java/org/hibernate/search/util/logging/impl/Log.java
@@ -35,9 +35,11 @@
import org.apache.lucene.index.CorruptIndexException;
import org.hibernate.annotations.common.AssertionFailure;
+import org.hibernate.annotations.common.reflection.XClass;
import org.hibernate.search.SearchException;
import org.hibernate.search.backend.impl.jgroups.JGroupsChannelProvider;
import org.hibernate.search.backend.spi.WorkType;
+import org.hibernate.search.bridge.FieldBridge;
import org.hibernate.search.errors.EmptyQueryException;
import org.hibernate.search.indexes.serialization.spi.LuceneWorkSerializer;
import org.jboss.logging.BasicLogger;
@@ -669,4 +671,8 @@
@Message(id = 168, value = "Serialization service %2$s being used for index '%1$s'")
void indexManagerUsesSerializationService(String indexName, String serializerDescription);
+ @Message(id = 169, value = "FieldBridge '%1$s' does not have a objectToString method: field '%2$s' in '%3$s'" +
+ " The FieldBridge must be a TwoWayFieldBridge or you have to enable the ignoreFieldBridge option when defining a Query" )
+ SearchException fieldBridgeNotTwoWay(Class<? extends FieldBridge> bridgeClass, String fieldName, XClass beanXClass);
+
}
View
60 hibernate-search-orm/src/test/java/org/hibernate/search/test/bridge/tika/TikaBridgeTest.java
@@ -50,11 +50,15 @@
import org.hibernate.search.bridge.BridgeException;
import org.hibernate.search.bridge.LuceneOptions;
import org.hibernate.search.bridge.TikaMetadataProcessor;
+import org.hibernate.search.query.dsl.QueryBuilder;
import org.hibernate.search.test.SearchTestCase;
import org.hibernate.search.test.TestConstants;
+import org.hibernate.search.test.util.TestForIssue;
+import org.junit.Assert;
/**
* @author Hardy Ferentschik
+ * @author Sanne Grinovero
*/
public class TikaBridgeTest extends SearchTestCase {
private static final String TEST_MP3_DOCUMENT = "/org/hibernate/search/test/bridge/tika/mysong.mp3";
@@ -79,6 +83,24 @@ public void testIndexMp3MetaTags() throws Exception {
session.close();
}
+ public void testIndexMp3MetaTagsDSL() throws Exception {
+ Session session = openSession();
+
+ persistSong( session );
+ searchSongDsl( session );
+
+ session.close();
+ }
+
+ public void testIndexMp3MetaTagsDSLErrorMessage() throws Exception {
+ Session session = openSession();
+
+ persistSong( session );
+ searchSongDsl( session );
+
+ session.close();
+ }
+
public void testUnsupportedTypeForTikaBridge() throws Exception {
Session session = openSession();
@@ -137,6 +159,44 @@ private void searchSong(Session session) throws Exception {
tx.commit();
}
+ private void searchSongDsl(Session session) throws Exception {
+ FullTextSession fullTextSession = Search.getFullTextSession( session );
+ Transaction tx = session.beginTransaction();
+
+ QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( Song.class ).get();
+
+ Query queryEmmanuel = queryBuilder.keyword().onField( "mp3FileName" ).ignoreFieldBridge().matching( "Emmanuel" ).createQuery();
+
+ List<Song> result = fullTextSession.createFullTextQuery( queryEmmanuel ).list();
+ assertEquals( "Emmanuel is not an artist", 0, result.size() );
+
+ Query queryHardy = queryBuilder.keyword().onField( "mp3FileName" ).ignoreFieldBridge().matching( "Hardy" ).createQuery();
+
+ result = fullTextSession.createFullTextQuery( queryHardy ).list();
+ assertEquals( "Hardy is the artist", 1, result.size() );
+
+ tx.commit();
+ }
+
+ @TestForIssue(jiraKey="HSEARCH-1256")
+ private void searchSongDslErrorMessage(Session session) throws Exception {
+ FullTextSession fullTextSession = Search.getFullTextSession( session );
+ Transaction tx = session.beginTransaction();
+
+ QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( Song.class ).get();
+
+ boolean exceptionCaught = false;
+ try {
+ queryBuilder.keyword().onField( "mp3FileName" ).ignoreFieldBridge().matching( "Emmanuel" ).createQuery();
+ } catch (Exception e) {
+ exceptionCaught = true;
+ Assert.assertTrue( e instanceof SearchException );
+ Assert.assertTrue( e.getMessage().contains( "The FieldBridge must be a TwoWayFieldBridge or you have to enable the ignoreFieldBridge option when defining a Query" ) );
+ }
+ Assert.assertTrue( exceptionCaught );
+ tx.commit();
+ }
+
@Entity
@Indexed
public static class Foo {
Please sign in to comment.
Something went wrong with that request. Please try again.