【问题标题】:Lucene vs Solr, indexning speed for sampe dataLucene vs Solr,样本数据的索引速度
【发布时间】:2015-08-29 15:48:29
【问题描述】:

我之前研究过 Lucene,现在转向 Solr。 问题是我无法像 Lucene 那样快速地在 Solr 上进行索引。

我的 Lucene 代码:

public class LuceneIndexer {

public static void main(String[] args) {

    String indexDir = "/home/demo/indexes/index1/"; 
    IndexWriterConfig indexWriterConfig = null;

    long starttime = System.currentTimeMillis();

    try (Directory dir = FSDirectory.open(Paths.get(indexDir));
            Analyzer analyzer = new StandardAnalyzer();
            IndexWriter indexWriter = new IndexWriter(dir,
                    (indexWriterConfig = new IndexWriterConfig(analyzer)));) {
        indexWriterConfig.setOpenMode(OpenMode.CREATE);

            StringField bat = new StringField("bat", "", Store.YES); //$NON-NLS-1$ //$NON-NLS-2$
            StringField id = new StringField("id", "", Store.YES); //$NON-NLS-1$ //$NON-NLS-2$
            StringField name = new StringField("name", "", Store.YES); //$NON-NLS-1$ //$NON-NLS-2$
            StringField id1 = new StringField("id1", "", Store.YES); //$NON-NLS-1$ //$NON-NLS-2$
            StringField name1 = new StringField("name1", "", Store.YES); //$NON-NLS-1$ //$NON-NLS-2$
            StringField id2 = new StringField("id2", "", Store.YES); //$NON-NLS-1$ //$NON-NLS-2$

            Document doc = new Document();
            doc.add(bat);doc.add(id);doc.add(name);doc.add(id1);doc.add(name1);doc.add(id2);

        for (int i = 0; i < 1000000; ++i) { 
             bat.setStringValue("book"+i);
             id.setStringValue("book id -" + i);
             name.setStringValue("The Legend of the Hobbit part 1 " + i);
             id1.setStringValue("book id -" + i);
             name1.setStringValue("The Legend of the Hobbit part 2 " + i); 
             id2.setStringValue("book id -" + i);//doc.addField("id2", "book id -" + i); //$NON-NLS-1$ 

             indexWriter.addDocument(doc);
        }
    }catch(Exception e) {
        e.printStackTrace();
    }
    long endtime = System.currentTimeMillis();
    System.out.println("commited"); //$NON-NLS-1$
    System.out.println("process completed in "+(endtime-starttime)/1000+" seconds"); //$NON-NLS-1$ //$NON-NLS-2$
}
}

输出: 过程在 19 秒内完成

跟随我的 Solr 代码:

    SolrClient solrClient = new HttpSolrClient("http://localhost:8983/solr/gettingstarted"); //$NON-NLS-1$

    // Empty the database...
    solrClient.deleteByQuery( "*:*" );// delete everything! //$NON-NLS-1$
    System.out.println("cleared"); //$NON-NLS-1$
    ArrayList<SolrInputDocument> docs = new ArrayList<>();



    long starttime = System.currentTimeMillis();
    for (int i = 0; i < 1000000; ++i) { 
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("bat", "biok"+i); //$NON-NLS-1$ //$NON-NLS-2$
        doc.addField("id", "biok id -" + i); //$NON-NLS-1$ //$NON-NLS-2$
        doc.addField("name", "Tle Legend of the Hobbit part 1 " + i); //$NON-NLS-1$ //$NON-NLS-2$
        doc.addField("id1", "bopk id -" + i); //$NON-NLS-1$ //$NON-NLS-2$
        doc.addField("name1", "Tue Legend of the Hobbit part 2 " + i); //$NON-NLS-1$ //$NON-NLS-2$
        doc.addField("id2", "bopk id -" + i); //$NON-NLS-1$ //$NON-NLS-2$

        docs.add(doc);

        if (i % 250000 == 0) {
            solrClient.add(docs);
            docs.clear();
        }
    }
    solrClient.add(docs);
    System.out.println("completed adding to Solr. Now commiting.. Please wait"); //$NON-NLS-1$
    solrClient.commit();
    long endtime = System.currentTimeMillis();
    System.out.println("process completed in "+(endtime-starttime)/1000+" seconds"); //$NON-NLS-1$ //$NON-NLS-2$

输出:过程在 159 秒内完成

我的 pom.xml 是

<!-- solr dependency -->
    <dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-solrj</artifactId>
        <version>5.0.0</version>
    </dependency>

<!-- other dependency -->   
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.1.1</version>
    </dependency>

<!-- Lucene dependency -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>5.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>5.0.0</version>
    </dependency>

我已经下载了 solr 5.0,然后开始使用 solr $solr/bin/solr 开始 -e 云 -noprompt 在 2 个节点中启动 solr。

我没有更改我下载的 solr 设置中的任何内容,任何人都可以指导我了解发生了什么问题。我读到 solr 可用于近乎实时的索引 (http://lucene.apache.org/solr/features.html),但我无法在我的演示代码中做到这一点,但 Lucene 的索引速度很快,如果不是,可以用于近乎实时的索引即时的。

我知道 Solr 使用 Lucene,所以我犯了什么错误.. 我仍在研究场景。

非常欢迎任何帮助或指导。

提前致谢。!! 干杯:)

【问题讨论】:

  • 更新:我用 Solr 5.2.0 尝试过同样的方法,结果是一样的:Solr 索引比 Lucen 的近实时索引(19 秒)慢得多(159 秒).. 有没有人使用 Solr 进行索引,发现它像 Lucene 一样快??

标签: java indexing solr lucene full-text-search


【解决方案1】:

Solr 是一个通用的高度可配置的搜索服务器。 Solr 中的 Lucene 代码针对一般用途而不是特定用例进行了调整。可以在配置和请求语法中进行一些调整。

为特定用例编写的经过良好调整的 Lucene 代码将始终优于 Solr。缺点是您必须自己编写、测试和调试搜索代码的低级实现。如果这对您来说不是一个主要缺点,那么您可能想要坚持使用 Lucene。您将拥有比 Solr 所能提供的更多功能,并且您很可能会使其运行得更快。

您在 Solr 邮件列表上从 Erick 获得的回复是相关的。为了获得最佳的索引性能,您的客户端必须并行向 Solr 发送更新。

他提到的 ConcurrentUpdateSolrClient 是执行此操作的一种方法,但它有一个相当大的缺点——如果任何这些索引请求失败,将不会通知客户端代码。 CUSC 吞下了大多数例外情况。

如果您想要适当的异常处理,您将需要自己管理线程并使用 HttpSolrClient,如果您选择运行 SolrCloud,则使用 CloudSolrClient。 SolrClient 实现是线程安全的。

【讨论】:

  • 您好 Elyograg,我尝试在此处的 solr 邮件列表中搜索 Ericks 回复 mail-archives.apache.org/mod_mbox/lucene-solr-user/201506.mbox/... 但找不到,如果您可以提供指向 Erick 解决我的问题的 solr 邮件列表的链接,将会有很大帮助。
猜你喜欢
  • 1970-01-01
  • 2011-10-26
  • 1970-01-01
  • 1970-01-01
  • 2016-01-20
  • 1970-01-01
  • 2015-03-26
  • 2011-01-19
  • 2010-11-26
相关资源
最近更新 更多