使用Lucene对文档进行评级 使用Lucene对文档进行评级
我是Lucene的新手,我将使用tf idf算法对一些文档进行评分。我的文档包含以下字段: 一、 T,A,B,W 我的使用Lucene对文档进行评级 使用Lucene对文档进行评级,lucene,information-retrieval,rating,Lucene,Information Retrieval,Rating,我是Lucene的新手,我将使用tf idf算法对一些文档进行评分。我的文档包含以下字段: 一、 T,A,B,W 我的索引功能: private void indexFile(File f) throws Exception { Document doc = getDocument(f); writer.addDocument(doc); } MygetDocument功能: protected Document getDocument(File f) throws Excepti
索引
功能:
private void indexFile(File f) throws Exception {
Document doc = getDocument(f);
writer.addDocument(doc);
}
MygetDocument
功能:
protected Document getDocument(File f) throws Exception {
String content = fileReader(f.getPath());
...
Document doc = new Document();
doc.add(new StringField("I", content.substring(I + 3, T - 1), Field.Store.YES));
doc.add(new StringField("T", content.substring(T + 3, A - 1), Field.Store.YES));
doc.add(new StringField("A", content.substring(A + 3, B - 1), Field.Store.YES));
doc.add(new StringField("B", content.substring(B + 3, W - 1), Field.Store.YES));
doc.add(new StringField("W", content.substring(W + 3, content.length() - 1), Field.Store.YES));
return doc;
}
public void search(int queryNumber, String q) throws IOException, ParseException {
IndexReader rdr = DirectoryReader.open(FSDirectory.open(Paths.get(INDEX_DIRECTORY)));
IndexSearcher is = new IndexSearcher(rdr);
QueryParser parser = new QueryParser("W", new StandardAnalyzer());
parser.setAllowLeadingWildcard(true);
Query query = parser.parse(q);
TopDocs hits = is.search(query, 20);
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);
System.out.println(queryNumber + " " + doc.get("I"));
}
}
我的搜索
功能:
protected Document getDocument(File f) throws Exception {
String content = fileReader(f.getPath());
...
Document doc = new Document();
doc.add(new StringField("I", content.substring(I + 3, T - 1), Field.Store.YES));
doc.add(new StringField("T", content.substring(T + 3, A - 1), Field.Store.YES));
doc.add(new StringField("A", content.substring(A + 3, B - 1), Field.Store.YES));
doc.add(new StringField("B", content.substring(B + 3, W - 1), Field.Store.YES));
doc.add(new StringField("W", content.substring(W + 3, content.length() - 1), Field.Store.YES));
return doc;
}
public void search(int queryNumber, String q) throws IOException, ParseException {
IndexReader rdr = DirectoryReader.open(FSDirectory.open(Paths.get(INDEX_DIRECTORY)));
IndexSearcher is = new IndexSearcher(rdr);
QueryParser parser = new QueryParser("W", new StandardAnalyzer());
parser.setAllowLeadingWildcard(true);
Query query = parser.parse(q);
TopDocs hits = is.search(query, 20);
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);
System.out.println(queryNumber + " " + doc.get("I"));
}
}
问题是我的搜索函数,它总是为所有查询返回0命中文档。我的错在哪里
我的另一个问题是“我如何更改和修改
tf idf
算法?StringField
未被分析。如果应该分析您的字段,请使用TextField
。是的,最后我使用这行代码,问题得到解决doc.add(new TextField(“W”,new FileReader(f)));
我的问题的第二部分,我要更改评级算法并使用另一个,我该怎么做?@femtoRgon