this time I am gonna teach you how to implement lucene for the first time, to be able to use it you must first download lucene here and after you had finish downloading it, then you need to find a file named lucene-core-3.0.1.jar or any version will suffice. this jar file contain all the classes that are needed to run your first lucene application. to extract jar files use this command (you can use it in your command prompt).
jar xvf lucene-core-3.0.1.jar
after all the file had been extracted and put into a folder, then you must changed your java working directory to that folder that contain all lucene classes that had been extracted beforehand. so that your program can run properly when you compile it. now open up your editplus and copy this source code.
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;
import java.io.IOException;
public class HelloLucene {
public static void main(String[] args) throws IOException, ParseException {
// 0. Specify the analyzer for tokenizing text.
// The same analyzer should be used for indexing and searching
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
// 1. create the index
Directory index = new RAMDirectory();
// the boolean arg in the IndexWriter ctor means to
// create a new index, overwriting any existing index
IndexWriter w = new IndexWriter(index, analyzer, true,
IndexWriter.MaxFieldLength.UNLIMITED);
addDoc(w, "Lucene in Action");
addDoc(w, "Lucene for Dummies");
addDoc(w, "Managing Gigabytes");
addDoc(w, "The Art of Computer Science");
addDoc(w, "lucene itu sulit.hehe...");
addDoc(w, "terserah lucene ajah ah");
w.close();
// 2. query
String querystr = args.length > 0 ? args[0] : "lucene";
// the "title" arg specifies the default field to use
// when no field is explicitly specified in the query.
Query q = new QueryParser(
Version.LUCENE_CURRENT, "title", analyzer).parse(querystr);
// 3. search
int hitsPerPage = 10;
IndexSearcher searcher = new IndexSearcher(index, true);
TopScoreDocCollector collector =
TopScoreDocCollector.create(hitsPerPage, true);
searcher.search(q, collector);
ScoreDoc[] hits = collector.topDocs().scoreDocs;
// 4. display results
System.out.println("Found " + hits.length + " hits.");
for(int i=0;i<hits.length;++i) {
int docId = hits[i].doc;
Document d = searcher.doc(docId);
System.out.println((i + 1) + ". " + d.get("title"));
}
// searcher can only be closed when there
// is no need to access the documents any more.
searcher.close();
}
private static void addDoc(IndexWriter w, String value) throws IOException {
Document doc = new Document();
doc.add(new Field("title", value, Field.Store.YES, Field.Index.ANALYZED));
w.addDocument(doc);
}
}
save it as HelloLucene.java and then run it, you will then see an output like this. you should know that beforehand we had 6 indexes in the source code but because we’re searching for the index that had “lucene” word in it then the output will only display 4 index. this means that our first lucene sample application program did work properly.
by using this sample application then you will know how to include lucene in your program properly. actually lucene is not a program, it’s just a collection of java classes that deals directly with searching and whatnot. so that you will find it easier to develop your first search application using lucene.
PS : I am now doing my thesis that deals with using lucene as a helper in building search application, if you had any suggestion whatsoever then feel free to tell me.




[...] http://jacobian.web.id/2010/08/09/how-to-use-lucene-part-1/ [...]
Hii,
I don’t have any suggestions as such, I was looking for help though !
as a part of my graduating project, I am building a search application, which performs search on a resume database, to look for particular keywords.
As an adept user of Lucene, we would most certainly like to know from you, the feasibility of using it in my project.
Thanks and regards,
Priyanka
Guys,
I was just searching few lucene tutorial, which i am learning some concept to understand how hibernate search use it..a denormalized version. I am using this book hope it will help you..
http://www.manning.com/hatcher3/
-harsh sondhi
California