Lucene入门教程(三)

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();
					
			//实例化IndexWriterConfig
			IndexWriterConfig con = new IndexWriterConfig(analyzer);
					
			//实例化IndexWriter
			IndexWriter 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])){
					
					//设权 默认为1
					field.setBoost(1.5f);
				}
				
				doc.add(field);
				doc.add(new StringField("contents",contents[i],Field.Store.NO));
				
				// 添加文档
				writer.addDocument(doc); 
			}
			
			//关闭writer
			writer.close();
		}

		/**
		 * 查询(对应图二)
		 * @throws Exception
		 */
		@Test
		public 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);
			
			//获得查询的hits
			TopDocs 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"));
			}
			
			//关闭reader
			reader.close();
		}

}

图一:见下图说明成功!


图二:代码中匹配的是:apple,4个文档里3个都有,验证一下到底是不是,看下图:


大家看 author的这个数组,MaryJerryMachech 对应的title 是不是有apple这个关键字。而且是不区分大小写的。哪有同学要问了,问什么它就是把索引为title的查出来了,为什么不差别的呢?这是因为在代码中,有注释为 //查找的范围的代码:StringsearchField = "title";因为它规定了只在索引为title范围里找符合条件的元素。当然换成别的也是可以实现的。



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值