Created
April 13, 2011 14:13
-
-
Save hferentschik/917615 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.hibernate.search.test.query.facet; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.Id; | |
import javax.persistence.ManyToMany; | |
import javax.persistence.Table; | |
import org.apache.lucene.queryParser.MultiFieldQueryParser; | |
import org.apache.lucene.queryParser.QueryParser; | |
import org.apache.lucene.search.Query; | |
import org.hibernate.Session; | |
import org.hibernate.Transaction; | |
import org.hibernate.search.FullTextQuery; | |
import org.hibernate.search.annotations.Field; | |
import org.hibernate.search.annotations.Fields; | |
import org.hibernate.search.annotations.Index; | |
import org.hibernate.search.annotations.Indexed; | |
import org.hibernate.search.annotations.IndexedEmbedded; | |
import org.hibernate.search.annotations.Store; | |
import org.hibernate.search.query.dsl.QueryBuilder; | |
import org.hibernate.search.query.facet.Facet; | |
import org.hibernate.search.query.facet.FacetSortOrder; | |
import org.hibernate.search.query.facet.FacetingRequest; | |
import org.hibernate.search.test.SearchTestCase; | |
/** | |
* @author Hardy Ferentschik | |
*/ | |
public class EmbeddedFacetingTest extends AbstractFacetTest { | |
public void testSimpleFaceting() throws Exception { | |
QueryParser parser = new MultiFieldQueryParser( | |
getTargetLuceneVersion(), new String[] { "name" }, SearchTestCase.standardAnalyzer | |
); | |
Query query; | |
FullTextQuery ftq; | |
QueryBuilder builder; | |
List<Facet> facets; | |
query = parser.parse( "Book*" ); | |
ftq = fullTextSession.createFullTextQuery( query, Book.class ); | |
FacetingRequest facetReq; | |
builder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity( Book.class ).get(); | |
facetReq = builder.facet().name( "someFacet" ).onField( "authors.name_untokenized" ) | |
.discrete().orderedBy( FacetSortOrder.COUNT_DESC ) | |
.includeZeroCounts( false ).maxFacetCount( 10 ) | |
.createFacetingRequest(); | |
facets = ftq.getFacetManager().enableFaceting( facetReq ).getFacets( "someFacet" ); | |
assertEquals( "error in collecting facets on embedded collections, wrong number of facets", 3, facets.size() ); | |
for ( Facet f : facets ) { | |
if ( f.getValue().equals( "Voltaire" ) ) { | |
assertEquals( "error in collecting facets on embedded collections (Author a)", 1, f.getCount() ); | |
} | |
if ( f.getValue().equals( "Victor Hugo" ) ) { | |
assertEquals( "error in collecting facets on embedded collections (Author a2)", 3, f.getCount() ); | |
} | |
if ( f.getValue().equals( "Moliere" ) ) { | |
assertEquals( "error in collecting facets on embedded collections (Author a3)", 2, f.getCount() ); | |
} | |
} | |
} | |
public void loadTestData(Session session) { | |
Transaction tx = session.beginTransaction(); | |
Author a = new Author(); | |
a.setName( "Voltaire" ); | |
Author a2 = new Author(); | |
a2.setName( "Victor Hugo" ); | |
Author a3 = new Author(); | |
a3.setName( "Moliere" ); | |
Book b1 = new Book(); | |
b1.setName( "Book1" ); | |
b1.getAuthors().add( a ); | |
b1.getAuthors().add( a2 ); // be creative | |
Book b2 = new Book(); | |
b2.setName( "Book2" ); | |
b2.getAuthors().add( a2 ); | |
b2.getAuthors().add( a3 ); | |
Book b3 = new Book(); | |
b3.setName( "Book3" ); | |
b3.getAuthors().add( a2 ); | |
b3.getAuthors().add( a3 ); | |
session.persist( a ); | |
session.persist( a2 ); | |
session.persist( a3 ); | |
session.persist( b1 ); | |
session.persist( b2 ); | |
session.persist( b3 ); | |
tx.commit(); | |
session.clear(); | |
} | |
@Indexed | |
@Entity | |
@Table(name = "BOOK") | |
public class Book { | |
@Id | |
@GeneratedValue | |
private int id; | |
@Field | |
private String name; | |
@IndexedEmbedded | |
@ManyToMany | |
Set<Author> authors; | |
public Book() { | |
authors = new HashSet<Author>(); | |
} | |
public int getId() { | |
return id; | |
} | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
public Set<Author> getAuthors() { | |
return authors; | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder(); | |
sb.append( "Book" ); | |
sb.append( "{name='" ).append( name ).append( '\'' ); | |
sb.append( '}' ); | |
return sb.toString(); | |
} | |
} | |
@Entity | |
@Table(name = "AUTHOR") | |
public class Author { | |
@Id | |
@GeneratedValue | |
private int id; | |
@Fields( { @Field(index = Index.TOKENIZED, store = Store.YES), @Field(index = Index.UN_TOKENIZED, name = "name_untokenized", store = Store.YES) }) | |
private String name; | |
public String getName() { | |
return name; | |
} | |
public void setName(String name) { | |
this.name = name; | |
} | |
@Override | |
public String toString() { | |
final StringBuilder sb = new StringBuilder(); | |
sb.append( "Author" ); | |
sb.append( "{name='" ).append( name ).append( '\'' ); | |
sb.append( '}' ); | |
return sb.toString(); | |
} | |
} | |
protected Class<?>[] getAnnotatedClasses() { | |
return new Class[] { | |
Author.class, Book.class | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment