【发布时间】: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