001/*
002 * Copyright 2012 Atteo.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.atteo.moonshine.hibernate.search;
017
018import java.util.HashMap;
019import java.util.Map;
020
021import javax.xml.bind.annotation.XmlElement;
022import javax.xml.bind.annotation.XmlRootElement;
023
024import org.atteo.evo.config.XmlDefaultValue;
025import org.atteo.moonshine.hibernate.HibernatePlugin;
026import org.hibernate.search.Environment;
027import org.hibernate.search.indexes.spi.IndexManager;
028
029@XmlRootElement(name = "search")
030public class HibernateSearch extends HibernatePlugin {
031    /**
032     * By default, every time an object is inserted, updated or deleted through Hibernate,
033     * Hibernate Search updates the according Lucene index. It is sometimes desirable
034     * to disable that feature if either your index is read-only
035     * or if index updates are done in a batch way.
036     * To disable event based indexing, set this property to "manual".
037     */
038    @XmlElement
039    private String indexing_strategy = "event";
040
041    /**
042     * Select implementations for {@link IndexManager} interface.
043     * Hibernate Search provides two implementations for this interface.
044     * <ul>
045     * <li>directory-based: the default implementation which uses the Lucene Directory
046     * abstraction to manage index files.</li>
047     * <li>near-real-time: avoid flushing writes to disk at each commit.
048     * This index manager is also Directory based, but also makes uses of Lucene's NRT functionality.
049     * </li>
050     * </ul>
051     * It is also possible to configure a custom IndexManager implementation
052     * by specifying the fully qualified class name of your custom implementation.
053     * This implementation must have a no-argument constructor.
054     */
055    @XmlElement
056    private String indexmanager = "directory-based";
057
058    /**
059     * Directory provider to use.
060     * <ul>
061     * <li>ram: Memory based directory, the directory will be uniquely identified
062     * (in the same deployment unit) by the @Indexed.index element</li>
063     * <li>filesystem: File system based directory.
064     * The directory used will be &lt;indexBase&gt;/&lt;indexName&gt;</li>
065     * </ul>
066     * See Hibernate Search <a ref="http://docs.jboss.org/hibernate/search/4.1/reference/en-US/html/search-configuration.html#directory-provider-table">documentation</a> for more options and details.
067     */
068    @XmlElement
069    private String directory_provider = "filesystem";
070
071    /**
072     * Base directory for "filesystem" directory provider.
073     */
074    @XmlElement
075    @XmlDefaultValue("${dataHome}/indexes")
076    private String indexBase;
077
078    @Override
079    public Map<String, Object> getProperties() {
080        Map<String, Object> map = new HashMap<>();
081        map.put(Environment.INDEXING_STRATEGY, indexing_strategy);
082        map.put("hibernate.search.autoregister_listeners", true);
083        map.put("hibernate.search.default." + Environment.INDEX_MANAGER_IMPL_NAME, indexmanager);
084        map.put("hibernate.search.default.directory_provider", directory_provider);
085        map.put("hibernate.search.default.indexBase", indexBase);
086        return map;
087    }
088}