- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试使用多线程在 Lucene 中构建索引。因此,我开始编码并编写了以下代码。首先,我找到文件并为每个文件创建一个线程来为其编制索引。之后我加入线程并优化索引。它有效,但我不确定......我可以大规模信任它吗?有什么办法可以改善吗?
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Document;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.StopAnalyzer;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.apache.lucene.index.TermFreqVector;
public class mIndexer extends Thread {
private File ifile;
private static IndexWriter writer;
public mIndexer(File f) {
ifile = f.getAbsoluteFile();
}
public static void main(String args[]) throws Exception {
System.out.println("here...");
String indexDir;
String dataDir;
if (args.length != 2) {
dataDir = new String("/home/omid/Ranking/docs/");
indexDir = new String("/home/omid/Ranking/indexes/");
}
else {
dataDir = args[0];
indexDir = args[1];
}
long start = System.currentTimeMillis();
Directory dir = FSDirectory.open(new File(indexDir));
writer = new IndexWriter(dir,
new StopAnalyzer(Version.LUCENE_34, new File("/home/omid/Desktop/stopwords.txt")),
true,
IndexWriter.MaxFieldLength.UNLIMITED);
int numIndexed = 0;
try {
numIndexed = index(dataDir, new TextFilesFilter());
} finally {
long end = System.currentTimeMillis();
System.out.println("Indexing " + numIndexed + " files took " + (end - start) + " milliseconds");
writer.optimize();
System.out.println("Optimization took place in " + (System.currentTimeMillis() - end) + " milliseconds");
writer.close();
}
System.out.println("Enjoy your day/night");
}
public static int index(String dataDir, FileFilter filter) throws Exception {
File[] dires = new File(dataDir).listFiles();
for (File d: dires) {
if (d.isDirectory()) {
File[] files = new File(d.getAbsolutePath()).listFiles();
for (File f: files) {
if (!f.isDirectory() &&
!f.isHidden() &&
f.exists() &&
f.canRead() &&
(filter == null || filter.accept(f))) {
Thread t = new mIndexer(f);
t.start();
t.join();
}
}
}
}
return writer.numDocs();
}
private static class TextFilesFilter implements FileFilter {
public boolean accept(File path) {
return path.getName().toLowerCase().endsWith(".txt");
}
}
protected Document getDocument() throws Exception {
Document doc = new Document();
if (ifile.exists()) {
doc.add(new Field("contents", new FileReader(ifile), Field.TermVector.YES));
doc.add(new Field("path", ifile.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));
String cat = "WIR";
cat = ifile.getAbsolutePath().substring(0, ifile.getAbsolutePath().length()-ifile.getName().length()-1);
cat = cat.substring(cat.lastIndexOf('/')+1, cat.length());
//doc.add(new Field("category", cat.subSequence(0, cat.length()), Field.Store.YES));
//System.out.println(cat.subSequence(0, cat.length()));
}
return doc;
}
public void run() {
try {
System.out.println("Indexing " + ifile.getAbsolutePath());
Document doc = getDocument();
writer.addDocument(doc);
} catch (Exception e) {
System.out.println(e.toString());
}
}
}
任何hep都被认为是。
最佳答案
如果你想并行索引,你可以做两件事:
您正走在并行调用 addDocuments 的正确道路上,但是为每个文档生成一个线程不会随着您需要索引的文档数量的增长而扩展。您应该使用固定大小的 ThreadPoolExecutor .由于此任务主要是 CPU 密集型任务(取决于您的分析器和检索数据的方式),因此将计算机的 CPU 数量设置为最大线程数可能是一个好的开始。
关于合并调度程序,您可以增加可用于 setMaxThreadCount method of ConcurrentMergeScheduler 的最大线程数.请注意,磁盘在顺序读/写方面比随机读/写要好得多,因此为合并调度程序设置过高的最大线程数更有可能减慢索引速度而不是加快索引速度。
但在尝试并行化您的索引编制过程之前,您可能应该尝试找出瓶颈所在。如果您的磁盘太慢,瓶颈很可能是刷新和合并步骤,因此并行调用 addDocument(本质上包括分析文档并将分析结果缓冲在内存中)不会提高索引速度完全没有。
一些旁注:
Lucene 的开发版本正在进行一些工作以提高索引并行性(特别是刷新部分,blog entry 解释了它是如何工作的)。
Lucene 在 how to improve indexing speed 上有一个不错的 wiki 页面您可以在其中找到提高索引速度的其他方法。
关于java - 使用 lucene 改进多线程索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9317981/
Lucene 对俄语的支持很差。 RussianAnalyzer(lucene-contrib 的一部分)质量非常低。 Snowball 的 RussianStemmer 模块更糟糕。它不识别 Uni
我需要使用 Lucene 对 Books 数据库进行多字段级别搜索。 例如:我的搜索条件类似于: (Author:a1 and title:t1) OR (Author:a2 and title:t
在搜索一堆文档时,我可以轻松找到符合我的搜索条件的文档数量: Hits hits = Searcher.Search(query); int DocumentCount = hits.Length()
我正在向 Lucene 索引添加数十亿行,每行几乎是 6000 字节。可以添加到 Lucene 索引的最大行数是否有限制? Lucene 索引上 6000 字节的十亿行将占用多少空间。这个尺寸有限制吗
如果我编写使用 Lucene 执行搜索的算法,我该如何说明它的计算复杂度?我知道 Lucene 使用 tf*idf 评分,但我不知道它是如何实现的。我发现 tf*idf 具有以下复杂性: O(|D|+
我想在索引中搜索特殊字符。 我转义了查询字符串中的所有特殊字符,但是当我在索引中的 lucene 上执行查询为 + 时,它会创建查询为 +()。 因此它不搜索任何字段。 如何解决这个问题呢?我的索引包
我不明白它们是什么,我真的很感激一个简单的解释,展示它们给世界带来的值(value),而没有太多关于它们如何工作的实现细节。 最佳答案 范数是计算分数的一部分。可以根据您的喜好计算标准,真的。使规范与
这可以被视为一般 Java 问题,但为了更好地理解,我使用 Lucene 作为示例。 您可以在 Lucene 中使用不同的分词器来分词文本。有主要的抽象 Tokenizer 类,然后是许多扩展它的不同
我必须索引应该一起搜索的不同类型的数据(文本文档、论坛消息、用户配置文件数据等)(即,单个搜索将返回不同类型数据的结果)。 拥有多个索引(每种类型的数据一个)的优缺点是什么? 以及对各种数据使用单一索
我使用Lucene.Net为一些文档建立索引。我想向用户展示几行有关为什么该文档出现在结果集中的信息。就像您使用Google进行搜索一样,它会显示链接,然后是链接,其中有几行带有突出显示的关键字。 有
Lucene 中的段是什么? 分段有什么好处? 最佳答案 Lucene 索引被分割成更小的 block ,称为段。每个段都有自己的索引。 Lucene 按顺序搜索所有这些。 当新的写入器打开以及写入器
我想了解 lucene 搜索如何运行得如此之快。我在网上找不到任何有用的文档。如果您有任何内容(除了 lucene 源代码)需要阅读,请告诉我。 在我的例子中,使用带索引的 mysql5 文本搜索进行
有人可以解释一下 Lucene 中不同分析器之间的区别吗?我收到 maxClauseCount 异常,我知道可以通过使用 KeywordAnalyzer 来避免这种情况,但我不想在不了解分析器周围问题
显然它不能用来破坏索引或破解卡号、密码等(除非有人愚蠢到将卡号或密码放入索引中)。 是否有可能因过于复杂的搜索而导致服务器瘫痪? 我想我真正需要知道的是我是否可以将用户输入的 Lucene 查询直
我已经索引了 400 个文档。然后我想给两个文档和 lucene 返回这两个文档之间的相似度。那可能吗?提前致谢。 最佳答案 简而言之。计算两个文档向量的余弦。 example code 关于luce
我正在考虑/致力于为我们公司的各种内容类型实现一个搜索引擎,并尝试着迷于 Lucene(特别是 .net 风格)。 目前,我的主要问题是索引的文档是否必须包含相同的字段。 例如: 文档1: 标题:“我
我对 Lucene 的评分功能有一个问题,我无法弄清楚。到目前为止,我已经能够编写这段代码来重现它。 package lucenebug; import java.util.Arrays; impor
我需要建立该矩阵,但找不到用于为每个单元格计算归一化tf-idf的方法。 我要执行的归一化是余弦归一化,将tf-idf(使用DefaultSimilarity计算)除以1 / sqrt(列中的sumO
有意义吗? 对于我的客户来说,开发克罗地亚语分析器太昂贵了,我没有找到任何现有的分析器...所以我的问题是...我是否告诉他们放弃使用 Lucene 来处理克罗地亚语内容的想法? 谢谢! 最佳答案 S
频繁更新 lucene 索引(每隔几秒)可以吗?更新将同样是添加,更新和搜索将同时发生。 最佳答案 我将在这个答案前面加上“我只使用过 Java Lucene”,但这应该仍然适用:从一般意义上讲,按照
我是一名优秀的程序员,十分优秀!