上文讲到对索引的管理,增删查改。今晚就讲讲,为索引域添加"权"了,有时在搜索的时候,会根据需要的不同,对不同的关键值或者不同的关键索引分配不同的权值,让权值高的内容更容易被用户搜索出来,而且排在前面。

为索引域添加权是再创建索引之前,把索引域的权值设置好,这样,在进行搜索时,lucene会对文档进行评分,这个评分机制是跟权值有关的,而且其它情况相同时,权值跟评分是成正相关的。

Java代码   收藏代码
  1. private String[] ids={"1","2","3","4","5","6"};  

  2. private String[] emails={"sam@163.com","holiday@163.com","issac@163.com","summer@qq.com","coco@sina.cn","roy@gmail.com"};  

  3. private String[] contents={"hello,how are you,@163.com,p,lucene,lucene","hi,I am fine!,@163.com,p,lucene","what is your name,@163.com,p,lucene","my name is summer,@qq.com,p,lucene,lucene","what is your number,@sina.cn,p,lucene,lucene","I will tell you,just wait a minute,@gmail.com,p,lucene"};  

  4. private String [] names={"sam","holiday","issac","summer","coco","roy"};  

  5. privateint[] attachs={2,3,2,4,5,7};  

  6. private Directory directory=null;  

  7. private Map<String,Float> powerScores=new HashMap<String,Float>();  

  8. public IndexUtil() throws IOException  

  9.    {  

  10.        powerScores.put("@163.com", 2.0f);  

  11.        powerScores.put("@qq.com", 1.5f);  

  12.        directory=FSDirectory.open(new File("E:/lucene/index02"));  

  13.    }  

  14. /**

  15.     * 建立索引

  16.     * @throws IOException

  17.     */

  18. publicvoid index() throws IOException  

  19.    {  

  20.        IndexWriter indexWriter=new IndexWriter(directory,new IndexWriterConfig(Version.LUCENE_43,new StandardAnalyzer(Version.LUCENE_43)));  

  21. for(int i=0;i<ids.length;i++)  

  22.        {  

  23.            Document document=new Document();  

  24.            Field contentField=new Field("content",contents[i],Field.Store.NO,Field.Index.ANALYZED);  

  25.            document.add(new Field("id",ids[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));  

  26.            document.add(new Field("email",emails[i],Field.Store.YES,Field.Index.NOT_ANALYZED));  

  27.            document.add(new Field("name",names[i],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));  

  28.            document.add(contentField);  

  29.            String contentPower=contents[i].substring(contents[i].lastIndexOf("@"),contents[i].lastIndexOf("p")-1);  

  30.            System.out.println(contentPower);  

  31. if(powerScores.containsKey(contentPower))  

  32.            {  

  33. //为索引域添加权,例如,这里的例子是,如果这个域有包含@163.com的就为2.0,如果为@qq.com的就为1.5,如果为其它的就为0.5,默认值是1.0

  34. //3.5版本有个为文档添加权的,但是现在4.3版本已经没有了

  35.                contentField.setBoost(powerScores.get(contentPower));  

  36.            }  

  37. else

  38.            {  

  39.                contentField.setBoost(0.5f);  

  40.            }  

  41.            indexWriter.addDocument(document);  

  42.        }  

  43. if(indexWriter!=null) indexWriter.close();  

  44.    }  


其实上面的代码跟上一篇索引的增删查改的代码几乎一样,笔者是在原有代码的基础上,加上一些对某个索引域权值的设置。至于测试的例子还是一样的,但是测试的结果会截然不同,笔者这里就不贴出测试结果了。


ps(纯属吐槽):因为博主白天还要上课,所以只能每天晚上抽出一点时间来弄这些文章了,之前因为有项目要做,连发文章的时间都被占有了,希望在这段时间能把这些文章快点弄好。好了,又差不多一点了,又是睡觉的节奏了,明天早上满课啊,惨啊!!