zoukankan      html  css  js  c++  java
  • lucene 4.4 demo

    ackage com.zxf.demo;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStreamReader;
    
    import org.apache.lucene.analysis.Analyzer;
    import org.apache.lucene.analysis.standard.StandardAnalyzer;
    import org.apache.lucene.document.Document;
    import org.apache.lucene.document.Field;
    import org.apache.lucene.document.StringField;
    import org.apache.lucene.document.TextField;
    import org.apache.lucene.index.DirectoryReader;
    import org.apache.lucene.index.IndexReader;
    import org.apache.lucene.index.IndexWriter;
    import org.apache.lucene.index.IndexWriterConfig;
    import org.apache.lucene.queryparser.classic.QueryParser;
    import org.apache.lucene.search.IndexSearcher;
    import org.apache.lucene.search.Query;
    import org.apache.lucene.search.ScoreDoc;
    import org.apache.lucene.search.TopDocs;
    import org.apache.lucene.store.Directory;
    import org.apache.lucene.store.FSDirectory;
    import org.apache.lucene.util.Version;
    
    
    
    public class LuceneForuDemo {
    static final String INDEXPATH = System.getProperty("user.dir") + "\index";
    static final String DATAPATH = System.getProperty("user.dir") + "\data";
    
    public static void main(String[] args) {
    try{
    LuceneForuDemo.indexDirectory();
    LuceneForuDemo.search();
    }catch (Exception e) {
    e.printStackTrace();
    }
    }
    
    /**
    * 建立索引
    * @throws Exception
    */
    public static void indexDirectory() throws Exception{
    File indexDir = new File(LuceneForuDemo.INDEXPATH);
    File dataDir = new File(LuceneForuDemo.DATAPATH);
    
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
    IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_44, analyzer);
    Directory dir = FSDirectory.open(indexDir);
    IndexWriter indexWriter = new IndexWriter(dir, iwc);
    
    File[] dataFiles = dataDir.listFiles();
    for (File file : dataFiles) {
    FileInputStream inStream = new FileInputStream(file);
    Document doc = new Document();
    Field fullFileName = new StringField("fullFileName", file.getCanonicalPath(),Field.Store.YES);
    doc.add(fullFileName);
    doc.add(new TextField("contents", new BufferedReader(
    new InputStreamReader(inStream, "UTF-8")
    )
    )
    );
    indexWriter.addDocument(doc);
    }
    indexWriter.close();
    }
    
    /*搜索*/
    public static void search() throws Exception{
    String field = "contents";
    String queryStr = "test"; //搜索的字符串
    File indexDir = new File(LuceneForuDemo.INDEXPATH);
    IndexReader reader = DirectoryReader.open(FSDirectory.open(indexDir));
    IndexSearcher searcher = new IndexSearcher(reader);
    Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);
    QueryParser parser = new QueryParser(Version.LUCENE_44, field, analyzer);
    Query query = parser.parse(queryStr);
    System.out.println("Searching for: " + query.toString(field));
    TopDocs results = searcher.search(query, 50);
    ScoreDoc[] hits = results.scoreDocs;
    int numTotalHits = results.totalHits;
    System.out.println(numTotalHits + " total matching in documents");
    for(ScoreDoc sd:hits){
    Document doc = searcher.doc(sd.doc);
    System.out.println(doc.get("fullFileName"));
    }
    
    
    }
    }
    
  • 相关阅读:
    阿里云服务器ECS centos_7 安装jdk环境
    Spring Data Jpa 投影(Projection)的用法
    win10 docker 部署微服务
    centos 安装docker
    VMware 启动 虚拟机后一直黑屏
    VMware 新建虚拟机选择操作系统的时候提示,此主机不支持64位客户机操作系统,此系统无法运行
    mysql 表名全变小写的解决(windows)
    docker: Error response from daemon: driver failed programming external connectivity on endpoint jovial_morse (71d0315110605c3812f7255c7072f5d38512118a4635feaf84cdc170d3bee8d1): Error starting userland
    win10 docker部署springboot项目
    nginx failed (1113: No mapping for the Unicode character exists in the target multi-byte code page)
  • 原文地址:https://www.cnblogs.com/sand-tiny/p/3935171.html
Copyright © 2011-2022 走看看