Creating indexes and searches

Source: Internet
Author: User
Tags create directory

Package Cn.dyg.luence;import Org.apache.lucene.analysis.standard.standardanalyzer;import Org.apache.lucene.document.document;import Org.apache.lucene.document.field;import Org.apache.lucene.index.indexreader;import Org.apache.lucene.index.indexwriter;import Org.apache.lucene.index.indexwriterconfig;import Org.apache.lucene.queryparser.parseexception;import Org.apache.lucene.queryparser.queryparser;import;import;import;import; Import;import;import Org.apache.lucene.util.version;import Org.slf4j.logger;import Org.slf4j.loggerfactory;import;import;import;/** * Created by Administrator on 2016/11/2.    */public class Indexutil {private static final Logger Logger = Loggerfactory.getlogger (Indexutil.class); private static Final String Indexpath = "E:\\javaweb\\luencerepository\\index";         Private Indexutil () {} public static Indexreader Getindexreader () { ("This is the Indexreader ...");    return null;   } public static void Setindexwriter () {//1 created directory//directory directory = new ramdirectory ();   Index is built in memory directory directory = NULL;        Index is built on disk try {directory = (new File (Indexpath));        } catch (IOException e) {e.printstacktrace (); }//2 Create IndexWriter indexwriterconfig indexwriterconfig = new Indexwriterconfig (version.lucene_35, New Stand        Ardanalyzer (version.lucene_35));        IndexWriter indexwriter = null;            try {indexwriter = new IndexWriter (directory, indexwriterconfig);            3 Creating a Document object documents = NULL;            4 Add filed file FileList = new file ("E:\\javaweb\\luencerepository\\example") for the document; for (File File:fiLelist.listfiles ()) {document = new document ();                Document.add (New Field ("Content", new FileReader (file));                Document.add (New Field ("name", File.getname (), Field.Store.YES, Field.Index.NOT_ANALYZED));                Document.add (New Field ("Path", File.getabsolutepath (), Field.Store.YES, Field.Index.NOT_ANALYZED));            5 adding documents to the index by IndexWriter indexwriter.adddocument (document);        }} catch (IOException e) {e.printstacktrace ();                } finally {if (IndexWriter! = null) {try {indexwriter.close ();                } catch (IOException e) {e.printstacktrace (); }}}} public static void Search () {//1 Create directory//directory directory = new RA   Mdirectory ();   Index is built in memory directory directory = NULL; Index is built on disk try {directory = (new FilE (Indexpath));        } catch (IOException e) {e.printstacktrace ();        }//2 Create indexreader indexreader indexreader = null;            try {indexreader = (directory);            3 Create search Indexsearcher Indexsearcher = new Indexsearcher (Indexreader);                    4 Create a search for query queryparser queryparser = new Queryparser (version.lucene_35,            "Content", new StandardAnalyzer (version.lucene_35));            5 Create a query that indicates that the search domain is a document containing Java in content query query = queryparser.parse ("3350527960");            6 searches according to searcher and returns Topdocs int querylimit = 10;            Topdocs Topdocs = (query, querylimit);            7 according to Topdocs obtained scoredocs scoredoc[] Scoredocs = Topdocs.scoredocs;            int i = 0;     for (Scoredoc scoredoc:scoredocs) {//8 Gets the Document object based on searcher and Scoredoc           Document document = Indexsearcher.doc (Scoredoc.doc);   (i + document.get ("path") + document.get ("name");        }} catch (IOException e) {e.printstacktrace ();        } catch (ParseException e) {e.printstacktrace ();                } finally {if (Indexreader! = null) {try {indexreader.close ();                } catch (IOException e) {e.printstacktrace (); }            }        }    }}


Creating indexes and searches

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: 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.