我想在Heroku的网站上使用这个解决方案,所以我可以使用MongoDB索引有没有人知道一个好的教程或书籍,深入举例说明了如何做到这一点?谷歌搜索并没有给我带来任何好处。
感谢。
答案 0 :(得分:1)
我花了很长时间在这上面因为我对Mongo DB这么新,并且没有计划使用它,直到它看起来像是Heroku上的搜索实现的一个很好的解决方案。我想我会添加这个答案来帮助其他想要做同样事情的人。
我使用了位于此处的lucene mongo db项目中的代码:
https://github.com/mongoutils/mongo-lucene
然后创建了我自己的hibernate搜索目录提供程序类,它与lucene mongo db项目接口。我使用org.hibernate.search.store.impl.RAMDirectoryProvider类作为要遵循的大纲。
剩下要做的就是让你的休眠配置指向你的新课程:
hibernate.search.default.directory_provider=com.foo.MongoLuceneDirectoryProvider
这是我的MongoLuceneDirectoryProvider类:
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<Directory> {
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<String> keySerializer = new SimpleFieldDBObjectSerializer<String>("key");
DBObjectSerializer<MapDirectoryEntry> valueSerializer = new MapDirectoryEntrySerializer("value");
ConcurrentMap<String, MapDirectoryEntry> store = new MongoConcurrentMap<String, MapDirectoryEntry>(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; }
}
答案 1 :(得分:0)
如果您想帮助实现它(我的意思是创建支持此配置的Java代码),请在更合适的邮件列表中加入对话:
http://lists.jboss.org/pipermail/hibernate-dev/2013-January/009329.html
在此订阅:
https://lists.jboss.org/mailman/listinfo/hibernate-dev
应该是直接编码来集成一些现有的组件。