- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目前,我可以将文档列表以及单个文档添加到 apache lucene 索引中。但我在从索引更新文档时遇到了问题:
我的方法是在文件上传后立即进行,因此在写入磁盘之前,我会检查驱动器/文件夹中是否存在文件,并根据文件名删除索引。
其次,我将上传的文件添加到 Lucene 索引中。
但是我遇到的问题是新添加的文档和旧文档都以不同的内容显示在搜索结果中。
例如:文件名为 Sample_One.txt,文本为:
This is the sample text for first time.
从索引中删除上述文件,然后将新文件内容添加到索引中。
现在文件内容已更新为具有相同文件名的另一个文本:
This is the sample text with updated content.
在搜索诸如“sample”之类的文本时,结果显示 Sample_One.txt 文件两次,其中包含旧内容和新内容。
我想知道我是否遗漏了某些内容以及如何将文档更新/删除到索引中。
代码片段是:
//Deleting the Document from the Index
public void deleteDocumentsFromIndexUsingTerm(Document doc) throws IOException, ParseException {
Term fileTerm = new Term("file_name",doc.get("file_name"));
Term contentTerm = new Term("content", doc.get("content"));
Term docIDTerm = new Term("document_id", doc.get("document_id"));
File indexDir = new File(INDEX_DIRECTORY);
Directory directory = FSDirectory.open(indexDir.toPath());
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter indexWriter = new IndexWriter(directory, conf);
System.out.println("Deleting the term with - "+doc.get("file_name"));
System.out.println("Deleting the term with contents - "+doc.get("content"));
indexWriter.deleteDocuments(fileTerm);
indexWriter.deleteDocuments(contentTerm);
indexWriter.deleteDocuments(docIDTerm);
indexWriter.commit();
indexWriter.close();
}
//将文档添加到索引的代码段
final String INDEX_DIRECTORY = "D:\\Development\\Lucene_Indexer";
long startTime = System.currentTimeMillis();
List<ContentHandler> contentHandlerList = new ArrayList<ContentHandler>();
String fileNames = (String)request.getAttribute("message");
File file = new File("D:\\Development\\Resume_Sample\\"+fileNames);
ArrayList<File> fileList = new ArrayList<File>();
fileList.add(file);
Metadata metadata = new Metadata();
// BodyContentHandler set the value as -1 to evade the Text Limit Exception
ContentHandler handler = new BodyContentHandler(-1);
ParseContext context = new ParseContext();
Parser parser = new AutoDetectParser();
InputStream stream = new FileInputStream(file);
try {
parser.parse(stream, handler, metadata, context);
contentHandlerList.add(handler);
}catch (TikaException e) {
e.printStackTrace();
}catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally {
try {
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
FieldType fieldType = new FieldType();
fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS);
fieldType.setStoreTermVectors(true);
fieldType.setStoreTermVectorPositions(true);
fieldType.setStoreTermVectorPayloads(true);
fieldType.setStoreTermVectorOffsets(true);
fieldType.setStored(true);
Analyzer analyzer = new StandardAnalyzer();
Directory directory = FSDirectory.open(new File(INDEX_DIRECTORY).toPath());
IndexWriterConfig conf = new IndexWriterConfig(analyzer);
IndexWriter writer = new IndexWriter(directory, conf);
Iterator<ContentHandler> handlerIterator = contentHandlerList.iterator();
Iterator<File> fileIterator = fileList.iterator();
while (handlerIterator.hasNext() && fileIterator.hasNext()) {
Document doc = new Document();
String text = handlerIterator.next().toString();
String textFileName = fileIterator.next().getName();
String idOne = UUID.randomUUID().toString();
Field idField = new Field("document_id",idOne,fieldType);
Field fileNameField = new Field("file_name", textFileName, fieldType);
Field contentField = new Field("content",text,fieldType);
doc.add(idField);
doc.add(contentField);
doc.add(fileNameField);
writer.addDocument(doc);
analyzer.close();
}
writer.commit();
writer.deleteUnusedFiles();
long endTime = System.currentTimeMillis();
writer.close();
首先,我会在文件上传后立即删除文档,然后对更新的文档建立索引。
最佳答案
问题是您的字段在编入索引时会被分析,但您尝试删除的术语不会被分析。
最好的解决方案是将您想要用作此目的标识符的字段设置为 StringField
,这将导致它在不进行分析的情况下被索引。如:
Field idField = new StringField("document_id", idOne);
doc.add(idField);
或者,您可以使用 IndexWriter.deleteDocuments(Query...)
,并传入一个经过分析的查询(由 QueryParser 生成),但在这种情况下,您应该小心,不要删除比预期更多的文档(查询找到的任何文档都将被删除,而不是只是最好的结果)。
关于java - 如何删除或更新 apache Lucene 中的文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39292911/
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”,但这应该仍然适用:从一般意义上讲,按照
我是一名优秀的程序员,十分优秀!