lucene全文检索

1、Lucene的核心jar包

lucene-core-5.2.1.jar

lucene-analyzers-common-5.2.1.jar

lucene-queryparser-5.2.1.jar

2、主要开发包说明

org.apache.lucene.analysis:语言分析器,主要用于分词

org.apache.lucene.document:索引文档的管理

org.apache.lucene.index:索引管理,如增、删、改

org.apache.lucene.queryparser:查询分析

org.apache.lucene.search:检索管理

org.apache.lucene.store:数据存储管理

org.apache.lucene.util:工具包

3、写入索引操作的核心类

Directory:代表索引文档的存储位置,这是一个抽象类有FSDirectoryRAMDirectory两个主要子类。前者将索引写入文件系统,后者将索引文档写入内存。

Analyzer:建立索引时使用的分析器,主要子类有StandardAnalyzer(一个汉字一个词),还可以由第三方提供如开源社区提供一些中文分词器。

IndexWriterConfig:操作索引库的配置信息

IndexWriter:建立索引的核心类,用来操作索引(增、删、改)

Document:代表一个索引文档

Field:代表索引文档中存储的数据,新版本的Lucene进行了细化给出了多个子类:IntFieldLongFieldFloatFieldDoubleFieldTextFieldStringField等。

4、写入索引

 

 1 package cn.harmel.lucene;
 2 
 3 import java.io.IOException;
 4 import java.nio.file.Paths;
 5 
 6 import org.apache.lucene.analysis.Analyzer;
 7 import org.apache.lucene.analysis.standard.StandardAnalyzer;
 8 import org.apache.lucene.document.Document;
 9 import org.apache.lucene.document.Field;
10 import org.apache.lucene.document.TextField;
11 import org.apache.lucene.index.IndexWriter;
12 import org.apache.lucene.index.IndexWriterConfig;
13 import org.apache.lucene.store.Directory;
14 import org.apache.lucene.store.FSDirectory;
15 
16 public class First {
17 
18     public static void main(String[] args) throws IOException {
19         Analyzer a = new StandardAnalyzer();
20         Directory dir = FSDirectory.open(Paths.get("./index"));
21         IndexWriterConfig iwc = new IndexWriterConfig(a);
22         IndexWriter iw = new IndexWriter(dir, iwc);
23         Document doc = new Document();
24         doc.add(new TextField("info", "this is my first lucene test", Field.Store.YES));
25         iw.addDocument(doc);
26         iw.close();
27         dir.close();
28     }
29 
30 }

 

5、查询索引操作的核心类

IndexReader:读取索引的工具类,常用子类有DirectoryReader

IndexSearch:查询索引的核心类

QueryParser:查询分析器,表示从哪里查用哪个分析器

Query:代表一次查询

TopDocs:封装了匹配情况,比如匹配多少个

ScoreDoc:匹配的数据,里面封装了索引文档的得分和索引ID

6、查询索引

package cn.harmel.lucene;

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

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.ParseException;
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;

public class Second {

    public static void main(String[] args) throws IOException, ParseException {
        Analyzer a = new StandardAnalyzer();
        Directory dir = FSDirectory.open(Paths.get("./index"));
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher is = new IndexSearcher(reader);
        QueryParser parser = new QueryParser("info", a);
        Query query = parser.parse("lucene");
        TopDocs topDocs = is.search(query, 1000);
        System.out.println("总共匹配多少个:" + topDocs.totalHits);
        ScoreDoc[] hits = topDocs.scoreDocs;
        // 应该与topDocs.totalHits相同
        System.out.println("多少条数据:" + hits.length);
        for (ScoreDoc scoreDoc : hits) {
            System.out.println("匹配得分:" + scoreDoc.score);
            System.out.println("文档索引ID:" + scoreDoc.doc);
            Document document = is.doc(scoreDoc.doc);
            System.out.println(document.get("info"));
        }
        reader.close();
        dir.close();
    }

}

 

posted @ 2017-05-19 14:14  云晴  阅读(464)  评论(0编辑  收藏  举报