Lucene在Eclipse下的部署

轉載出處:http://blog.csdn.net/ayi_5788/article/details/52121131

Tools required: 
Apache lucene – Download Here (After downloading extract the files to the desktop) 
JDK/JRE 7 – Download Here (Install) 
Eclipse – Download Here

Apache Lucene: Lucene is a full text search library written in Java. Lucene allows users to embed search functionality into any application. Read more about lucene at their official website. You can get an idea of the basic concepts in lucene by visiting this website.

Procedure: So, what are we waiting for? Let’s start to use lucene with eclipse…

Step 1: Open eclipse IDE 
這裏寫圖片描述 
Step 2: Create a Java project (File -> New -> java Project) 
這裏寫圖片描述 
Step 3: Give a name for the project. I have given “LuceneDemo” as the project name. Then click on “Finish”. 
這裏寫圖片描述 
Now your eclipse IDE would look something like shown below:這裏寫圖片描述 
Step 4: Right click on the project (LuceneDemo) and select “Properties” as shown below: 
這裏寫圖片描述 
Step 5: Click on “Java Build Path” on the left side and select “Add External JARs…” button on the right as shown below: 
這裏寫圖片描述 
Step 6: This step is very important. In this step, we will be adding 4 JAR files to our project (LuceneDemo). Previously you have extracted lucene on to your desktop. The folder name will be lucene-4.4.0. Below I will be listing the paths to 4 JAR files that we will be adding to our project:

C:\Users\User\Desktop\lucene-4.4.0\queryparser\lucene-queryparser-4.4.0.jar 
C:\Users\User\Desktop\lucene-4.4.0\analysis\common\lucene-analyzers-common-4.4.0.jar 
C:\Users\User\Desktop\lucene-4.4.0\core\lucene-core-4.4.0.jar 
C:\Users\User\Desktop\lucene-4.4.0\demo\lucene-demo-4.4.0.jar

My lucene folder (lucene-4.4.0) is stored at location C:\Users\User\Desktop. This path might vary on your systems.

After selecting and adding the 4 JAR files, the eclipse “Java Build Path” dialog box will look as shown below: 
這裏寫圖片描述 
After adding the JAR files, click on the “OK” button as shown in the above figure.

Step 7: Again right click on the project and select “New” and select “Class” as shown below: 
這裏寫圖片描述 
Step 8: Provide a name for your class. I provided the name as “LuceneTest”. We will write our java code in this file and run it. Refer the below picture to know where to write the class name. Click on “Finish” once you are done. 
這裏寫圖片描述

Step 9: Copy/Paste the code from below. You should have prior knowledge of core java to understand the code. The code is commented at appropriate places so that you can understand what is going on in the code.

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.util.Version;


public class LuceneTest 
{
    public static void main(String[] args)
    {
        try
        {
            //  Specify the analyzer for tokenizing text.
            //  The same analyzer should be used for indexing and searching
            StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_44);

            //  Code to create the index
            Directory index = new RAMDirectory();

            IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_44, analyzer);

            IndexWriter w = new IndexWriter(index, config);
            addDoc(w, \"Lucene in Action\", \"193398817\");
            addDoc(w, \"Lucene for Dummies\", \"55320055Z\");
            addDoc(w, \"Managing Gigabytes\", \"55063554A\");
            addDoc(w, \"The Art of Computer Science\", \"9900333X\");
            addDoc(w, \"My name is teja\", \"12842d99\");
            addDoc(w, \"Lucene demo by teja\", \"23k43413\");
            w.close();

            //  Text to search
            String querystr = args.length > 0 ? args[0] : \"teja\";

            //  The \"title\" arg specifies the default field to use when no field is explicitly specified in the query
            Query q = new QueryParser(Version.LUCENE_44, \"title\", analyzer).parse(querystr);

            // Searching code
            int hitsPerPage = 10;
            IndexReader reader = DirectoryReader.open(index);
            IndexSearcher searcher = new IndexSearcher(reader);
            TopScoreDocCollector collector = TopScoreDocCollector.create(hitsPerPage, true);
            searcher.search(q, collector);
            ScoreDoc[] hits = collector.topDocs().scoreDocs;

            //  Code to display the results of search
            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(\"isbn\") + \"\\t\" + d.get(\"title\"));
            }

            // reader can only be closed when there is no need to access the documents any more
            reader.close();
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
    }
    private static void addDoc(IndexWriter w, String title, String isbn) throws IOException 
    {
          Document doc = new Document();
          // A text field will be tokenized
          doc.add(new TextField(\"title\", title, Field.Store.YES));
          // We use a string field for isbn because we don\'t want it tokenized
          doc.add(new StringField(\"isbn\", isbn, Field.Store.YES));
          w.addDocument(doc);
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

這裏寫圖片描述 
Step 10: Click on “Run” button as shown below: 
 
Output: The keyword I was searching in the code is “teja”. You can see the output in the console window as shown below: 
這裏寫圖片描述 
Final note: I hope this tutorial helped you to start up with apache lucene using eclipse. Things may not Go perfect for you. You might face some errors while performing the steps in this tutorial. You can feel free to contact me via email if you have any errors or you can comment here in this article.


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章