Java 为什么';t Lucene是否找到具有此代码的文档?
我正在编写这段代码,它将一个文档添加到lucene(4.7)索引中,然后通过查询文档中确实存在的术语来尝试查找它。但indexSearcher不返回任何文档。我的代码有什么问题?感谢您的评论和反馈Java 为什么';t Lucene是否找到具有此代码的文档?,java,search,full-text-search,lucene,Java,Search,Full Text Search,Lucene,我正在编写这段代码,它将一个文档添加到lucene(4.7)索引中,然后通过查询文档中确实存在的术语来尝试查找它。但indexSearcher不返回任何文档。我的代码有什么问题?感谢您的评论和反馈 String indexDir = "/home/richard/luc_index_03"; try { Directory directory = new SimpleFSDirectory(new File( indexDir));
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:
索引但未标记化的字段:整个字符串值作为单个标记进行索引
只需使用TextField
即可