lucene的索引建立及查找

本文通过Java代码示例介绍了如何使用Apache Lucene创建文档索引,并演示了如何进行简单查询。示例中创建了两个包含特定字段的文档,并设置最大字段长度限制。之后通过查询解析器进行搜索并打印出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Java代码 复制代码  收藏代码
  1. package lucene;   
  2.   
  3. import org.apache.lucene.analysis.standard.StandardAnalyzer;   
  4. import org.apache.lucene.document.Document;   
  5. import org.apache.lucene.document.Field;   
  6. import org.apache.lucene.index.IndexWriter;   
  7. import org.apache.lucene.queryParser.QueryParser;   
  8. import org.apache.lucene.search.Hits;   
  9. import org.apache.lucene.search.IndexSearcher;   
  10. import org.apache.lucene.search.Query;   
  11. import org.apache.lucene.store.FSDirectory;   
  12.   
  13. public class FSDirectoryTest {   
  14.   
  15.     //建立索引的路径      
  16.     public static final String path = "f://index2";   
  17.   
  18.     public static void main(String[] args) throws Exception {   
  19.         Document doc1 = new Document();   
  20.         doc1.add(new Field("name""lighter javaeye com", Field.Store.YES, Field.Index.TOKENIZED));   
  21.   
  22.         Document doc2 = new Document();   
  23.         doc2.add(new Field("name""lighter blog", Field.Store.YES, Field.Index.TOKENIZED));   
  24.   
  25.         IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);   
  26.         writer.setMaxFieldLength(3);   
  27.         writer.addDocument(doc1);   
  28.         writer.setMaxFieldLength(3);   
  29.         writer.addDocument(doc2);   
  30.         writer.close();   
  31.   
  32.         IndexSearcher searcher = new IndexSearcher(path);   
  33.         Hits hits = null;   
  34.         Query query = null;   
  35.         QueryParser qp = new QueryParser("name"new StandardAnalyzer());   
  36.   
  37.         query = qp.parse("lighter");   
  38.         hits = searcher.search(query);   
  39.         System.out.println("查找/"lighter/" 共" + hits.length() + "个结果");   
  40.   
  41.         Document doc = null;   
  42.         for (int i = 0; i < hits.length(); i++) {   
  43.             doc = hits.doc(i);   
  44.             System.out.println(doc.get("name"));   
  45.             doc = null;   
  46.         }   
  47.   
  48.         query = qp.parse("javaeye");   
  49.         hits = searcher.search(query);   
  50.         System.out.println("查找/"javaeye/" 共" + hits.length() + "个结果");   
  51.   
  52.     }   
  53.   
  54. }  

package lucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.FSDirectory;

public class FSDirectoryTest {

	//建立索引的路径   
	public static final String path = "f://index2";

	public static void main(String[] args) throws Exception {
		Document doc1 = new Document();
		doc1.add(new Field("name", "lighter javaeye com", Field.Store.YES, Field.Index.TOKENIZED));

		Document doc2 = new Document();
		doc2.add(new Field("name", "lighter blog", Field.Store.YES, Field.Index.TOKENIZED));

		IndexWriter writer = new IndexWriter(FSDirectory.getDirectory(path, true), new StandardAnalyzer(), true);
		writer.setMaxFieldLength(3);
		writer.addDocument(doc1);
		writer.setMaxFieldLength(3);
		writer.addDocument(doc2);
		writer.close();

		IndexSearcher searcher = new IndexSearcher(path);
		Hits hits = null;
		Query query = null;
		QueryParser qp = new QueryParser("name", new StandardAnalyzer());

		query = qp.parse("lighter");
		hits = searcher.search(query);
		System.out.println("查找/"lighter/" 共" + hits.length() + "个结果");

		Document doc = null;
		for (int i = 0; i < hits.length(); i++) {
			doc = hits.doc(i);
			System.out.println(doc.get("name"));
			doc = null;
		}

		query = qp.parse("javaeye");
		hits = searcher.search(query);
		System.out.println("查找/"javaeye/" 共" + hits.length() + "个结果");

	}

}

 

附件为相关JAR包

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值