簡(jiǎn)單實(shí)現(xiàn)lucence搜索

2018-11-29 16:08 更新

一:pom.xml

    <!-- lucene -->
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-core</artifactId>
        <version>5.3.1</version>
    </dependency>


    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-queryparser</artifactId>
        <version>5.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-common</artifactId>
        <version>5.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-analyzers-smartcn</artifactId>
        <version>5.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.lucene</groupId>
        <artifactId>lucene-highlighter</artifactId>
        <version>5.3.1</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.9</version>
    </dependency>  

二:創(chuàng)建索引,Indexer.java

package com.lucene;


import java.nio.file.Paths;


import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;


public class Indexer {
    private Integer ids[] = {1,2,3};
    private String citys[] = {"青島","南京","上海"};
    private String descs[] = {
            "青島是一個(gè)美麗的城市。",
            "南京是一個(gè)有文化的城市。南京是一個(gè)文化的城市南京,簡(jiǎn)稱(chēng)寧,是江蘇省會(huì),地處中國(guó)東部地區(qū),長(zhǎng)江下游,瀕江近海。全市下轄11個(gè)區(qū)," +
            "總面積6597平方公里,2013年建成區(qū)面積752.83平方公里,常住人口818.78萬(wàn),其中城鎮(zhèn)人口659.1萬(wàn)人。[1-4] “江南佳麗地,金陵帝王州”," +
            "南京擁有著6000多年文明史、近2600年建城史和近500年的建都史,是中國(guó)四大古都之一,有“六朝古都”、“十朝都會(huì)”之稱(chēng),是中華文明的重要發(fā)祥地," +
            "歷史上曾數(shù)次庇佑華夏之正朔,長(zhǎng)期是中國(guó)南方的政治、經(jīng)濟(jì)、文化中心,擁有厚重的文化底蘊(yùn)和豐富的歷史遺存。[5-7] 南京是國(guó)家重要的科教中心," +
            "自古以來(lái)就是一座崇文重教的城市,有“天下文樞”、“東南第一學(xué)”的美譽(yù)。截至2013年,南京有高等院校75所,其中211高校8所,僅次于北京上海;" +
            "國(guó)家重點(diǎn)實(shí)驗(yàn)室25所、國(guó)家重點(diǎn)學(xué)科169個(gè)、兩院院士83人,均居中國(guó)第三。[8-10] 。",
            "上海是一個(gè)繁華的城市。"
    };

    
    private Directory dir;

    
    /**
     * 獲取IndexWriter實(shí)例
     * @return
     * @throws Exception
     */
    private IndexWriter getWriter()throws Exception{
        //Analyzer analyzer  =  new StandardAnalyzer(); // 標(biāo)準(zhǔn)分詞器
        SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
        IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
        IndexWriter writer = new IndexWriter(dir, iwc);
        return writer;
    }

    
    /**
     * 生成索引
     * @param indexDir
     * @throws Exception
     */
    private void index(String indexDir)throws Exception{
        dir = FSDirectory.open(Paths.get(indexDir));
        IndexWriter writer = getWriter();
        for(int i = 0;i<ids.length;i++){
            Document doc = new Document();
            doc.add(new IntField("id", ids[i], Field.Store.YES));
            doc.add(new StringField("city",citys[i],Field.Store.YES));
            doc.add(new TextField("desc", descs[i], Field.Store.YES));
            writer.addDocument(doc); // 添加文檔
        }
        writer.close();
    }
    public static void main(String[] args) throws Exception {
        new Indexer().index("F:\\data");
    }
}

三:搜索,Lucence01.java

package com.lucene;


import java.io.StringReader;
import java.nio.file.Paths;


import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
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.search.highlight.Fragmenter;
import org.apache.lucene.search.highlight.Highlighter;
import org.apache.lucene.search.highlight.QueryScorer;
import org.apache.lucene.search.highlight.SimpleHTMLFormatter;
import org.apache.lucene.search.highlight.SimpleSpanFragmenter;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;




public class Lucene01{
    public static void search(String indexDir,String q)throws Exception{
        Directory dir = FSDirectory.open(Paths.get(indexDir));
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher is = new IndexSearcher(reader);
        // Analyzer analyzer = new StandardAnalyzer(); // 標(biāo)準(zhǔn)分詞器
        SmartChineseAnalyzer analyzer = new SmartChineseAnalyzer();
        QueryParser parser = new QueryParser("desc", analyzer);
        Query query = parser.parse(q);
        long start = System.currentTimeMillis();
        TopDocs hits = is.search(query, 10);
        long end = System.currentTimeMillis();
        System.out.println("匹配 "+q+" ,總共花費(fèi)"+(end-start)+"毫秒"+"查詢(xún)到"+hits.totalHits+"個(gè)記錄");

        
        QueryScorer scorer = new QueryScorer(query);
        Fragmenter fragmenter = new SimpleSpanFragmenter(scorer);
        SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter("<b><font color = 'red'>","</font></b>");
        Highlighter highlighter = new Highlighter(simpleHTMLFormatter, scorer);
        highlighter.setTextFragmenter(fragmenter);
        for(ScoreDoc scoreDoc:hits.scoreDocs){
            Document doc = is.doc(scoreDoc.doc);
            System.out.println(doc.get("city"));
            System.out.println(doc.get("desc"));
            String desc = doc.get("desc");
            if(desc != null){
                TokenStream tokenStream = analyzer.tokenStream("desc", new StringReader(desc));
                System.out.println(highlighter.getBestFragment(tokenStream, desc));
            }
        }
        reader.close();
    }

    
    public static void main(String[] args) {
        String indexDir = "F:\\data";
        String q = "南京文明是個(gè)美麗的城市";
        try {
            search(indexDir,q);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)