Author: hardy.ferentschik
Date: 2010-05-03 08:10:11 -0400 (Mon, 03 May 2010)
New Revision: 19347
Added:
search/trunk/hibernate-search-archetype/archetype.properties
search/trunk/hibernate-search-archetype/src/main/java/com/
search/trunk/hibernate-search-archetype/src/main/java/com/example/
search/trunk/hibernate-search-archetype/src/main/java/com/example/Author.java
search/trunk/hibernate-search-archetype/src/main/java/com/example/Book.java
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java
Removed:
search/trunk/hibernate-search-archetype/src/main/archetype/archetype.properties
search/trunk/hibernate-search-archetype/src/main/java/com/
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java
Modified:
search/trunk/hibernate-search-archetype/pom.xml
Log:
HSEARCh-503 Updated archetype
Copied: search/trunk/hibernate-search-archetype/archetype.properties (from rev 19345,
search/trunk/hibernate-search-archetype/src/main/archetype/archetype.properties)
===================================================================
--- search/trunk/hibernate-search-archetype/archetype.properties
(rev 0)
+++ search/trunk/hibernate-search-archetype/archetype.properties 2010-05-03 12:10:11 UTC
(rev 19347)
@@ -0,0 +1,7 @@
+archetype.artifactId=hibernate-search-quickstart
+archetype.version=2.0
+
+archetype.filteredExtensions=java
+archetype.languages=java
+
+
Property changes on: search/trunk/hibernate-search-archetype/archetype.properties
___________________________________________________________________
Name: svn:keywords
+ Id
Name: svn:eol-style
+ native
Modified: search/trunk/hibernate-search-archetype/pom.xml
===================================================================
--- search/trunk/hibernate-search-archetype/pom.xml 2010-05-03 11:59:44 UTC (rev 19346)
+++ search/trunk/hibernate-search-archetype/pom.xml 2010-05-03 12:10:11 UTC (rev 19347)
@@ -1,7 +1,9 @@
<?xml version="1.0"?>
-<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/maven-v4_0_0.xsd">
+<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
-
+
<!--
The link to the hibernate-search parent POM only exists out of convenience.
If you want to continue using this quick-start project you should remove this
@@ -11,19 +13,19 @@
<artifactId>hibernate-search-parent</artifactId>
<groupId>org.hibernate</groupId>
<version>3.2.0-SNAPSHOT</version>
- <relativePath>../pom.xml</relativePath>
</parent>
-
+
+ <groupId>com.example</groupId>
<artifactId>hibernate-search-archetype</artifactId>
<packaging>jar</packaging>
<name>Hibernate Search Archetype</name>
<url>http://www.myorganization.org</url>
-
+
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search</artifactId>
- <version>${pom.version}</version>
+ <version>${parent.version}</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
@@ -73,9 +75,9 @@
<artifactId>junit</artifactId>
<version>4.7</version>
<scope>test</scope>
- </dependency>
+ </dependency>
</dependencies>
-
+
<build>
<plugins>
<plugin>
@@ -92,6 +94,10 @@
<skip>true</skip>
</configuration>
</plugin>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-archetype-plugin</artifactId>
+ </plugin>
</plugins>
</build>
</project>
Deleted: search/trunk/hibernate-search-archetype/src/main/archetype/archetype.properties
===================================================================
---
search/trunk/hibernate-search-archetype/src/main/archetype/archetype.properties 2010-05-03
11:59:44 UTC (rev 19346)
+++
search/trunk/hibernate-search-archetype/src/main/archetype/archetype.properties 2010-05-03
12:10:11 UTC (rev 19347)
@@ -1,3 +0,0 @@
-archetype.groupId=org.hibernate
-archetype.artifactId=hibernate-search-quickstart
-
Added: search/trunk/hibernate-search-archetype/src/main/java/com/example/Author.java
===================================================================
--- search/trunk/hibernate-search-archetype/src/main/java/com/example/Author.java
(rev 0)
+++
search/trunk/hibernate-search-archetype/src/main/java/com/example/Author.java 2010-05-03
12:10:11 UTC (rev 19347)
@@ -0,0 +1,35 @@
+// $Id: com.example.Author.java 17630 2009-10-06 13:38:43Z sannegrinovero $
+package com.example;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Store;
+
+@Entity
+public class Author {
+ @Id
+ @GeneratedValue
+ private Integer id;
+ private String name;
+
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ @Field(index = Index.TOKENIZED, store = Store.YES)
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
Added: search/trunk/hibernate-search-archetype/src/main/java/com/example/Book.java
===================================================================
--- search/trunk/hibernate-search-archetype/src/main/java/com/example/Book.java
(rev 0)
+++ search/trunk/hibernate-search-archetype/src/main/java/com/example/Book.java 2010-05-03
12:10:11 UTC (rev 19347)
@@ -0,0 +1,99 @@
+// $Id: com.example.Book.java 17630 2009-10-06 13:38:43Z sannegrinovero $
+package com.example;
+
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+
+import org.apache.solr.analysis.LowerCaseFilterFactory;
+import org.apache.solr.analysis.SnowballPorterFilterFactory;
+import org.apache.solr.analysis.StandardTokenizerFactory;
+
+import org.hibernate.search.annotations.Analyzer;
+import org.hibernate.search.annotations.AnalyzerDef;
+import org.hibernate.search.annotations.DateBridge;
+import org.hibernate.search.annotations.DocumentId;
+import org.hibernate.search.annotations.Field;
+import org.hibernate.search.annotations.Index;
+import org.hibernate.search.annotations.Indexed;
+import org.hibernate.search.annotations.IndexedEmbedded;
+import org.hibernate.search.annotations.Parameter;
+import org.hibernate.search.annotations.Resolution;
+import org.hibernate.search.annotations.Store;
+import org.hibernate.search.annotations.TokenFilterDef;
+import org.hibernate.search.annotations.TokenizerDef;
+
+@Entity
+@AnalyzerDef(name = "customanalyzer",
+ tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
+ filters = {
+ @TokenFilterDef(factory = LowerCaseFilterFactory.class),
+ @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
+ @Parameter(name = "language", value = "English")
+ })
+ })
+@Indexed
+public class Book {
+
+ private Integer id;
+ private String title;
+ private String subtitle;
+ private Set<Author> authors = new HashSet<Author>();
+ private Date publicationDate;
+
+ @IndexedEmbedded
+ @ManyToMany
+ public Set<Author> getAuthors() {
+ return authors;
+ }
+
+ public void setAuthors(Set<Author> authors) {
+ this.authors = authors;
+ }
+
+ public Book() {
+ }
+
+ @Field(index = Index.TOKENIZED, store = Store.YES)
+ @Analyzer(definition = "customanalyzer")
+ public String getTitle() {
+ return title;
+ }
+
+ public void setTitle(String title) {
+ this.title = title;
+ }
+
+ @Id
+ @DocumentId
+ public Integer getId() {
+ return id;
+ }
+
+ public void setId(Integer id) {
+ this.id = id;
+ }
+
+ @Field(index = Index.TOKENIZED, store = Store.NO)
+ @Analyzer(definition = "customanalyzer")
+ public String getSubtitle() {
+ return subtitle;
+ }
+
+ public void setSubtitle(String subtitle) {
+ this.subtitle = subtitle;
+ }
+
+ @Field(index = Index.UN_TOKENIZED, store = Store.YES)
+ @DateBridge(resolution = Resolution.DAY)
+ public Date getPublicationDate() {
+ return publicationDate;
+ }
+
+ public void setPublicationDate(Date publicationDate) {
+ this.publicationDate = publicationDate;
+ }
+}
\ No newline at end of file
Deleted:
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java
===================================================================
---
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java 2010-05-03
11:59:44 UTC (rev 19346)
+++
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java 2010-05-03
12:10:11 UTC (rev 19347)
@@ -1,169 +0,0 @@
-/* $Id: IndexAndSearchTest.java 18745 2010-02-09 13:00:22Z sannegrinovero $
- *
- * Hibernate, Relational Persistence for Idiomatic Java
- *
- * Copyright (c) 2009, Red Hat, Inc. and/or its affiliates or third-party contributors
as
- * indicated by the @author tags or express copyright attribution
- * statements applied by the authors. All third-party contributions are
- * distributed under license by Red Hat, Inc.
- *
- * This copyrighted material is made available to anyone wishing to use, modify,
- * copy, or redistribute it subject to the terms and conditions of the GNU
- * Lesser General Public License, as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
- * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
- * for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this distribution; if not, write to:
- * Free Software Foundation, Inc.
- * 51 Franklin Street, Fifth Floor
- * Boston, MA 02110-1301 USA
- */
-package com.example;
-
-import com.example.Book;
-
-import org.apache.lucene.analysis.Analyzer;
-import org.apache.lucene.queryParser.MultiFieldQueryParser;
-import org.apache.lucene.queryParser.ParseException;
-import org.apache.lucene.queryParser.QueryParser;
-import org.apache.lucene.util.Version;
-import org.hibernate.Session;
-import org.hibernate.ejb.Ejb3Configuration;
-import org.hibernate.search.FullTextSession;
-import org.hibernate.search.jpa.FullTextEntityManager;
-import org.hibernate.search.jpa.FullTextQuery;
-import org.junit.After;
-import static org.junit.Assert.assertEquals;
-import org.junit.Before;
-import org.junit.Test;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.Query;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-/**
- * Example testcase for Hibernate Search
- */
-public class IndexAndSearchTest {
-
- private EntityManagerFactory emf;
-
- private EntityManager em;
-
- private static Logger log = LoggerFactory.getLogger(IndexAndSearchTest.class);
-
- @Before
- public void setUp() {
- initHibernate();
- }
-
- @After
- public void tearDown() {
- purge();
- }
-
- @Test
- public void testIndexAndSearch() throws Exception {
- List<Book> books = search("hibernate");
- assertEquals("Should get empty list since nothing is indexed yet", 0,
books.size());
-
- index();
-
- // search by title
- books = search("hibernate");
- assertEquals("Should find one book", 1, books.size());
- assertEquals("Wrong title", "Java Persistence with
Hibernate", books.get(0).getTitle());
-
- // search author
- books = search("\"Gavin King\"");
- assertEquals("Should find one book", 1, books.size());
- assertEquals("Wrong title", "Java Persistence with
Hibernate", books.get(0).getTitle());
- }
-
- @Test
- public void testStemming() throws Exception {
-
- index();
-
- List<Book> books = search("refactor");
- assertEquals("Wrong title", "Refactoring: Improving the Design of
Existing Code", books.get(0).getTitle());
-
- books = search("refactors");
- assertEquals("Wrong title", "Refactoring: Improving the Design of
Existing Code", books.get(0).getTitle());
-
- books = search("refactored");
- assertEquals("Wrong title", "Refactoring: Improving the Design of
Existing Code", books.get(0).getTitle());
-
- books = search("refactoring");
- assertEquals("Wrong title", "Refactoring: Improving the Design of
Existing Code", books.get(0).getTitle());
- }
-
-
- private void initHibernate() {
- Ejb3Configuration config = new Ejb3Configuration();
- config.configure("hibernate-search-example", new HashMap());
- emf = config.buildEntityManagerFactory();
- em = emf.createEntityManager();
- }
-
- private void index() {
- FullTextSession ftSession =
org.hibernate.search.Search.getFullTextSession((Session) em.getDelegate());
- try {
- ftSession.createIndexer().startAndWait();
- }
- catch (InterruptedException e) {
- log.error( "Was interrupted during indexing", e );
- }
- }
-
- private void purge() {
- FullTextSession ftSession =
org.hibernate.search.Search.getFullTextSession((Session) em.getDelegate());
- ftSession.purgeAll(Book.class);
- ftSession.flushToIndexes();
- }
-
- private List<Book> search(String searchQuery) throws ParseException {
- Query query = searchQuery(searchQuery);
-
- List<Book> books = query.getResultList();
-
- for (Book b : books) {
- log.info("Title: " + b.getTitle());
- }
- return books;
- }
-
- private Query searchQuery(String searchQuery) throws ParseException {
-
- String[] bookFields = {"title", "subtitle",
"authors.name", "publicationDate"};
-
- //lucene part
- Map<String, Float> boostPerField = new HashMap<String, Float>(4);
- boostPerField.put(bookFields[0], (float) 4);
- boostPerField.put(bookFields[1], (float) 3);
- boostPerField.put(bookFields[2], (float) 4);
- boostPerField.put(bookFields[3], (float) .5);
-
- FullTextEntityManager ftEm =
org.hibernate.search.jpa.Search.getFullTextEntityManager((EntityManager) em);
- Analyzer customAnalyzer =
ftEm.getSearchFactory().getAnalyzer("customanalyzer");
- QueryParser parser = new MultiFieldQueryParser(Version.LUCENE_29, bookFields,
- customAnalyzer, boostPerField);
-
- org.apache.lucene.search.Query luceneQuery;
- luceneQuery = parser.parse(searchQuery);
-
- final FullTextQuery query = ftEm.createFullTextQuery(luceneQuery, Book.class);
-
- return query;
- }
-
-}
Added:
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java
===================================================================
---
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java
(rev 0)
+++
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java 2010-05-03
12:10:11 UTC (rev 19347)
@@ -0,0 +1,147 @@
+package com.example;// $Id: com.example.IndexAndSearchTest.java 18745 2010-02-09
13:00:22Z sannegrinovero $
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Query;
+
+import org.apache.lucene.analysis.Analyzer;
+import org.apache.lucene.queryParser.MultiFieldQueryParser;
+import org.apache.lucene.queryParser.ParseException;
+import org.apache.lucene.queryParser.QueryParser;
+import org.apache.lucene.util.Version;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.hibernate.Session;
+import org.hibernate.ejb.Ejb3Configuration;
+import org.hibernate.search.FullTextSession;
+import org.hibernate.search.jpa.FullTextEntityManager;
+import org.hibernate.search.jpa.FullTextQuery;
+
+import static org.junit.Assert.assertEquals;
+
+/**
+ * Example testcase for Hibernate Search
+ */
+public class IndexAndSearchTest {
+
+ private EntityManagerFactory emf;
+
+ private EntityManager em;
+
+ private static Logger log = LoggerFactory.getLogger( IndexAndSearchTest.class );
+
+ @Before
+ public void setUp() {
+ initHibernate();
+ }
+
+ @After
+ public void tearDown() {
+ purge();
+ }
+
+ @Test
+ public void testIndexAndSearch() throws Exception {
+ List<Book> books = search( "hibernate" );
+ assertEquals( "Should get empty list since nothing is indexed yet", 0,
books.size() );
+
+ index();
+
+ // search by title
+ books = search( "hibernate" );
+ assertEquals( "Should find one book", 1, books.size() );
+ assertEquals( "Wrong title", "Java Persistence with Hibernate",
books.get( 0 ).getTitle() );
+
+ // search author
+ books = search( "\"Gavin King\"" );
+ assertEquals( "Should find one book", 1, books.size() );
+ assertEquals( "Wrong title", "Java Persistence with Hibernate",
books.get( 0 ).getTitle() );
+ }
+
+ @Test
+ public void testStemming() throws Exception {
+
+ index();
+
+ List<Book> books = search( "refactor" );
+ assertEquals( "Wrong title", "Refactoring: Improving the Design of
Existing Code", books.get( 0 ).getTitle() );
+
+ books = search( "refactors" );
+ assertEquals( "Wrong title", "Refactoring: Improving the Design of
Existing Code", books.get( 0 ).getTitle() );
+
+ books = search( "refactored" );
+ assertEquals( "Wrong title", "Refactoring: Improving the Design of
Existing Code", books.get( 0 ).getTitle() );
+
+ books = search( "refactoring" );
+ assertEquals( "Wrong title", "Refactoring: Improving the Design of
Existing Code", books.get( 0 ).getTitle() );
+ }
+
+
+ private void initHibernate() {
+ Ejb3Configuration config = new Ejb3Configuration();
+ config.configure( "hibernate-search-example", new HashMap() );
+ emf = config.buildEntityManagerFactory();
+ em = emf.createEntityManager();
+ }
+
+ private void index() {
+ FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession( ( Session )
em.getDelegate() );
+ try {
+ ftSession.createIndexer().startAndWait();
+ }
+ catch ( InterruptedException e ) {
+ log.error( "Was interrupted during indexing", e );
+ }
+ }
+
+ private void purge() {
+ FullTextSession ftSession = org.hibernate.search.Search.getFullTextSession( ( Session )
em.getDelegate() );
+ ftSession.purgeAll( Book.class );
+ ftSession.flushToIndexes();
+ }
+
+ private List<Book> search(String searchQuery) throws ParseException {
+ Query query = searchQuery( searchQuery );
+
+ List<Book> books = query.getResultList();
+
+ for ( Book b : books ) {
+ log.info( "Title: " + b.getTitle() );
+ }
+ return books;
+ }
+
+ private Query searchQuery(String searchQuery) throws ParseException {
+
+ String[] bookFields = { "title", "subtitle",
"authors.name", "publicationDate" };
+
+ //lucene part
+ Map<String, Float> boostPerField = new HashMap<String, Float>( 4 );
+ boostPerField.put( bookFields[0], ( float ) 4 );
+ boostPerField.put( bookFields[1], ( float ) 3 );
+ boostPerField.put( bookFields[2], ( float ) 4 );
+ boostPerField.put( bookFields[3], ( float ) .5 );
+
+ FullTextEntityManager ftEm = org.hibernate.search.jpa.Search.getFullTextEntityManager(
( EntityManager ) em );
+ Analyzer customAnalyzer = ftEm.getSearchFactory().getAnalyzer(
"customanalyzer" );
+ QueryParser parser = new MultiFieldQueryParser(
+ Version.LUCENE_29, bookFields,
+ customAnalyzer, boostPerField
+ );
+
+ org.apache.lucene.search.Query luceneQuery;
+ luceneQuery = parser.parse( searchQuery );
+
+ final FullTextQuery query = ftEm.createFullTextQuery( luceneQuery, Book.class );
+
+ return query;
+ }
+
+}