Hibernate Search N Hot Tips and More

Hibernate Search N Hot Tips and More

#32 Brought to you by… m CONTENTS INCLUDE: n Google Your Database! n Getting Started with Mapping Entities n Bridges n Initial Indexing of Entities n Querying Indexes Hibernate Search n Hot Tips and more... By John Griffin Visit refcardz.co GOOGLE YOUR DATABASE! Hot The apache-solr-analyzer.jar capabilities are only available in Hibernate Search 3.1+. Hibernate Search complements Hibernate Core by enabling full- Tip text search queries on persistent domain models, and brings Lucene search features to the Hibernate world. Hibernate Search Hibernate Search is not compatible with all versions of Hibernate depends on Apache Lucene, a powerful full-text search engine Core and Hibernate Annotations. Refer to Table 2 for compati- library (and a de facto standard solution in Java) hosted at the bility requirements. The latest version is available on the Apache Software Foundation (http://www.apache.org/). This Hibernate download page at http://www.hibernate.org/6.html. refcard explains installation and configuration, and covers Package Version Core Annotations Entity Search Get More Refcardz! Get More Mapping entities, bridges, building indexes, querying them and Manager examining their contents. Table 1 shows links to documentation. Hibernate Core 3.2.6 GA 3.2.x, 3.3.x 3.2.x, 3.3.x 3.0.x Topic URL 3.3.0 SP1 3.4.x 3.4.x 3.1.x Lucene http://lucene.apache.org/java/docs/ m Hibernate 3.3.1 GA 3.2.x 3.3.x 3.0.x Hibernate Search http://www.hibernate.org/410.html Annotations Mailing lists http://www.hibernate.org/20.html 3.4.0 GA 3.3.x 3.4.x 3.1.x e.co JIRA http://opensource.atlassian.com/projects/hibernate/secure/ Hibernate 3.3.2 GA 3.2.x 3.3.x 3.0.x Dashboard.jspa EntityManager zon d 3.4.0 GA . Table 1 Documentation Links 3.3.x 3.4.x 3.1.x w Hibernate 3.0.0 GA 3.2.x 3.3.x 3.3.x 3.0.x GETTING STARTED Validator ww 3.1.0 GA 3.3.x 3.4.x 3.4.x 3.1.x Hibernate 3.0.1 GA >= 3.2.2 3.3.x (better 3.3.x In order to use Hibernate Search you should understand the basics Search (better if if >= 3.3.1 ) of Hibernate, and be familiar with the object manipulation APIs >= 3.2.6) from the Hibernate Session or the Java Persistence EntityManager 3.1.0 3.3 3.4 3.4 - as well as the query APIs. You should also be familiar with associ- Beta1 ation mappings and the concept of bidirectional relationships. Hibernate 3.0.0 3.2.x 3.3.x Not 3.0.x Search Download Hibernate Search at http://www.hibernate.org or use Shards Beta2 compatible the JBoss Maven repository (http://repository.jboss.org/maven2/ Hibernate 3.2.2 3.2.x 3.2.x and 3.2.x and (3.2.0) org/hibernate/hibernate-search). It is interesting to download Tools 3.3.x 3.3.x the Apache Lucene distribution as well, available at http://lucene. Table 2: Compatibility Matrix. apache.org/java/. It contains both documentation and a contri- bution section containing various add-ons not included in Hibernate Search. Make sure to use the same Lucene version Hibernate Search is based on. You can find the correct version in the Hibernate Search distribution in lib/readme.txt. Hibernate >\kjlggfik]fi?`Y\ieXk\ Hibernate Search requires three JARs – all available in the JBoss Enterprise Application Platform Hibernate Search distribution: includes Hibernate n hibernate-search.jar: the core API and engine of • JBoss Enterprise Application Platform pre-integrates Hibernate Search JBoss Application Server, Seam, and Hibernate n lucene-core.jar: Apache Lucene engine • Includes caching, clustering, messaging, transactions, and integrated web services stack n hibernate-commons-annotations.jar: some common utilities • Support for industry-leading Java and technologies like for the Hibernate project JAX-WS, EJB 3.0, JPA 1.0, JSF 1.2, and JTA 1.1 You can also add the optional support for modular analyzers • Use JBoss Operations Network to monitor and tune by adding: apache-solr-analyzer.jar. This JAR (available in the Hibernate queries Download today: jboss.com/download Hibernate Search distribution), is a subset of the SOLR distribution that contains various analyzers. While optional, it is recommended © 2008 Red Hat Middleware, LLC. All Rights Reserved. Red Hat, Red Hat Enterprise Linux, the to add this JAR to your classpath as it greatly simplifies the use of Shadowman logo and JBoss are registered trademarks of Red Hat, Inc. in the U.S. and other analyzers. countries. Linux is a registered trademark of Linus Torvalds. Getting Started wtih DZone, Inc. | www.dzone.com 2 Getting Started with Hibernate Search tech facts at your fingertips Table 3, continued Dependencies needed to build and initially test Hot hibernate.search.filter.cache_ The filter caching strategy class (must Tip Hibernate Search are included in the Hibernate strategy have a no-arg constructor and Search distribution or can be found in the implement FilterCachingStrategy). hibernate.search.filter.cache_bit_ The hard ref count of our Caching Maven dependency file (POM) which is results.size WrapperFilter. Defaults to 5. included with the Hibernate Search download. Table 3: Hibernate Search configuration parameters, continued Configuration Parameters MAPPING ENTITIES Configuration parameters can be provided in three ways: n In a hibernate.cfg.xml file n In a /hibernate.properties file n Through the configuration API and specifically configuration.setProperty(String, String) Listing 1: An example hibernate.cfg.xml file. <?xml version=”1.0” encoding=”UTF-8”?> hibernate.cfg.xml file <!DOCTYPE hibernate-configuration PUBLIC “-//Hibernate/Hibernate Configuration DTD 3.0//EN” “http://hibernate.sourceforge.net/ hibernate-configuration-3.0.dtd”> <!-- hibernate.cfg.xml --> <hibernate-configuration> <session-factory name=”dvdstore-catalog”> <!-- regular Hibernate Core configuration --> <property name=”hibernate.dialect”> org.hibernate.dialect.PostgreSQLDialect” </property> <property name=”hibernate.connection.datasource”> jdbc/test </property> <!-- Hibernate Search configuration --> <property name=”hibernate.search.default.indexBase”> /users/application/indexes Figure 1: Basic entity mapping. </property> <!-- mapping classes --> <mapping class=”com.manning.dvdstore.model.Item”/> BRIDGES list additional entities </session-factory> </hibernate-configuration> Bridges fulfill several needs in the Hibernate Search architecture. n Converts an object instance into a Lucene consumable Parameter Description representation (commonly a String) and adds it to a Lucene hibernate.search.autoregister_ Enable listeners auto registration in document. listeners Hibernate Annotations and Entity- Manager. Default to true. n Reads information from the Lucene document and builds hibernate.search.indexing_strategy Defines the indexing strategy, default to back the object representation. event. Other option is manual. hibernate.search.analyzer The default Lucene analyzer class. Bridges that support both the conversion object to Lucene and Lucene to object are called two-way bridges. Table 4 lists all out- hibernate.search.similarity The default Lucene similarity class. of-the-box Hibernate Search bridges. hibernate.search.worker.batch_size Has been deprecated in favor of this explicit API Java Type Build-in Bridge Description hibernate.search.worker.backend Out of the box support for the Apache Lucene backend and the JMS back end. String StringBridge no-op Defaults to lucene. Other option is jms. hibernate.search.worker.execution Supports synchronous and asynchronous short / Short ShortBridge Use toString(), not comparable execution. Defaults to sync. Other option is async. int / Integer IntegerBridge Use toString(), not comparable hibernate.search.worker.thread_ Defines the number of threads in the long / Long LongBridge Use toString(), not comparable pool.size pool. Useful only for asynchronous execution. Default to 1. float / Float FloatBridge Use toString(), not comparable hibernate.search.worker.buffer_ Defines the maximal number of work double / DoubleBridge Use toString(), not comparable queue.max queue if the thread poll is starved. Useful Double only for asynchronous execution. Default to infinite. If the limit is reached, the work BigDecimal BigDecimalBridge Use toString(), not comparable is done by the main thread. hibernate.search.worker.jndi.* Defines the JNDI properties to initiate BigInteger BigIntegerBridge Use toString(), not comparable the InitialContext (if needed). JNDI is only used by the JMS back end. boolean / BooleanBridge String value: “true” / “false” Boolean hibernate.search.worker.jms. Mandatory for the JMS back end. Defines connection_factory the JNDI name to lookup the JMS con- Class ClassBridge Allows manipulation of any combination nection factory from. (java:/Connect of different fields. ionFactory by default in JBoss AS) Enum EnumBridge Use enum.name() hibernate.search.worker.jms.queue Mandatory for the JMS back end. Defines the JNDI name to lookup the URL UrlBridge Converts to the String representation JMS queue from. The queue will be used to post work messages. URI UriBridge Converts to the String representation hibernate.search.reader.strategy Defines the reader strategy used. Defaults to shared. Other option is not-shared. Table 4: List of standard Hibernate Search bridges. Table 3: Hibernate Search configuration parameters. DZone, Inc. | www.dzone.com 3 Getting Started with Hibernate Search tech facts at your fingertips Bridges, continued Associations, continued Date DateBridge The string representation depends on Listing 4 shows that @ContainedIn is paired to an @IndexedEmbedded @DateBridge. Converting Date into annotation on the other side of the bi-directional relationship. string and back is not guaranteed to be idempotent Listing 4: Figure 2 in code. Table 4: List of standard Hibernate Search bridges, continued. @Entity @Indexed public class Item { @ManyToMany Custom bridges allow for converting unexpected data types. The @IndexedEmbedded @FieldBridge annotation is placed on a property (field or getter) private Set<Actor> actors; // embed actors when indexing that needs to be processed by a custom bridge. An example, @ManyToOne @IndexedEmbedded including parameter passing, is given in Listing 2.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    6 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us