Lucene的入门例子

本文内容:
1.lucene的基本步骤(配代码与jar包版本)
2.示例的代码
3.完成示例后再该延伸到哪些知识




1.lucene的基本步骤(配代码与jar包版本)

jar包版本:

       一  ,  从数据库中查数据 ====爬数据  -------------1


   public ArrayList<BaseItem> getDate(String sql) throws SQLException {
  ArrayList<BaseItem> item = new ArrayList<BaseItem>();
  ConnBase dataConn = new ConnBase();//数据库连接
  conn = dataConn.DBconn();
  ps = conn.prepareStatement(sql);
  rs = ps.executeQuery();
  // jdbcTemplate.execute(sql);
  while (rs.next()) {
   BaseItem i = new BaseItem();
   i.setTitle(rs.getString("title")); // 对应你的Blog表里的title
   i.setContent(rs.getString("content")); // 取表里的博客内容
   i.setUr("SingleArticle_lucene.action?id=" + rs.getInt("blogId")); // 如 a. action ?id=8
   item.add(i);
  } // 把数据库里的数据取出来
  return item;
 }


   二 , // 建立索引存放的位置 ------本方法是创建 在C盘--------------------2

 public void CreateFileIndex(String dir) {
  try {
   /* 这里放索引文件的位置 */
   File indexDir = new File("c:\\" + dir); // 存放 检索文件的路径
   if (!indexDir.exists()) {
    indexDir.mkdirs();
   }
   // 创建标准文本分析器, 标准的是可以支持的中文的

   Analyzer luceneAnalyzer = new StandardAnalyzer();
   indexWriter = new IndexWriter(indexDir, luceneAnalyzer, true);
   // 可以说是创建一个新的写入工具
   // 第一个参数是要索引建立在哪个目录里
   // 第二个参数是新建一个文本分析器,这里用的是标准的大家也可以自己写一个
   // 第三个参数如果是true,在建立索引之前先将c: \\index目录清空
   indexWriter.setMaxFieldLength(100000);
   indexWriter.optimize();
  } catch (IOException e) {
   System.out.println("建立索引失败!!!");
   e.printStackTrace();
  }
 }




三 , // 添加数据到索引里去-----------------3

 public String createIndex(String title, String url, String content) {
  try {
   
   // 增加document到索引去
   // document对象,相当于数据库中一条记录
   Document document = new Document();
   // Field对象,相当于数据库中字段
   Field FiledTitle = new Field("title", title, Field.Store.YES,
     Field.Index.ANALYZED);// Field.Index.ANALYZED 这就能进行索引了, 如果设置为NO的话就不能检索
   Field FiledContent = new Field("content", content, Field.Store.YES,
     Field.Index.ANALYZED);
   Field FieldBody = new Field("url", url, Field.Store.YES,
     Field.Index.NO); 
   document.add(FieldBody);
   document.add(FiledContent);
   document.add(FiledTitle);
   indexWriter.addDocument(document);
  } catch (IOException e) {
   e.printStackTrace();
   
   return "建立索引失败!!!!!!";
  }
  return "建立索引成功!!!!!!!";
 }



四 , // 关闭索引================================== 4

 public void close() throws IOException {
  this.indexWriter.close();    // 这里非常的重要,不关闭直接导致你的索引创建不成功
 }



五  , // 查询索引的方法 ===============================5

 public ArrayList<Document> getQueryDate(String info)
   throws CorruptIndexException, IOException,
   org.apache.lucene.queryParser.ParseException {
  ArrayList<Document> doc = new ArrayList<Document>();
  String queryString = info;
//   Hits hits = null;
//   Query query = null;
//   QueryParser qp = null;
//   String dir = "c:\\hujiong"; // 一定要跟你建索引的位置 一致
//  
//   // 建立索引检索对象
//   IndexSearcher searcher = new IndexSearcher(dir);
//   // 分词器
//   Analyzer analyzer = new StandardAnalyzer();
//   qp = new QueryParser("content", analyzer);// 这里上面只写了一个按Content查找.
//   下面添加的是title, 查找
//   query = qp.parse(queryString);
//  
//   if (searcher != null) {
//   hits = searcher.search(query);
//   doc = new ArrayList<Document>();
//   for (int i = 0; i < hits.length(); i++) {
//   doc.add(hits.doc(i));
//   }
//   }

  IndexSearcher searcher = new IndexSearcher("c:\\hujiong");
  Analyzer analyzer = new StandardAnalyzer();
  Query query = null;
  if (searcher != null) {
   // 合并你搜索的字段, 增强你的搜索能力!!
   BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD,
     BooleanClause.Occur.SHOULD };
   query = MultiFieldQueryParser.parse(queryString, new String[] {
     "title", "content" }, clauses, analyzer);  // 这里就是在两个范围内进行收索 , 不过这些索引的字段必须要在添加数据到索引的时候设置它
   
   TopDocCollector collector = new TopDocCollector(5); // 设置返回的最大数目,就返回前100条
   
   searcher.search(query, collector);
   ScoreDoc[] hits1 = collector.topDocs().scoreDocs;
           // 返回的结果他是一个数组
   if (hits1.length > 0) {
    for (int i = 0; i < hits1.length; i++) {
     Document doc1 = searcher.doc(hits1[i].doc); 
               // 这是从这个返回的数组里面迭代每一个数据,  它的值是Document
     doc.add(doc1);
     System.out.println(doc1.get("title") + "-----title");
     System.out.println(doc1.get("content") + "-------content");
    }
   } else {
    System.out.println("没有数据");
   }
  }
  return doc;

 }


2.示例的代码


package phz;
 
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.MultiFieldQueryParser;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MultiSearcher;
import org.apache.lucene.search.Query;
 
/**
 * 这个实例包含了lucene所有核心用法
 *
 * @author panhuizi
 *
 */
public class LuceneTest {
 
    public static void main(String[] args) {
        try {
            LuceneTest luceneTest = new LuceneTest();
            // 创建索引
            luceneTest.index();
            // 在索引所在目录下搜索"中国 金牌"
            luceneTest.search("中国 金牌");
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("ok");
    }
 
    public void index() throws Exception {
        /* 创建索引初始化,执行这些语句将创建或清空d:\\save\\目录下所有索引 */
        IndexWriter writer1 = new IndexWriter("d:\\save\\",
                new StandardAnalyzer(), true);
        writer1.close();
 
        /*
         * 往创建的初始化索引中添加索引内容,StandardAnalyzer表示用lucene自带的标准分词机制,
         * false表示不覆盖原来该目录的索引,细心的读者可能已经发现, 这句话和上面的那句就这个false不一样
         */
        IndexWriter writer2 = new IndexWriter("d:\\save\\",
                new StandardAnalyzer(), false);
        /* 创建一份文件 */
        Document doc1 = new Document();
        /*
         * 创建一个域ArticleTitle,并往这个域里面添加内容 "Field.Store.YES"表示域里面的内容将被存储到索引
         * "Field.Index.TOKENIZED"表示域里面的内容将被索引,以便用来搜索
         */
        Field field1 = new Field("ArticleTitle", "北京2008年奥运会", Field.Store.YES,
                Field.Index.TOKENIZED);
        /* 往文件里添加这个域 */
        doc1.add(field1);
        /* 同理:创建另外一个域ArticleText,并往这个域里面添加内容 */
        Field field2 = new Field("ArticleText", "这是一届创造奇迹、超越梦想的奥运会.......",
                Field.Store.YES, Field.Index.TOKENIZED);
        doc1.add(field2);
        // 在这里还可以添加其他域
        /* 添加这份文件到索引 */
        writer2.addDocument(doc1);
 
        /* 同理:创建第二份文件 */
        Document doc2 = new Document();
        field1 = new Field("ArticleTitle", "中国获得全球赞誉", Field.Store.YES,
                Field.Index.TOKENIZED);
        doc2.add(field1);
        field2 = new Field("ArticleText", "中国所取得的金牌总数排行榜的榜首........",
                Field.Store.YES, Field.Index.TOKENIZED);
        doc2.add(field2);
 
        writer2.addDocument(doc2);
 
        // 在这里可以添加其他文件
 
        /* 关闭 */
        writer2.close();
    }
 
    public void search(String serchString) throws Exception {
        /* 创建一个搜索,搜索刚才创建的d:\\save\\目录下的索引 */
        IndexSearcher indexSearcher = new IndexSearcher("d:\\save\\");
        /* 在这里我们只需要搜索一个目录 */
        IndexSearcher indexSearchers[] = { indexSearcher };
        /* 我们需要搜索两个域"ArticleTitle", "ArticleText"里面的内容 */
        String[] fields = { "ArticleTitle", "ArticleText" };
        /* 下面这个表示要同时搜索这两个域,而且只要一个域里面有满足我们搜索的内容就行 */
        BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD,
                BooleanClause.Occur.SHOULD };
        /*
         * MultiFieldQueryParser表示多个域解析,
         * 同时可以解析含空格的字符串,如果我们搜索"中国 金牌",根据前面的索引,显然搜到的是第二份文件
         */
        Query query = MultiFieldQueryParser.parse(serchString, fields, clauses,
                new StandardAnalyzer());
        /* Multisearcher表示多目录搜索,在这里我们只有一个目录 */
        MultiSearcher searcher = new MultiSearcher(indexSearchers);
        /* 开始搜索 */
        Hits h = searcher.search(query);
        /* 把搜索出来的所有文件打印出来 */
        for (int i = 0; i < h.length(); i++) {
            /* 打印出文件里面ArticleTitle域里面的内容 */
            System.out.println(h.doc(i).get("ArticleTitle"));
            /* 打印出文件里面ArticleText域里面的内容 */
            System.out.println(h.doc(i).get("ArticleText"));
        }
        /* 关闭 */
        searcher.close();
    }
}

3.完成示例后再该延伸到哪些知识

分词器:对于输入的关键词的分词该如何分,需要再去研究
索引的更新:如果资源内容变了该如何更新索引



原文内容:http://www.cnblogs.com/zengwei/archive/2010/07/20/1781415.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值