zoukankan      html  css  js  c++  java
  • 我的云之旅–Lucene内容存储进入Hadoop(136)

    首先了解一下Lucene的使用:


    package com.rx;索引的建立:

    import java.io.File;

    import java.io.IOException;

    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.index.IndexWriterConfig;

    import org.apache.lucene.store.Directory;

    import org.apache.lucene.store.SimpleFSDirectory;

    import org.apache.lucene.util.Version;

    public class C {

    public static void main(String[] args) throws IOException {

    IndexWriterConfig i = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));

    Directory d = new SimpleFSDirectory(new File("E:/index"));

    IndexWriter writer = new IndexWriter(d, i);

    Document doc = new Document();

    doc.add(new Field("title", "lucene introduction", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));

    doc.add(new Field("time", "60", Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));

    writer.addDocument(doc);

    writer.commit();

    writer.close();

    }

    }

     
    索引的查询:
    package com.rx;
     
    import java.io.File;
    import java.io.IOException;
     
    import org.apache.lucene.document.Document;
    import org.apache.lucene.index.IndexReader;
    import org.apache.lucene.index.Term;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TermQuery;
    import org.apache.lucene.search.TopDocs;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.SimpleFSDirectory;
     
    public class R {
    public static void main(String[] args) throws IOException {
    Directory d = new SimpleFSDirectory(new File("E:/index"));
    IndexReader reader = IndexReader.open(d);
    IndexSearcher searcher = new IndexSearcher(reader);
    Query query = new TermQuery(new Term("title", "lucene"));
    TopDocs hits = searcher.search(query, 10);
    System.out.println(hits.totalHits);
    for (ScoreDoc scoreDoc : hits.scoreDocs) {
    System.out.println(scoreDoc.doc);
    Document doc = searcher.doc(scoreDoc.doc);
    System.out.println("title /t " + doc.get("title"));
    System.out.println(doc.get("time"));
    }
    searcher.close();
    }
    }
     
    lucene的查看工具:
    java -jar lukeall-3.5.0.jar 运行即可。
     
    上面的写入运行2次后查询结果:
    2
    0
    title /t lucene introduction
    60
    1
    title /t lucene introduction
    60
     
    目前发现有人使用修改的lucene的代码和solr以及Hbase提供的分布式搜索,可以支持三千五百万的日搜索服务。

  • 相关阅读:
    数据结构学习8——二叉树的销毁
    单链表的反向
    LNK4098: 默认库“MSVCRT”与其他库的使用冲突
    动态链接库(VC_Win32)
    注册表操作(VC_Win32)
    消息钩子与定时器(VC_Win32)
    套接字编程(VC_Win32)
    线程概述,优先级,睡眠,创建及终止(VC_Win32)
    进程通信(VC_Win32)
    进程概述及创建,终止(VC_Win32)
  • 原文地址:https://www.cnblogs.com/hehehaha/p/6332469.html
Copyright © 2011-2022 走看看