Author: epbernard
Date: 2006-11-01 01:55:20 -0500 (Wed, 01 Nov 2006)
New Revision: 10674
Added:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/AlternateDocument.java
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/FSDirectoryTest.java
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/RamDirectoryTest.java
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/AlternateBook.java
Removed:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/MappingTest.java
Modified:
branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/event/Worker.java
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/Book.java
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/LuceneQueryTest.java
Log:
ANN-460 removal is not safe for multiple entities per index but querying is not yet
Modified:
branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/event/Worker.java
===================================================================
---
branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/event/Worker.java 2006-11-01
06:03:25 UTC (rev 10673)
+++
branches/Lucene_Integration/HibernateExt/metadata/src/java/org/hibernate/lucene/event/Worker.java 2006-11-01
06:55:20 UTC (rev 10674)
@@ -10,6 +10,7 @@
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermDocs;
import org.hibernate.AssertionFailure;
import org.hibernate.HibernateException;
import org.hibernate.lucene.DocumentBuilder;
@@ -76,16 +77,35 @@
}
private void remove(Class entity, Serializable id) {
- //FIXME lookup by term id and filter by test _hibernate_class
log.trace( "remove from Lucene index: " + entity + "#" + id );
DocumentBuilder builder = workspace.getDocumentBuilder( entity );
Term term = builder.getTerm( id );
IndexReader reader = workspace.getIndexReader( entity );
+ TermDocs termDocs = null;
try {
- reader.deleteDocuments( term );
+ //TODO is there a faster way?
+ //TODO include TermDocs into the workspace?
+ termDocs = reader.termDocs( term );
+ String entityName = entity.getName();
+ while ( termDocs.next() ) {
+ int docIndex = termDocs.doc();
+ if ( entityName.equals( reader.document( docIndex ).get(
DocumentBuilder.CLASS_FIELDNAME ) ) ) {
+ //remove only the one of the right class
+ //loop all to remove all the matches (defensive code)
+ reader.deleteDocument( docIndex );
+ }
+ }
}
catch (Exception e) {
throw new HibernateException( "Unable to remove from Lucene index: " +
entity + "#" + id, e );
}
+ finally {
+ if (termDocs != null) try {
+ termDocs.close();
+ }
+ catch (IOException e) {
+ log.warn( "Unable to close termDocs properly", e);
+ }
+ }
}
}
Added:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/AlternateDocument.java
===================================================================
---
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/AlternateDocument.java 2006-11-01
06:03:25 UTC (rev 10673)
+++
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/AlternateDocument.java 2006-11-01
06:55:20 UTC (rev 10674)
@@ -0,0 +1,82 @@
+//$Id: $
+package org.hibernate.lucene.test;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Lob;
+
+import org.hibernate.lucene.Indexed;
+import org.hibernate.lucene.DocumentId;
+import org.hibernate.lucene.Field;
+import org.hibernate.lucene.Store;
+import org.hibernate.lucene.Index;
+import org.hibernate.lucene.Boost;
+
+/**
+ * Example of 2 entities mapped in the same index
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Indexed(index = "Documents")
+public class AlternateDocument {
+ private Long id;
+ private String title;
+ private String summary;
+ private String text;
+
+ AlternateDocument() {
+ }
+
+ public AlternateDocument(Long id, String title, String summary, String text) {
+ super();
+ this.id = id;
+ this.summary = summary;
+ this.text = text;
+ this.title = title;
+ }
+
+ @Id
+ //@Keyword(id = true)
+ @DocumentId()
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ //@Text
+ @Field( name = "alt_title", store = Store.YES, index = Index.TOKENIZED )
+ @Boost(2)
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ //@Unstored(name = "Abstract")
+ @Field( name="Abstract", store = Store.NO, index = Index.TOKENIZED )
+ public String getSummary() {
+ return summary;
+ }
+
+ public void setSummary(String summary) {
+ this.summary = summary;
+ }
+
+ @Lob
+ //@Unstored
+ @Field( store = Store.NO, index = Index.TOKENIZED )
+ public String getText() {
+ return text;
+ }
+
+ public void setText(String text) {
+ this.text = text;
+ }
+}
+
Copied:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/FSDirectoryTest.java
(from rev 10671,
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/MappingTest.java)
===================================================================
---
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/MappingTest.java 2006-11-01
04:27:26 UTC (rev 10671)
+++
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/FSDirectoryTest.java 2006-11-01
06:55:20 UTC (rev 10674)
@@ -0,0 +1,193 @@
+//$Id: LuceneTest.java 10014 2006-06-12 09:56:27 -0700 (lun., 12 juin 2006) epbernard $
+package org.hibernate.lucene.test;
+
+import java.io.File;
+import java.util.List;
+
+import org.apache.lucene.analysis.StopAnalyzer;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.index.TermDocs;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.hibernate.Session;
+import org.hibernate.event.PostDeleteEventListener;
+import org.hibernate.event.PostInsertEventListener;
+import org.hibernate.event.PostUpdateEventListener;
+import org.hibernate.lucene.Environment;
+import org.hibernate.lucene.store.FSDirectoryProvider;
+import org.hibernate.lucene.event.LuceneEventListener;
+
+/**
+ * @author Gavin King
+ */
+public class FSDirectoryTest extends TestCase {
+
+
+ protected void setUp() throws Exception {
+ File sub = getBaseIndexDir();
+ sub.mkdir();
+ File[] files = sub.listFiles();
+ for (File file : files) {
+ if ( file.isDirectory() ) {
+ delete( file );
+ }
+ }
+ //super.setUp(); //we need a fresh session factory each time for index set up
+ buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
+ }
+
+ private File getBaseIndexDir() {
+ File current = new File( "." );
+ File sub = new File( current, "indextemp" );
+ return sub;
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ File sub = getBaseIndexDir();
+ delete( sub );
+ }
+
+ private void delete(File sub) {
+ if ( sub.isDirectory() ) {
+ for ( File file : sub.listFiles() ) {
+ delete( file );
+ }
+ sub.delete();
+ }
+ else {
+ sub.delete();
+ }
+ }
+
+ public void testEventIntegration() throws Exception {
+
+
+ Session s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.persist(
+ new Document( "Hibernate in Action", "Object/relational mapping with
Hibernate", "blah blah blah" )
+ );
+ s.getTransaction().commit();
+ s.close();
+ IndexReader reader = IndexReader.open( new File( getBaseIndexDir(),
"Documents" ) );
+ try {
+ int num = reader.numDocs();
+ assertEquals( 1, num );
+ TermDocs docs = reader.termDocs( new Term( "Abstract", "Hibernate"
) );
+ org.apache.lucene.document.Document doc = reader.document( docs.doc() );
+ assertFalse( docs.next() );
+ docs = reader.termDocs( new Term( "Title", "Action" ) );
+ doc = reader.document( docs.doc() );
+ assertFalse( docs.next() );
+ assertEquals( "1", doc.getField( "id" ).stringValue() );
+ }
+ finally {
+ reader.close();
+ }
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ Document entity = (Document) s.get( Document.class, new Long( 1 ) );
+ entity.setSummary( "Object/relational mapping with EJB3" );
+ s.persist( new Document( "Seam in Action", "", "blah blah blah
blah" ) );
+ s.getTransaction().commit();
+ s.close();
+
+ reader = IndexReader.open( new File( getBaseIndexDir(), "Documents" ) );
+ try {
+ int num = reader.numDocs();
+ assertEquals( 2, num );
+ TermDocs docs = reader.termDocs( new Term( "Abstract", "EJB3" )
);
+ org.apache.lucene.document.Document doc = reader.document( docs.doc() );
+ assertFalse( docs.next() );
+ }
+ finally {
+ reader.close();
+ }
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.delete( entity );
+ s.getTransaction().commit();
+ s.close();
+
+ reader = IndexReader.open( new File( getBaseIndexDir(), "Documents" ) );
+ try {
+ int num = reader.numDocs();
+ assertEquals( 1, num );
+ TermDocs docs = reader.termDocs( new Term( "Title", "Seam" ) );
+ org.apache.lucene.document.Document doc = reader.document( docs.doc() );
+ assertFalse( docs.next() );
+ assertEquals( "2", doc.getField( "id" ).stringValue() );
+ }
+ finally {
+ reader.close();
+ }
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.delete( s.createCriteria( Document.class ).uniqueResult() );
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ public void testBoost() throws Exception {
+ Session s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.persist(
+ new Document( "Hibernate in Action", "Object and Relational",
"blah blah blah" )
+ );
+ s.persist(
+ new Document( "Object and Relational", "Hibernate in Action",
"blah blah blah" )
+ );
+ s.getTransaction().commit();
+ s.close();
+
+ IndexSearcher searcher = new IndexSearcher( new File( getBaseIndexDir(),
"Documents" ).getCanonicalPath() );
+ try {
+ QueryParser qp = new QueryParser( "id", new StandardAnalyzer() );
+ Query query = qp.parse( "title:Action OR Abstract:Action" );
+ Hits hits = searcher.search( query );
+ assertEquals( 2, hits.length() );
+ assertTrue( hits.score( 0 ) == 2 * hits.score( 1 ) );
+ assertEquals( "Hibernate in Action", hits.doc( 0 ).get( "title" )
);
+ }
+ finally {
+ if ( searcher != null ) searcher.close();
+ }
+
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ List list = s.createQuery( "from Document" ).list();
+ for ( Document document : (List<Document>) list ) {
+ s.delete( document );
+ }
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ protected Class[] getMappings() {
+ return new Class[]{
+ Document.class
+ };
+ }
+
+ protected void configure(org.hibernate.cfg.Configuration cfg) {
+ File sub = getBaseIndexDir();
+ cfg.setProperty( "hibernate.lucene.default.indexBase", sub.getAbsolutePath()
);
+ cfg.setProperty( "hibernate.lucene.Clock.directory_provider",
FSDirectoryProvider.class.getName() );
+ cfg.setProperty( Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
+ LuceneEventListener del = new LuceneEventListener();
+ cfg.getEventListeners().setPostDeleteEventListeners( new PostDeleteEventListener[]{del}
);
+ cfg.getEventListeners().setPostUpdateEventListeners( new PostUpdateEventListener[]{del}
);
+ cfg.getEventListeners().setPostInsertEventListeners( new PostInsertEventListener[]{del}
);
+ }
+
+}
+
Deleted:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/MappingTest.java
===================================================================
---
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/MappingTest.java 2006-11-01
06:03:25 UTC (rev 10673)
+++
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/MappingTest.java 2006-11-01
06:55:20 UTC (rev 10674)
@@ -1,191 +0,0 @@
-//$Id: LuceneTest.java 10014 2006-06-12 09:56:27 -0700 (lun., 12 juin 2006) epbernard $
-package org.hibernate.lucene.test;
-
-import java.io.File;
-import java.util.List;
-
-import org.apache.lucene.analysis.StopAnalyzer;
-import org.apache.lucene.analysis.standard.StandardAnalyzer;
-import org.apache.lucene.index.IndexReader;
-import org.apache.lucene.index.Term;
-import org.apache.lucene.index.TermDocs;
-import org.apache.lucene.queryParser.QueryParser;
-import org.apache.lucene.search.Hits;
-import org.apache.lucene.search.IndexSearcher;
-import org.apache.lucene.search.Query;
-import org.hibernate.Session;
-import org.hibernate.event.PostDeleteEventListener;
-import org.hibernate.event.PostInsertEventListener;
-import org.hibernate.event.PostUpdateEventListener;
-import org.hibernate.lucene.Environment;
-import org.hibernate.lucene.store.FSDirectoryProvider;
-import org.hibernate.lucene.event.LuceneEventListener;
-
-/**
- * @author Gavin King
- */
-public class MappingTest extends TestCase {
-
-
- protected void setUp() throws Exception {
- File sub = getBaseIndexDir();
- sub.mkdir();
- File[] files = sub.listFiles();
- for (File file : files) {
- if ( file.isDirectory() ) {
- delete( file );
- }
- }
- //super.setUp(); //we need a fresh session factory each time for index set up
- buildSessionFactory( getMappings(), getAnnotatedPackages(), getXmlFiles() );
- }
-
- private File getBaseIndexDir() {
- File current = new File( "." );
- File sub = new File( current, "indextemp" );
- return sub;
- }
-
- protected void tearDown() throws Exception {
- super.tearDown();
- File sub = getBaseIndexDir();
- delete( sub );
- }
-
- private void delete(File sub) {
- if ( sub.isDirectory() ) {
- for ( File file : sub.listFiles() ) {
- delete( file );
- }
- sub.delete();
- }
- else {
- sub.delete();
- }
- }
-
- public void testEventIntegration() throws Exception {
-
-
- Session s = getSessions().openSession();
- s.getTransaction().begin();
- s.persist(
- new Document( "Hibernate in Action", "Object/relational mapping with
Hibernate", "blah blah blah" )
- );
- s.getTransaction().commit();
- s.close();
- IndexReader reader = IndexReader.open( new File( getBaseIndexDir(),
"Documents" ) );
- try {
- int num = reader.numDocs();
- assertEquals( 1, num );
- TermDocs docs = reader.termDocs( new Term( "Abstract", "Hibernate"
) );
- org.apache.lucene.document.Document doc = reader.document( docs.doc() );
- assertFalse( docs.next() );
- docs = reader.termDocs( new Term( "Title", "Action" ) );
- doc = reader.document( docs.doc() );
- assertFalse( docs.next() );
- assertEquals( "1", doc.getField( "id" ).stringValue() );
- }
- finally {
- reader.close();
- }
-
- s = getSessions().openSession();
- s.getTransaction().begin();
- Document entity = (Document) s.get( Document.class, new Long( 1 ) );
- entity.setSummary( "Object/relational mapping with EJB3" );
- s.persist( new Document( "Seam in Action", "", "blah blah blah
blah" ) );
- s.getTransaction().commit();
- s.close();
-
- reader = IndexReader.open( new File( getBaseIndexDir(), "Documents" ) );
- try {
- int num = reader.numDocs();
- assertEquals( 2, num );
- TermDocs docs = reader.termDocs( new Term( "Abstract", "EJB3" )
);
- org.apache.lucene.document.Document doc = reader.document( docs.doc() );
- assertFalse( docs.next() );
- }
- finally {
- reader.close();
- }
-
- s = getSessions().openSession();
- s.getTransaction().begin();
- s.delete( entity );
- s.getTransaction().commit();
- s.close();
-
- reader = IndexReader.open( new File( getBaseIndexDir(), "Documents" ) );
- try {
- int num = reader.numDocs();
- assertEquals( 1, num );
- TermDocs docs = reader.termDocs( new Term( "Title", "Seam" ) );
- org.apache.lucene.document.Document doc = reader.document( docs.doc() );
- assertFalse( docs.next() );
- assertEquals( "2", doc.getField( "id" ).stringValue() );
- }
- finally {
- reader.close();
- }
-
- s = getSessions().openSession();
- s.getTransaction().begin();
- s.delete( s.createCriteria( Document.class ).uniqueResult() );
- s.getTransaction().commit();
- s.close();
- }
-
- public void testBoost() throws Exception {
- Session s = getSessions().openSession();
- s.getTransaction().begin();
- s.persist(
- new Document( "Hibernate in Action", "Object and Relational",
"blah blah blah" )
- );
- s.persist(
- new Document( "Object and Relational", "Hibernate in Action",
"blah blah blah" )
- );
- s.getTransaction().commit();
- s.close();
-
- IndexSearcher searcher = new IndexSearcher( new File( getBaseIndexDir(),
"Documents" ).getCanonicalPath() );
- try {
- QueryParser qp = new QueryParser( "id", new StandardAnalyzer() );
- Query query = qp.parse( "title:Action OR Abstract:Action" );
- Hits hits = searcher.search( query );
- assertEquals( 2, hits.length() );
- assertTrue( hits.score( 0 ) == 2 * hits.score( 1 ) );
- assertEquals( "Hibernate in Action", hits.doc( 0 ).get( "title" )
);
- }
- finally {
- if ( searcher != null ) searcher.close();
- }
-
-
- s = getSessions().openSession();
- s.getTransaction().begin();
- List list = s.createQuery( "from Document" ).list();
- for ( Document document : (List<Document>) list ) {
- s.delete( document );
- }
- s.getTransaction().commit();
- s.close();
- }
-
- protected Class[] getMappings() {
- return new Class[]{Document.class};
- }
-
- protected void configure(org.hibernate.cfg.Configuration cfg) {
- File sub = getBaseIndexDir();
- cfg.setProperty( "hibernate.lucene.default.indexBase", sub.getAbsolutePath()
);
- cfg.setProperty( "hibernate.lucene.Clock.directory_provider",
FSDirectoryProvider.class.getName() );
- cfg.setProperty( Environment.ANALYZER_CLASS, StopAnalyzer.class.getName() );
- LuceneEventListener del = new LuceneEventListener();
- cfg.getEventListeners().setPostDeleteEventListeners( new PostDeleteEventListener[]{del}
);
- cfg.getEventListeners().setPostUpdateEventListeners( new PostUpdateEventListener[]{del}
);
- cfg.getEventListeners().setPostInsertEventListeners( new PostInsertEventListener[]{del}
);
- }
-
-}
-
Added:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/RamDirectoryTest.java
===================================================================
---
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/RamDirectoryTest.java 2006-11-01
06:03:25 UTC (rev 10673)
+++
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/RamDirectoryTest.java 2006-11-01
06:55:20 UTC (rev 10674)
@@ -0,0 +1,71 @@
+//$Id: $
+package org.hibernate.lucene.test;
+
+import java.io.File;
+import java.util.List;
+
+import org.hibernate.Session;
+import org.apache.lucene.index.IndexReader;
+import org.apache.lucene.index.TermDocs;
+import org.apache.lucene.index.Term;
+import org.apache.lucene.search.IndexSearcher;
+import org.apache.lucene.search.Query;
+import org.apache.lucene.search.Hits;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.analysis.standard.StandardAnalyzer;
+
+/**
+ * @author Emmanuel Bernard
+ */
+public class RamDirectoryTest extends TestCase {
+
+ public void testMultipleEntitiesPerIndex() throws Exception {
+
+
+ Session s = getSessions().openSession();
+ s.getTransaction().begin();
+ Document document =
+ new Document( "Hibernate in Action", "Object/relational mapping with
Hibernate", "blah blah blah" );
+ s.persist(document);
+ s.flush();
+ s.persist(
+ new AlternateDocument( document.getId(), "Hibernate in Action",
"Object/relational mapping with Hibernate", "blah blah blah" )
+ );
+ s.getTransaction().commit();
+ s.close();
+
+ assertEquals( 2, getDocumentNbr() );
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.delete( s.get( AlternateDocument.class, document.getId() ) );
+ s.getTransaction().commit();
+ s.close();
+
+ assertEquals( 1, getDocumentNbr() );
+
+ s = getSessions().openSession();
+ s.getTransaction().begin();
+ s.delete( s.createCriteria( Document.class ).uniqueResult() );
+ s.getTransaction().commit();
+ s.close();
+ }
+
+ private int getDocumentNbr() throws Exception {
+ IndexReader reader = IndexReader.open( getDirectory( Document.class ) );
+ try {
+ return reader.numDocs();
+ }
+ finally {
+ reader.close();
+ }
+ }
+
+ protected Class[] getMappings() {
+ return new Class[]{
+ Document.class,
+ AlternateDocument.class
+ };
+ }
+
+}
Added:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/AlternateBook.java
===================================================================
---
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/AlternateBook.java 2006-11-01
06:03:25 UTC (rev 10673)
+++
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/AlternateBook.java 2006-11-01
06:55:20 UTC (rev 10674)
@@ -0,0 +1,47 @@
+//$Id: $
+package org.hibernate.lucene.test.query;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+
+import org.hibernate.lucene.Indexed;
+import org.hibernate.lucene.DocumentId;
+import org.hibernate.lucene.Field;
+import org.hibernate.lucene.Index;
+
+/**
+ * @author Emmanuel Bernard
+ */
+@Entity
+@Indexed(index="Book")
+public class AlternateBook {
+ @Id @DocumentId
+ private Integer id;
+ @Field(index = Index.TOKENIZED)
+ private String summary;
+
+
+ public AlternateBook() {
+ }
+
+ public AlternateBook(Integer id, String summary) {
+ this.id = id;
+ this.summary = summary;
+ }
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ public String getSummary() {
+ return summary;
+ }
+
+ public void setSummary(String summary) {
+ this.summary = summary;
+ }
+}
Modified:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/Book.java
===================================================================
---
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/Book.java 2006-11-01
06:03:25 UTC (rev 10673)
+++
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/Book.java 2006-11-01
06:55:20 UTC (rev 10674)
@@ -13,7 +13,7 @@
* @author Emmanuel Bernard
*/
@Entity
-@Indexed
+@Indexed(index = "Book" )
public class Book {
private Integer id;
Modified:
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/LuceneQueryTest.java
===================================================================
---
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/LuceneQueryTest.java 2006-11-01
06:03:25 UTC (rev 10673)
+++
branches/Lucene_Integration/HibernateExt/metadata/src/test/org/hibernate/lucene/test/query/LuceneQueryTest.java 2006-11-01
06:55:20 UTC (rev 10674)
@@ -30,7 +30,7 @@
s.save(book);
book = new Book(2, "La gloire de mon p�re", "Les deboires de mon p�re en
v�lo");
s.save(book);
- tx.commit();//post commit events for lucene
+ tx.commit();
s.clear();
tx = s.beginTransaction();
QueryParser parser = new QueryParser("title", new StopAnalyzer() );
@@ -56,6 +56,7 @@
assertTrue( Hibernate.isInitialized( element ) );
s.delete( element );
}
+ for (Object element : s.createQuery( "from Book" ).list() ) s.delete( element
);
tx.commit();
s.close();
}
@@ -92,6 +93,7 @@
s.delete( result.next() );
}
assertEquals( 2, index );
+ for (Object element : s.createQuery( "from Book" ).list() ) s.delete( element
);
tx.commit();
s.close();
}
@@ -134,14 +136,48 @@
while ( result.next() ) {
s.delete( result.get()[0] );
}
+ for (Object element : s.createQuery( "from Book" ).list() ) s.delete( element
);
tx.commit();
s.close();
}
+ public void testMultipleEntityPerIndex() throws Exception {
+ LuceneSession s = new LuceneSession( openSession() );
+ Transaction tx = s.beginTransaction();
+ Book book = new Book(1, "La chute de la petite reine a travers les yeux de
Festina", "La chute de la petite reine a travers les yeux de Festina,
blahblah");
+ s.save(book);
+ AlternateBook alternateBook = new AlternateBook(1, "La chute de la petite reine a
travers les yeux de Festina");
+ s.save(alternateBook);
+ tx.commit();
+ s.clear();
+ tx = s.beginTransaction();
+ QueryParser parser = new QueryParser("title", new StopAnalyzer() );
+ Query query = parser.parse( "summary:Festina" );
+ org.hibernate.Query hibQuery = s.createLuceneQuery( query, Clock.class, Book.class );
+ List result = hibQuery.list();
+ assertNotNull( result );
+ assertEquals( "Query with explicit class filter", 1, result.size() );
+
+ query = parser.parse( "summary:Festina" );
+ hibQuery = s.createLuceneQuery( query );
+ result = hibQuery.list();
+ assertNotNull( result );
+ assertEquals( "Query with no class filter", 2, result.size() );
+ for (Object element : result) {
+ assertTrue( Hibernate.isInitialized( element ) );
+ s.delete( element );
+ }
+ for (Object element : s.createQuery( "from java.lang.Object" ).list() )
s.delete( element );
+ tx.commit();
+ s.close();
+ }
+
+
protected Class[] getMappings() {
return new Class[] {
Book.class,
+ AlternateBook.class,
Clock.class
};
}