gpt4 book ai didi

java - 为什么 Lucene 找不到任何包含此代码的文档?

转载 作者:行者123 更新时间:2023-12-02 06:00:47 24 4
gpt4 key购买 nike

我正在编写这段代码,它将单个文档添加到 lucene (4.7) 索引,然后尝试通过查询文档中确实存在的术语来找到它。但indexSearcher 不返回任何文档。我的代码有什么问题吗?感谢您的评论和反馈。

String indexDir = "/home/richard/luc_index_03";
try {
Directory directory = new SimpleFSDirectory(new File(
indexDir));
Analyzer analyzer = new SimpleAnalyzer(
Version.LUCENE_47);
IndexWriterConfig conf = new IndexWriterConfig(
Version.LUCENE_47, analyzer);
conf.setOpenMode(OpenMode.CREATE_OR_APPEND);
conf.setRAMBufferSizeMB(256.0);
IndexWriter indexWriter = new IndexWriter(
directory, conf);

Document doc = new Document();
String title="New York is an awesome city to live!";
doc.add(new StringField("title", title, StringField.Store.YES));
indexWriter.addDocument(doc);
indexWriter.commit();
indexWriter.close();
directory.close();
IndexReader reader = DirectoryReader
.open(FSDirectory.open(new File(
indexDir)));
IndexSearcher indexSearcher = new IndexSearcher(
reader);


String field="title";
SimpleQueryParser qParser = new SimpleQueryParser(analyzer, field);
String queryText="New York" ;
Query query = qParser.parse(queryText);
int hitsPerPage = 100;
TopDocs results = indexSearcher.search(query, 5 * hitsPerPage);
System.out.println("number of results: "+results.totalHits);
ScoreDoc[] hits = results.scoreDocs;
int numTotalHits = results.totalHits;

for (ScoreDoc scoreDoc:hits){
Document docC = indexSearcher.doc(scoreDoc.doc);
String path = docC.get("path");
String titleC = docC.get("title");
String ne = docC.get("ne");
System.out.println(path+"\n"+titleC+"\n"+ne);
System.out.println("---*****----");

}

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

运行后我就得到了

number of results: 0

最佳答案

这是因为您使用了StringField。来自javadoc:

A field that is indexed but not tokenized: the entire String value is indexed as a single token.

只需使用 TextField 即可,应该没问题。

关于java - 为什么 Lucene 找不到任何包含此代码的文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22693197/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com