1、介绍
lucene可以对指定索引的字段修改搜索权重,使权重大的索引优先显示
2、代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| @Test public void TestBoot() throws Exception { IndexWriter writer = getIndexWriter(indexDir); Document document = new Document(); Field nameField = new TextField("name", "hello world", Field.Store.YES); nameField.setBoost(1.5F); document.add(nameField); document.add(new TextField("describes", "测试luence", Field.Store.YES)); writer.addDocument(document); writer.close(); } @Test public void TestSearch() throws Exception { IndexReader reader = getIndexReader(); IndexSearcher searcher = new IndexSearcher(reader); QueryParser parser = new QueryParser("name", getAnalyzer()); Query query = parser.parse("hello"); TopDocs hits = searcher.search(query, 10); for (ScoreDoc sd : hits.scoreDocs) { Document doc = searcher.doc(sd.doc); System.out.println("name:" + doc.get("name") + ",评分:"+sd.score); } reader.close(); }
|