【概述】
在数学领域,权值指加权平均数中的每个数的频数,也称为权数或权重。在搜索引擎中,权值越高的内容在排序中越靠前。
实际应用中可以通过修改权值来重新调整索引在列表中的排序位置。
【示例】
1 /** 2 * 创建索引 3 */ 4 @Test 5 public void testIndex(){ 6 //存储3篇文章的信息。包括:id、标题、作者、内容 7 int[] ids = {1,2,3}; 8 String[] titles = {"Hello","I love you","morning"}; 9 String[] authors = {"Hello Mike","HanMeimei","Tom"}; 10 String[] contents= {"Hello,My Name Is Mike; good","Tome,I Love You; good,good,good","Good Moring,I'm so sorry; good,,good,good,good,good"}; 11 12 IndexWriter writer = null; 13 14 try { 15 //1、创建Directory 16 Directory directory = FSDirectory.open(new File("E:\\lucene\\index2")); 17 18 //2、创建Writer 19 ...... 20 21 for(int i = 0;i<ids.length;i++){ 22 doc = new Document(); 23 24 //为文档添加域(属性) 25 String id = Integer.toString(ids[i]); 26 doc.add(new Field("id",id,Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS)); 27 doc.add(new Field("title",titles[i],Field.Store.YES,Field.Index.NOT_ANALYZED)); 28 doc.add(new Field("author",authors[i],Field.Store.YES,Field.Index.NOT_ANALYZED)); 29 doc.add(new Field("content", contents[i],Field.Store.NO,Field.Index.ANALYZED)); 30 31 //修改索引的权值 32 //默认权值为1.0 33 //将Id为1的数据权值调整到5.0 34 if(ids[i] == 1) 35 doc.setBoost(5.0f); 36 37 writer.addDocument(doc); 38 } 39 } catch (IOException e) { 40 e.printStackTrace(); 41 } 42 finally{ 43 if(writer != null) 44 try { 45 writer.close(); 46 } catch (CorruptIndexException e) { 47 // TODO Auto-generated catch block 48 e.printStackTrace(); 49 } catch (IOException e) { 50 // TODO Auto-generated catch block 51 e.printStackTrace(); 52 } 53 } 54 }
注意:读取索引时读取到的权值和索引中的权值是不同的。因为他们属于不同的doc对象。