Skip to content

Instantly share code, notes, and snippets.

@hferentschik
Created September 12, 2011 14:24
Show Gist options
  • Save hferentschik/1211379 to your computer and use it in GitHub Desktop.
Save hferentschik/1211379 to your computer and use it in GitHub Desktop.
Hibernate Search store/omit norms test
public class StoreNormsTest extends SearchTestCase {
public void testStoreAndOmitNorms() throws Exception {
Session session = openSession();
FullTextSession fullTextSession = Search.getFullTextSession( session );
Transaction tx = fullTextSession.beginTransaction();
Test test = new Test();
test.setWithNormsImplicit( "hello" );
test.setWithNormsExplicit( "world" );
test.setWithoutNorms( "how are you?" );
fullTextSession.save( test );
tx.commit();
// querying works
// QueryParser parser = new QueryParser(
// getTargetLuceneVersion(),
// "withNormsImplicit",
// fullTextSession.getSearchFactory().getAnalyzer( Test.class )
// );
// org.apache.lucene.search.Query luceneQuery = parser.parse( "withNormsImplicit:hello" );
// FullTextQuery query = fullTextSession.createFullTextQuery( luceneQuery, Test.class );
// assertEquals( 1, query.getResultSize() );
// get a index reader to the underlying index to verify using the Lucene API
SearchFactoryImplementor searchFactory = getSearchFactoryImpl();
IndexManager indexManager = searchFactory.getAllIndexesManager().getIndexManager( "test" );
org.apache.lucene.index.IndexReader indexReader = indexManager.getIndexReaderManager().openIndexReader();
try {
Document document = indexReader.document( 0 );
Fieldable implicitNormField = document.getFieldable( "withNormsImplicit" );
assertTrue( "norms should be stored for this field", implicitNormField.getOmitNorms() );
}
finally {
indexManager.getIndexReaderManager().closeIndexReader( indexReader );
}
}
@Override
protected Class<?>[] getAnnotatedClasses() {
return new Class<?>[] { Test.class };
}
}
@Entity
@Indexed(index = "test")
public class Test {
@Id
@GeneratedValue
private int id;
@Field
private String withNormsImplicit;
@Field(norms = Norms.YES)
private String withNormsExplicit;
@Field(norms = Norms.NO)
private String withoutNorms;
public String getWithNormsImplicit() {
return withNormsImplicit;
}
public void setWithNormsImplicit(String withNormsImplicit) {
this.withNormsImplicit = withNormsImplicit;
}
public String getWithNormsExplicit() {
return withNormsExplicit;
}
public void setWithNormsExplicit(String withNormsExplicit) {
this.withNormsExplicit = withNormsExplicit;
}
public String getWithoutNorms() {
return withoutNorms;
}
public void setWithoutNorms(String withoutNorms) {
this.withoutNorms = withoutNorms;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment