Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I spent a long time on this because I was so new to Mongo DB and hadn't planned on using it until it seemed like a good solution for a search implementation on Heroku. I thought I would add this answer to help anyone else who wanted to do the same thing.</p> <p>I used code from the lucene mongo db project located here:</p> <p><a href="https://github.com/mongoutils/mongo-lucene" rel="nofollow">https://github.com/mongoutils/mongo-lucene</a></p> <p>Then created my own hibernate search directory provider class that interfaces with the lucene mongo db project. I used the org.hibernate.search.store.impl.RAMDirectoryProvider class as an outline to follow.</p> <p>All thats left to do is to have your hibernate configuration point to your new class:</p> <pre><code>hibernate.search.default.directory_provider=com.foo.MongoLuceneDirectoryProvider </code></pre> <p>Here is my MongoLuceneDirectoryProvider class:</p> <pre><code>package com.foo.MongoLuceneDirectoryProvider import java.io.IOException; import java.net.UnknownHostException; import java.util.Properties; import java.util.concurrent.ConcurrentMap; import org.apache.log4j.Logger; import org.apache.lucene.store.Directory; import org.hibernate.search.SearchException; import org.hibernate.search.indexes.impl.DirectoryBasedIndexManager; import org.hibernate.search.spi.BuildContext; import org.hibernate.search.store.DirectoryProvider; import org.hibernate.search.store.impl.DirectoryProviderHelper; import com.github.mongoutils.collections.DBObjectSerializer; import com.github.mongoutils.collections.MongoConcurrentMap; import com.github.mongoutils.collections.SimpleFieldDBObjectSerializer; import com.github.mongoutils.lucene.MapDirectory; import com.github.mongoutils.lucene.MapDirectoryEntry; import com.github.mongoutils.lucene.MapDirectoryEntrySerializer; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.Mongo; import com.mongodb.MongoException; public class MongoLuceneDirectoryProvider implements DirectoryProvider&lt;Directory&gt; { private Logger log = Logger.getLogger(MongoLuceneDirectoryProvider.class); private Directory directory; private String indexName; private Properties properties; @Override public void initialize(String directoryProviderName, Properties properties, BuildContext context) { indexName = directoryProviderName; this.properties = properties; Mongo mongo; try { mongo = new Mongo("localhost"); DB db = mongo.getDB("testdb"); DBCollection dbCollection = db.getCollection("testcollection"); DBObjectSerializer&lt;String&gt; keySerializer = new SimpleFieldDBObjectSerializer&lt;String&gt;("key"); DBObjectSerializer&lt;MapDirectoryEntry&gt; valueSerializer = new MapDirectoryEntrySerializer("value"); ConcurrentMap&lt;String, MapDirectoryEntry&gt; store = new MongoConcurrentMap&lt;String, MapDirectoryEntry&gt;(dbCollection, keySerializer, valueSerializer); directory = new MapDirectory(store); } catch (UnknownHostException e) { log.error("While attempting to initalize Mongo directory provider", e); } catch (MongoException e) { log.error("While attempting to initalize Mongo directory provider", e); } catch (IOException e) { log.error("While attempting to initalize Mongo directory provider", e); } } @Override public void start(DirectoryBasedIndexManager indexManager) { try { directory.setLockFactory(DirectoryProviderHelper.createLockFactory(null, properties)); properties = null; DirectoryProviderHelper.initializeIndexIfNeeded(directory); } catch (IOException e) { throw new SearchException("Unable to initialize index: " + indexName, e); } } @Override public void stop() { try { directory.close(); } catch (IOException e) { log.error("While attempting to stop directory provider", e); } } @Override public Directory getDirectory() { return this.directory; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload