Micha Kops avatar Micha Kops committed 84e554b

Initial import

Comments (0)

Files changed (7)

+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>hibernate-facet-search</name>
+	<comment></comment>
+	<projects>
+	</projects>
+	<buildSpec>
+		<buildCommand>
+			<name>org.eclipse.jdt.core.javabuilder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+		<buildCommand>
+			<name>org.eclipse.m2e.core.maven2Builder</name>
+			<arguments>
+			</arguments>
+		</buildCommand>
+	</buildSpec>
+	<natures>
+		<nature>org.eclipse.jdt.core.javanature</nature>
+		<nature>org.eclipse.m2e.core.maven2Nature</nature>
+	</natures>
+</projectDescription>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+	<groupId>com.hascode.tutorial</groupId>
+	<artifactId>hibernate-facet-search</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<properties>
+		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
+	</properties>
+	<dependencies>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-search</artifactId>
+			<version>4.0.0.Final</version>
+		</dependency>
+		<dependency>
+			<groupId>org.hibernate</groupId>
+			<artifactId>hibernate-entitymanager</artifactId>
+			<version>4.0.1.Final</version>
+		</dependency>
+		<dependency>
+			<groupId>org.hsqldb</groupId>
+			<artifactId>hsqldb</artifactId>
+			<version>2.2.8</version>
+		</dependency>
+	</dependencies>
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>1.6</source>
+					<target>1.6</target>
+				</configuration>
+			</plugin>
+		</plugins>
+	</build>
+	<repositories>
+		<repository>
+			<id>maven2-repository.dev.java.net</id>
+			<name>Java.net Repository for Maven</name>
+			<url>http://download.java.net/maven/2/</url>
+			<layout>default</layout>
+		</repository>
+	</repositories>
+</project>

src/main/java/com/hascode/tutorial/Book.java

+package com.hascode.tutorial;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+import org.hibernate.search.annotations.Analyze;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.Store;
+
+@Entity
+@Indexed(index = "index/books")
+public class Book {
+	private Long id;
+	private String title;
+	private String author;
+	private String category;
+	private int price;
+
+	@Id
+	@GeneratedValue
+	public Long getId() {
+		return id;
+	}
+
+	@Field(name = "title", analyze = Analyze.YES, store = Store.YES)
+	public String getTitle() {
+		return title;
+	}
+
+	public void setTitle(final String title) {
+		this.title = title;
+	}
+
+	public void setId(final Long id) {
+		this.id = id;
+	}
+
+	public void setAuthor(final String author) {
+		this.author = author;
+	}
+
+	@Field(analyze = Analyze.NO, store = Store.YES)
+	public String getAuthor() {
+		return author;
+	}
+
+	@Field(analyze = Analyze.NO, store = Store.YES)
+	public String getCategory() {
+		return category;
+	}
+
+	public void setCategory(final String category) {
+		this.category = category;
+	}
+
+	@Field(analyze = Analyze.NO, store = Store.YES)
+	public int getPrice() {
+		return price;
+	}
+
+	public void setPrice(final int price) {
+		this.price = price;
+	}
+
+}

src/main/java/com/hascode/tutorial/BookSetup.java

+package com.hascode.tutorial;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityTransaction;
+
+public class BookSetup {
+	public static void createBooks(final EntityManager em,
+			final EntityTransaction tx) {
+		tx.begin();
+		Book book1 = new Book();
+		book1.setTitle("The big book of nothing");
+		book1.setCategory("Adventure");
+		book1.setAuthor("fred");
+		book1.setPrice(12);
+
+		Book book2 = new Book();
+		book2.setTitle("Exciting stories I");
+		book2.setCategory("Horror");
+		book2.setAuthor("selma");
+		book2.setPrice(22);
+
+		Book book3 = new Book();
+		book3.setTitle("My life");
+		book3.setCategory("Horror");
+		book3.setAuthor("fred");
+		book3.setPrice(10);
+
+		Book book4 = new Book();
+		book4.setTitle("Some science book");
+		book4.setCategory("Science");
+		book4.setAuthor("tim");
+		book4.setPrice(35);
+
+		Book book5 = new Book();
+		book5.setTitle("The universe and stuff");
+		book5.setCategory("Science");
+		book5.setAuthor("charlize");
+		book5.setPrice(65);
+
+		Book book6 = new Book();
+		book6.setTitle("Indiana Bones XII");
+		book6.setCategory("Adventure");
+		book6.setAuthor("charles");
+		book6.setPrice(14);
+
+		Book book7 = new Book();
+		book7.setTitle("A day at work without coffee");
+		book7.setCategory("Horror");
+		book7.setAuthor("me");
+		book7.setPrice(25);
+
+		Book book8 = new Book();
+		book8.setTitle("Horror Pirate Cyber Ninjas from Hell III");
+		book8.setCategory("Cartoon");
+		book8.setAuthor("peter");
+		book8.setPrice(51);
+
+		em.persist(book1);
+		em.persist(book2);
+		em.persist(book3);
+		em.persist(book4);
+		em.persist(book5);
+		em.persist(book6);
+		em.persist(book7);
+		em.persist(book8);
+		tx.commit();
+	}
+}

src/main/java/com/hascode/tutorial/DiscreteFacetingSearch.java

+package com.hascode.tutorial;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+import org.apache.lucene.search.Query;
+import org.hibernate.search.jpa.FullTextEntityManager;
+import org.hibernate.search.query.dsl.QueryBuilder;
+import org.hibernate.search.query.engine.spi.FacetManager;
+import org.hibernate.search.query.facet.Facet;
+import org.hibernate.search.query.facet.FacetSortOrder;
+import org.hibernate.search.query.facet.FacetingRequest;
+
+public class DiscreteFacetingSearch {
+	public static void main(final String[] args) {
+		final EntityManagerFactory emf = Persistence
+				.createEntityManagerFactory("hascode-local");
+		final EntityManager em = emf.createEntityManager();
+		final EntityTransaction tx = em.getTransaction();
+
+		BookSetup.createBooks(em, tx);
+
+		FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search
+				.getFullTextEntityManager(em);
+		QueryBuilder builder = fullTextEntityManager.getSearchFactory()
+				.buildQueryBuilder().forEntity(Book.class).get();
+
+		FacetingRequest categoryFacetingRequest = builder.facet()
+				.name("categoryFaceting").onField("category").discrete()
+				.orderedBy(FacetSortOrder.COUNT_DESC).includeZeroCounts(false)
+				.createFacetingRequest();
+
+		Query luceneQuery = builder.all().createQuery();
+		org.hibernate.search.jpa.FullTextQuery fullTextQuery = fullTextEntityManager
+				.createFullTextQuery(luceneQuery);
+		FacetManager facetManager = fullTextQuery.getFacetManager();
+		facetManager.enableFaceting(categoryFacetingRequest);
+
+		List<Facet> facets = facetManager.getFacets("categoryFaceting");
+		for (Facet f : facets) {
+			System.out.println(f.getValue() + " (" + f.getCount() + ")");
+			List<Book> books = fullTextEntityManager.createFullTextQuery(
+					f.getFacetQuery()).getResultList();
+			for (final Book b : books) {
+				System.out.println("\t" + b.getTitle() + " (" + b.getAuthor()
+						+ ")");
+			}
+		}
+		em.close();
+		emf.close();
+	}
+}

src/main/java/com/hascode/tutorial/RangeFacetingSearch.java

+package com.hascode.tutorial;
+
+import java.util.List;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.EntityTransaction;
+import javax.persistence.Persistence;
+
+import org.apache.lucene.search.Query;
+import org.hibernate.search.jpa.FullTextEntityManager;
+import org.hibernate.search.query.dsl.QueryBuilder;
+import org.hibernate.search.query.engine.spi.FacetManager;
+import org.hibernate.search.query.facet.Facet;
+import org.hibernate.search.query.facet.FacetSortOrder;
+import org.hibernate.search.query.facet.FacetingRequest;
+
+public class RangeFacetingSearch {
+	public static void main(final String[] args) {
+		final EntityManagerFactory emf = Persistence
+				.createEntityManagerFactory("hascode-local");
+		final EntityManager em = emf.createEntityManager();
+		final EntityTransaction tx = em.getTransaction();
+
+		BookSetup.createBooks(em, tx);
+
+		FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search
+				.getFullTextEntityManager(em);
+		QueryBuilder builder = fullTextEntityManager.getSearchFactory()
+				.buildQueryBuilder().forEntity(Book.class).get();
+
+		FacetingRequest priceFacetingRequest = builder.facet()
+				.name("priceFaceting").onField("price").range().below(20)
+				.from(30).to(40).from(40).to(50).above(50).excludeLimit()
+				.orderedBy(FacetSortOrder.RANGE_DEFINITION_ODER)
+				.createFacetingRequest();
+		Query luceneQuery = builder.all().createQuery();
+		org.hibernate.search.jpa.FullTextQuery fullTextQuery = fullTextEntityManager
+				.createFullTextQuery(luceneQuery);
+		FacetManager facetManager = fullTextQuery.getFacetManager();
+		facetManager.enableFaceting(priceFacetingRequest);
+
+		List<Facet> facets = facetManager.getFacets("priceFaceting");
+		for (Facet f : facets) {
+			System.out.println("Price range: " + f.getValue() + " ("
+					+ f.getCount() + ")");
+		}
+		em.close();
+		emf.close();
+	}
+}

src/main/resources/META-INF/persistence.xml

+<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
+<persistence-unit name="hascode-local" transaction-type="RESOURCE_LOCAL">
+    <provider>org.hibernate.ejb.HibernatePersistence</provider>
+    <class>com.hascode.tutorial.Book</class>
+    <properties>
+        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver"/>
+        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:testdb"/>
+        <property name="javax.persistence.jdbc.user" value="sa"/>
+        <property name="javax.persistence.jdbc.password" value=""/>
+        <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
+        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
+    </properties>
+</persistence-unit>
+</persistence>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.