Created
March 11, 2013 14:08
-
-
Save meigesir/5134477 to your computer and use it in GitHub Desktop.
官方简单的例子:lucene 4.1.0,导入lucene-core-4.1.0.jar、lucene-analyzers-common-4.1.0.jar、lucene-queries-4.1.0.jar、lucene-queryparser-4.1.0.jar
1.IndexWriter建立索引的类;2.IndexSearcher搜索索引的类
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.mei.testlucene; | |
| import java.io.File; | |
| import java.io.IOException; | |
| 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.TextField; | |
| import org.apache.lucene.index.DirectoryReader; | |
| import org.apache.lucene.index.IndexWriter; | |
| import org.apache.lucene.index.IndexWriterConfig; | |
| 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.store.Directory; | |
| import org.apache.lucene.store.FSDirectory; | |
| import org.apache.lucene.store.RAMDirectory; | |
| import org.apache.lucene.util.Version; | |
| public class HelloLucene { | |
| public static void main(String[] args) throws IOException, ParseException { | |
| Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); | |
| // Store the index in memory: | |
| Directory directory = new RAMDirectory(); | |
| // To store an index on disk, use this instead: | |
| //Directory directory = FSDirectory.open(new File("H:/luceneIndex")); | |
| IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); | |
| IndexWriter iwriter = new IndexWriter(directory, config); | |
| Document doc = new Document(); | |
| String text = "This is the text to be indexed."; | |
| doc.add(new Field("fieldname", text, TextField.TYPE_STORED)); | |
| iwriter.addDocument(doc); | |
| iwriter.close(); | |
| System.out.println("建立索引 ok"); | |
| // Now search the index: | |
| DirectoryReader ireader = DirectoryReader.open(directory); | |
| IndexSearcher isearcher = new IndexSearcher(ireader); | |
| // Parse a simple query that searches for "text": | |
| QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, "fieldname", analyzer); | |
| Query query = parser.parse("text"); | |
| ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; | |
| System.out.println(hits.length == 1); | |
| // Iterate through the results: | |
| for (int i = 0; i < hits.length; i++) { | |
| Document hitDoc = isearcher.doc(hits[i].doc); | |
| System.out.println(hitDoc.get("fieldname")); | |
| } | |
| ireader.close(); | |
| directory.close(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment