建立索引和搜尋

來源:互聯網
上載者:User

標籤:getname   div   top   mdi   reader   analyzer   amp   []   trace   

package cn.dyg.luence;import org.apache.lucene.analysis.standard.StandardAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.document.Field;import org.apache.lucene.index.IndexReader;import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;import org.apache.lucene.queryParser.ParseException;import org.apache.lucene.queryParser.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;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import java.io.File;import java.io.FileReader;import java.io.IOException;/** * Created by Administrator on 2016/11/2. */public class IndexUtil {    private static final Logger logger = LoggerFactory.getLogger(IndexUtil.class);    private static final String indexPath = "E:\\javaweb\\luenceRepository\\index";    private IndexUtil() { }    public static IndexReader getIndexReader() {        logger.info("this is the indexReader ...");        return null;    }    public static void setIndexWriter() {        //1 建立Directory//        Directory directory = new RAMDirectory();   //索引建立在記憶體中        Directory directory = null;   //索引建立在磁碟中        try {            directory = FSDirectory.open(new File(indexPath));        } catch (IOException e) {            e.printStackTrace();        }        //2 建立IndexWriter        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, new StandardAnalyzer(Version.LUCENE_35));        IndexWriter indexWriter = null;        try {            indexWriter = new IndexWriter(directory, indexWriterConfig);            //3 建立文檔對象            Document document = null;            //4 為文檔添加filed            File fileList = new File("E:\\javaweb\\luenceRepository\\example");            for (File file : fileList.listFiles()) {                document = new Document();                document.add(new Field("content", new FileReader(file)));                document.add(new Field("name", file.getName(), Field.Store.YES, Field.Index.NOT_ANALYZED));                document.add(new Field("path", file.getAbsolutePath(), Field.Store.YES, Field.Index.NOT_ANALYZED));                //5 通過IndexWriter 添加文檔到索引中                indexWriter.addDocument(document);            }        } catch (IOException e) {            e.printStackTrace();        } finally {            if (indexWriter != null) {                try {                    indexWriter.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }    public static void search() {        //1 建立Directory//        Directory directory = new RAMDirectory();   //索引建立在記憶體中        Directory directory = null;   //索引建立在磁碟中        try {            directory = FSDirectory.open(new File(indexPath));        } catch (IOException e) {            e.printStackTrace();        }        //2 建立IndexReader        IndexReader indexReader = null;        try {            indexReader = IndexReader.open(directory);            //3 建立search            IndexSearcher indexSearcher = new IndexSearcher(indexReader);            //4 建立搜尋的Query            QueryParser queryParser = new QueryParser(                    Version.LUCENE_35,                    "content",                    new StandardAnalyzer(Version.LUCENE_35)            );            //5 建立Query, 表示搜尋域為content中包含java的文檔            Query query = queryParser.parse("3350527960");            //6 根據searcher搜尋並返回TopDocs            int queryLimit = 10;            TopDocs topDocs = indexSearcher.search(query, queryLimit);            //7 根據topDocs獲得scoreDocs            ScoreDoc[] scoreDocs = topDocs.scoreDocs;            int i = 0;            for (ScoreDoc scoreDoc : scoreDocs) {                //8 根據searcher 和ScoreDoc擷取document對象                Document document = indexSearcher.doc(scoreDoc.doc);                logger.info(i + document.get("path") + document.get("name"));            }        } catch (IOException e) {            e.printStackTrace();        } catch (ParseException e) {            e.printStackTrace();        } finally {            if (indexReader != null) {                try {                    indexReader.close();                } catch (IOException e) {                    e.printStackTrace();                }            }        }    }}

  

建立索引和搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.