检索的得分相当重要,这关乎你的搜索结果排名,因为百度搜索的东西特别多好多页,用户根本不可能会一直看到100页,也就会第一页和后几页,在访问量为王的的互联网时代,排名相对重要。lucene对于得分有一套自己的算法,当然也可以人工干预,比如给钱。提高得分现在比较大的就是在贴吧多发贴等等,其实最实在的就是你给百度钞票,给了钞票越多你的搜索结果排名越靠前,本文对于lucene的得分做一个简单的介绍
1:用到的工具类是前几篇lucene的,本文不再展示
//内容一样,搜索关键字一样,得分也是一样的..
for(int i=0;i<25;i++){//添加数据
Article article = new Article();
article.setId(i+1);
article.setAuthor("毕加索");
article.setTitle("我的画天下第一");
article.setLink("www.qqw.com");
article.setContent("java是世界上最好的语言");
LuceneDao luceneDao = new LuceneDao();
luceneDao.addIndex(article);
}先插入25条记录,
IndexSearcher indexSearcher=LuceneUtil.getIndexSearcher();
//需要根据那几个字段进行检索...
String fields []={"title"};//根据标题
QueryParser queryParser=new MultiFieldQueryParser(LuceneUtil.getMatchVersion(),fields,LuceneUtil.getAnalyzer());
Query query=queryParser.parse("画");//查询画字
//检索符合query 前面N条记录...
TopDocs topDocs=indexSearcher.search(query, 100);
ScoreDoc scoreDocs []=topDocs.scoreDocs;
for(ScoreDoc scoreDoc:scoreDocs){
//VSM
Document document=indexSearcher.doc(scoreDoc.doc);
System.out.println("文档编号,id"+document.get("id") +"====得分===="+scoreDoc.score);
}
查看得分:得分一样
再插入一条记录
for(int i=25;i<26;i++){//添加数据
Article article = new Article();
article.setId(i+1);
article.setAuthor("毕加索");
article.setTitle("画画画画画画");//画字多
article.setLink("www.qqw.com");
article.setContent("java是世界上最好的语言");
LuceneDao luceneDao = new LuceneDao();
luceneDao.addIndex(article);
}
查看得分:是26条记录中最高的
可见搜索的关键字越多越全,得分最高。
//.............可以单独修改某条索引的权重,这样也可以提高分数,这个得搜索引擎公司内部修改,比如你给钱,就提高你的索引权重
在介绍lucene的CRUD那篇文章有个工具类,修改一下他,
public static Document articleToDocument(Article article){
Document document=new Document();
IntField idfield=new IntField("id",article.getId(),Store.YES);
StringField authorfield=new StringField("author", article.getAuthor(), Store.YES);
StringField urlfield=new StringField("link", article.getLink(), Store.YES);
TextField title=new TextField("title", article.getTitle(),Store.YES);
//设置权重值,默认为1f..
//title.setBoost(4f);
TextField contentfield=new TextField("content", article.getContent(),Store.YES);
document.add(idfield);
document.add(authorfield);
document.add(urlfield);
document.add(title);
document.add(contentfield);
return document;
}
再添加一条记录,这条记录标题和前面一样,可是得分会是前面的4倍
至此,lucene检索的得分介绍完毕