Establish an index and query example with lucene

xiaoxiao2021-03-06  39

First go to the Apache website to download Lucene's development package, and configure environment variable http://jakarta.apache.org/lucene/docs/index.html to establish an index: / * Created on 2004-4-26 * /

Import org.apache.lucene.index. *; import org.apache.lucene.Alysis.standard. *; import org.apache.lucene.document. *; import java.io. *;

/ ** * @Author bell.wang * / public class indexfiles {

Public static void main (string [】 args) {try {indexwriter write = new indexwriter ("MyIndex", New StandardAnalyzer (), true); file files = new file ("mydoc"); string [] fnamelist = files.list (); For (int i = 0; i

} The program establishes an index on all the files in the MyDoc directory under the current path, with three fields: file path, final modification time, file content. Created index files in the current path MyIndex directory search program: / * * Created On 2004 -4-26 * * / Import org.apache.lucene.Analysis. *; Import org.apache.lucene.Alysis.standard. *; Import org.apache.lucene.search. *; Import org.apache.lucene.QueryParser . *; import org.apache.lucene.document. *; // Import com.augmentum.hrms. *; import java.util.date; / ** * @Author bell.wang * * / public class searchfile {public static Void main (string [] args) {// xmap a = new xmap (""); analyzer anlzr = new standardArDArdArDAr ("database", "contents", ANLZR); System; SYSTEM .out.println ("Searching for:" Q.TOString ("Contents")); Searcher Serch = New IndexSearcher ("MyIndex"); Hits HTS = Serch.Search (q); for (int i = 0; i

The program is queried with the "Database" keyword with the "Database" keyword. It is all the items containing the keyword, and print out all fields. I use a plain text file to pass, .txt, .jsp, .html can, Word, PDF and other files need to be converted to index it.

转载请注明原文地址:https://www.9cbs.com/read-70378.html

New Post(0)