problem description
when learning lucene, we encounter the problem of setting weights. Using lucene version 5.3.1, calling api setBoost () is invalid. No matter how you set it, the query result is tz zs lh, to tell you why
.related codes
private static String[]ids={"1","2","3"};
private static String[]authors={"tz","zs","lh"};
private static String titles[]={"Java is a good language.","Java is a cross platform language","You should learn java"};
private static String contents[]={
"If possible, use the same JRE major version at both index and search time.",
"When upgrading to a different JRE major version, consider re-indexing. ",
"Different JRE major versions may implement different versions of Unicode,",
"For example: with Java 1.4, `LetterTokenizer` will split around the character U+02C6,"
};
public static void main(String[] args) {
try {
Directory dir=FSDirectory.open(Paths.get("F:/Computer World/luceneRepository"));
IndexWriter iw=new IndexWriter(dir, new IndexWriterConfig(new StandardAnalyzer()));
for(int i=0;i<authors.length;iPP) {
Document doc=new Document();
doc.add(new StringField("id", ids[i], Field.Store.YES));
doc.add(new TextField("title", titles[i],Store.YES));
TextField textField = new TextField("author", authors[i],Store.YES);
if("zs".equals(authors[i])){
textField.setBoost(4f);
}
doc.add(textField);
doc.add(new TextField("content", contents[i],Store.YES));
iw.addDocument(doc);
}
iw.close();
} catch (IOException e) {
// TODO catch
e.printStackTrace();
}
}
FSDirectory dir;
try {
dir = FSDirectory.open(Paths.get("F:/Computer World/luceneRepository"));
IndexReader ir = DirectoryReader.open(dir);
IndexSearcher indexSearcher=new IndexSearcher(ir);
Term t=new Term("title","java");
Query query=new TermQuery(t);
TopDocs hits=indexSearcher.search(query, 10);
System.out.println(""+hits.totalHits+"");
for(ScoreDoc scoreDoc:hits.scoreDocs){
Document doc=indexSearcher.doc(scoreDoc.doc);
System.out.println(doc.get("author")+":"+scoreDoc.score);
}
ir.close();
} catch (IOException e) {
e.printStackTrace();
}