Lucene vs Solr,采样数据的索引速度



我以前研究过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秒内完成

我的绒球.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 start -e cloud -noprompt在 2 个节点中启动 solr。

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

我知道Solr使用Lucene,所以我犯的错误是什么。我仍在研究这个场景。

欢迎任何帮助或指导。

提前感谢。!!干杯:)

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

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

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

他提到的ConcurrentUpdateSolrClient是实现此目的的一种方法,但它有一个相当大的缺点 - 如果任何索引请求失败,则不会通知客户端代码。 CUSC吞下了大多数例外。

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

最新更新