Lucene vs Solr,样本数据的索引速度
Lucene vs Solr, indexning speed for sampe data
我之前在 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 是一个通用的高度可配置的搜索服务器。 Solr 中的 Lucene 代码针对一般用途进行了调整,而不是针对特定用例。可以在配置和请求语法中进行一些调整。
为特定用例编写的经过良好调整的 Lucene 代码将始终优于 Solr。缺点是您必须自己编写、测试和调试搜索代码的低级实现。如果这不是您的主要缺点,那么您可能想要坚持使用 Lucene。您将拥有比 Solr 所能提供的更多的功能,而且您很可能 运行 更快。
您在 Solr 邮件列表中从埃里克那里得到的回复是相关的。要获得最佳索引性能,您的客户端必须并行向 Solr 发送更新。
他提到的 ConcurrentUpdateSolrClient 是执行此操作的一种方法,但它有一个相当大的缺点——如果任何这些索引请求失败,则不会通知客户端代码。 CUSC 吞下了大多数例外。
如果您想要适当的异常处理,您将需要自己管理线程并使用 HttpSolrClient,或者如果您选择 运行 SolrCloud,则使用 CloudSolrClient。 SolrClient 实现是线程安全的。
我之前在 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 是一个通用的高度可配置的搜索服务器。 Solr 中的 Lucene 代码针对一般用途进行了调整,而不是针对特定用例。可以在配置和请求语法中进行一些调整。
为特定用例编写的经过良好调整的 Lucene 代码将始终优于 Solr。缺点是您必须自己编写、测试和调试搜索代码的低级实现。如果这不是您的主要缺点,那么您可能想要坚持使用 Lucene。您将拥有比 Solr 所能提供的更多的功能,而且您很可能 运行 更快。
您在 Solr 邮件列表中从埃里克那里得到的回复是相关的。要获得最佳索引性能,您的客户端必须并行向 Solr 发送更新。
他提到的 ConcurrentUpdateSolrClient 是执行此操作的一种方法,但它有一个相当大的缺点——如果任何这些索引请求失败,则不会通知客户端代码。 CUSC 吞下了大多数例外。
如果您想要适当的异常处理,您将需要自己管理线程并使用 HttpSolrClient,或者如果您选择 运行 SolrCloud,则使用 CloudSolrClient。 SolrClient 实现是线程安全的。