Lucene入门教程(三)
来源:互联网 发布:淘宝怎么买av百度云盘 编辑:程序博客网 时间:2024/05/14 13:33
Lucene对被索引文档加权
在进入学习之前。先给大家介绍一下,什么是加权?
加权就是有时在搜索的时候,会根据需要的不同,对不同的关键值或者不同的关键索引分配不同的权值,让权值高的内容更容易被用户搜索出来,而且排在前面。
为索引域添加权是再创建索引之前,把索引域的权值设置好,这样,在进行搜索时,lucene会对文档进行评分,这个评分机制是跟权值有关的,而且其它情况相同时,权值跟评分是成正相关的。
也就是说,给谁加权,就会给谁评分,评分越高,就越排在最前面。
那么问题又来了,评分是啥呢?评分就是信息过滤,让用户快速,准确的找到其想要的结果,丰富用户体验。下图为计算公式:
q为查询语句,t是q分词后的每一项,d为去匹配的文档。
打分流程:http://www.360doc.com/content/13/0426/20/891660_281142154.shtml
代码展示(具体步骤):
1.创建一个maven项目
2.配置pom.xml,代码此处省略,如有需要,请参考Lucene入门教程(一)中的pom.xml配置文件。
3.编写代码,如下:
import java.nio.file.Paths;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.index.Term;import org.apache.lucene.search.IndexSearcher;import org.apache.lucene.search.Query;import org.apache.lucene.search.ScoreDoc;import org.apache.lucene.search.TermQuery;import org.apache.lucene.search.TopDocs;import org.apache.lucene.store.Directory;import org.apache.lucene.store.FSDirectory;import org.junit.Test;/** * 文档加权 * * 1、加权操作:给要迅速查找的东西加上权,可提升查找速度! * */public class AddDocumentReg {//写测试数据,这些数据是写到索引文档里去的。private String ids[] = {"1","2","3","4"}; //标示文档private String author[] = {"Jack","Mary","Jerry","Machech"}; private String title[] = {"java of china","Apple of china","Androw of apple the USA","People of Apple java"}; //private String contents[] = {"java of China!the world the why what","why a dertity compante is my hometown!","Jdekia ssde hhh is a beautiful city!","Jdekia ssde hhh is a beautiful java!"};private Directory dir;/** * 获取IndexWriter实例 * @return * @throws Exception */private IndexWriter getWriter()throws Exception{//实例化分析器Analyzer analyzer = new StandardAnalyzer();//实例化IndexWriterConfigIndexWriterConfig con = new IndexWriterConfig(analyzer);//实例化IndexWriterIndexWriter writer = new IndexWriter(dir, con);return writer;}/** * 生成索引(对应图一) * @throws Exception */@Test public void index()throws Exception{dir=FSDirectory.open(Paths.get("E:\\luceneDemo3"));IndexWriter writer=getWriter();for(int i=0;i<ids.length;i++){Document doc=new Document();doc.add(new StringField("id", ids[i], Field.Store.YES));doc.add(new StringField("author", author[i], Field.Store.YES));// 加权操作TextField field=new TextField("title", title[i], Field.Store.YES);if("Mary".equals(author[i])){//设权 默认为1field.setBoost(1.5f);}doc.add(field);doc.add(new StringField("contents",contents[i],Field.Store.NO));// 添加文档writer.addDocument(doc); }//关闭writerwriter.close();}/** * 查询(对应图二) * @throws Exception */@Testpublic void search()throws Exception{//得到读取索引文件的路径dir = FSDirectory.open(Paths.get("E:\\luceneDemo3"));//通过dir得到的路径下的所有的文件IndexReader reader = DirectoryReader.open(dir);//建立索引查询器IndexSearcher searcher = new IndexSearcher(reader);//查找的范围String searchField = "title";//查找的字段String q = "apple";//运用term来查找Term t = new Term(searchField,q);//通过term得到query对象Query query = new TermQuery(t);//获得查询的hitsTopDocs hits = searcher.search(query, 10);//显示结果System.out.println("匹配 '"+q+"',总共查询到"+hits.totalHits+"个文档");//循环得到文档,得到文档就可以得到数据for(ScoreDoc scoreDoc:hits.scoreDocs){Document doc=searcher.doc(scoreDoc.doc);System.out.println(doc.get("author"));}//关闭readerreader.close();}}
图一:见下图说明成功!
图二:代码中匹配的是:apple,4个文档里3个都有,验证一下到底是不是,看下图:
大家看 author的这个数组,Mary、Jerry、Machech对应的title是不是有apple这个关键字。而且是不区分大小写的。哪有同学要问了,问什么它就是把索引为title的查出来了,为什么不差别的呢?这是因为在代码中,有注释为“//查找的范围”的代码:StringsearchField = "title";因为它规定了只在索引为title范围里找符合条件的元素。当然换成别的也是可以实现的。
- Lucene入门教程(三)
- Lucene入门教程(一)
- Lucene入门教程(一)
- Lucene入门教程(一)
- Lucene入门教程(二)
- Lucene入门教程
- Lucene入门教程
- Lucene入门教程
- Lucene入门教程
- Lucene入门教程
- Lucene入门教程
- Lucene入门教程
- Lucene入门教程
- lucene入门教程
- Lucene入门教程
- Lucene入门教程
- Lucene入门教程
- Lucene入门教程
- 微信小程序把玩(六)模块化
- Linux连接锐捷校园网客户端
- socket编程对端意外断开后的结果
- ButterKnife8.4的错误解决
- C语言中内存分配
- Lucene入门教程(三)
- Linux网络流量实时监控ifstat iftop命令详解
- [English]The path of English paper
- java常见排序
- 样式与include
- CSS实战技巧:图片(大小不固定)的水平垂直居中
- mxnet sample
- 获取手机存储空间大小
- 记一次百万级数据误更新操作后的恢复处理