Lucene example, Lucene demo, Lucene tutorial

Source: Internet
Author: User

Looked up a lot of lucene data, wondering why can't share a simple example, I wrote a

Lucene implementation is actually very simple, first indexed, in the search, easy!

Download the jar package, Link here: http://download.csdn.net/detail/dannor2010/8183641 project to import Lib, not much to say. Upfront: Create two txt files,
C:\\source
C:\\index
Create a TXT file in source and enter the string type of content you want to test the search for.
1, build the index, the code is as follows

Package Com.ch.lucene;import Java.io.file;import Java.io.filereader;import java.util.date;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.index.indexwriter;import Org.apache.lucene.index.indexwriterconfig;import Org.apache.lucene.index.indexwriterconfig.openmode;import Org.apache.lucene.store.directory;import Org.apache.lucene.store.fsdirectory;import Org.apache.lucene.util.version;import Com.ch.util.FileUtil;public Class Textfileindexer {@SuppressWarnings ("deprecation") public static void main (string[] args) throws Exception {File Filedir = new File ("C:\\source"); File FileIndex = new file ("C:\\index");D irectory dir = Fsdirectory.open (FileIndex); Analyzer Luceneanalyzer = new StandardAnalyzer (version.lucene_45); Indexwriterconfig IWC = new Indexwriterconfig ( Version.lucene_45,luceneanalyzer); Iwc.setopenmode (openmode.create); IndexWriter IW = new IndexWriter (dir, IWC); file[] Textfiles = Filedir.listfiles (); Long startTime = new Date (). GetTime ();//Add document to index go for (File file:textfiles) {if (File.isfile () && file.getname (). EndsWith (". txt")) {System.out.println ("file:" + file.getcanonicalpath () + " The operation is being indexed. "); String temp = Fileutil.readtxtfile (File.getpath ()); System.out.println (temp);D ocument document = new Document (), @SuppressWarnings ("unused") field filepath = new Field (" Path ", File.getpath (), Field.Store.YES, Field.Index.NO); Field fieldbody = new Field ("Body", temp, Field.store.yes,field.index.analyzed,field.termvector.with_positions_ Offsets);d Ocument.add (filepath);d ocument.add (fieldbody); iw.adddocument (document);} Iw.close ();//test the time of the index long endTime = new Date (). GetTime (); SYSTEM.OUT.PRINTLN ("This cost" + (Endtime-starttime) + "milliseconds to add the document to the index" + filedir.getpath ());}}


2, search, the code is as follows

Package Com.ch.lucene;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.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.scoredoc;import Org.apache.lucene.search.topdocs;import Org.apache.lucene.store.fsdirectory;import Org.apache.lucene.util.version;public class Query {public static void main (string[] args) throws IOException, parseexception {String index = "C:\\index"; @SuppressWarnings ("deprecation") Indexreader reader = Indexreader.open ( Fsdirectory.open (index)); Indexsearcher searcher = new Indexsearcher (reader); Scoredoc[] hits = null; String queryString = "keywords"; Search Keywords Analyzer analyzer = new StandardAnalyzer (version.lucene_45); Queryparser QP = new Queryparser (version.lucene_45, "body", anAlyzer); Org.apache.lucene.search.Query Query = Qp.parse (queryString); if (searcher! = null) {Topdocs results = Searcher.search (query); hits = results.scoredocs;if (Hits.length > 0) {System.out.println ("found:" + Hits.length);}}}





Lucene example, Lucene demo, Lucene tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.