[hibernate-commits] Hibernate SVN: r19209 - in search/trunk/hibernate-search-archetype: src/main/java and 5 other directories.
hibernate-commits at lists.jboss.org
hibernate-commits at lists.jboss.org
Sun Apr 11 07:17:30 EDT 2010
Author: sannegrinovero
Date: 2010-04-11 07:17:29 -0400 (Sun, 11 Apr 2010)
New Revision: 19209
Added:
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/
search/trunk/hibernate-search-archetype/src/test/java/com/example/
search/trunk/hibernate-search-archetype/src/test/java/com/example/IndexAndSearchTest.java
Removed:
search/trunk/hibernate-search-archetype/src/main/java/example/
search/trunk/hibernate-search-archetype/src/test/java/example/
search/trunk/hibernate-search-archetype/src/test/java/java/
Modified:
search/trunk/hibernate-search-archetype/pom.xml
Log:
HSEARCH-503 Generated archetype broken for any package name with dot (.) (Gustavo Fernandes)
Modified: search/trunk/hibernate-search-archetype/pom.xml
===================================================================
--- search/trunk/hibernate-search-archetype/pom.xml 2010-04-10 17:16:15 UTC (rev 19208)
+++ search/trunk/hibernate-search-archetype/pom.xml 2010-04-11 11:17:29 UTC (rev 19209)
@@ -5,7 +5,7 @@
<!--
The link to the hibernate-search parent POM only exists out of convenience.
- If you want to continue using this quickstart proejct you should remove this
+ If you want to continue using this quickstart project you should remove this
entry.
-->
<parent>
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-04-11 11:17:29 UTC (rev 19209)
@@ -0,0 +1,59 @@
+/* $Id: Author.java 17630 2009-10-06 13:38:43Z 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 org.hibernate.search.annotations.*;
+
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+
+/*
+ *
+ */
+ at 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-04-11 11:17:29 UTC (rev 19209)
@@ -0,0 +1,111 @@
+/* $Id: Book.java 17630 2009-10-06 13:38:43Z 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.Author;
+import org.hibernate.search.annotations.*;
+import org.apache.solr.analysis.*;
+
+import javax.persistence.Entity;
+import javax.persistence.Id;
+import javax.persistence.ManyToMany;
+import java.util.Date;
+import java.util.HashSet;
+import java.util.Set;
+
+/**
+ *
+ */
+ at Entity
+ at AnalyzerDef(name = "customanalyzer",
+ tokenizer = @TokenizerDef(factory = StandardTokenizerFactory.class),
+ filters = {
+ @TokenFilterDef(factory = LowerCaseFilterFactory.class),
+ @TokenFilterDef(factory = SnowballPorterFilterFactory.class, params = {
+ @Parameter(name = "language", value = "English")
+ })
+ })
+ at 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
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-04-11 11:17:29 UTC (rev 19209)
@@ -0,0 +1,169 @@
+/* $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;
+ }
+
+}
More information about the hibernate-commits
mailing list