1

我想在 Heroku 上为我的网站使用这个解决方案,这样我就可以使用 MongoDB 进行索引。有谁知道一个很好的教程或书籍,它提供了一个深入的例子来说明如何做到这一点?谷歌搜索并没有真正为我找到任何东西。

谢谢。

4

2 回答 2

1

我在这方面花了很长时间,因为我对 Mongo DB 还很陌生,直到它看起来是 Heroku 上搜索实现的一个很好的解决方案时才打算使用它。我想我会添加这个答案来帮助其他想做同样事情的人。

我使用了来自这里的 lucene mongo db 项目的代码:

https://github.com/mongoutils/mongo-lucene

然后创建了我自己的与 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; }

}
于 2013-01-28T01:16:55.200 回答
0

如果您想帮助实现它(我的意思是创建 Java 代码来支持此配置),请加入更合适的邮件列表中的对话:

http://lists.jboss.org/pipermail/hibernate-dev/2013-January/009329.html

在这里订阅:

https://lists.jboss.org/mailman/listinfo/hibernate-dev

集成一些现有组件应该是直接编码。

于 2013-01-11T18:52:04.720 回答