lucene4.10.2 example (Increase and deletion check)

Source: Internet
Author: User
Tags create directory createindex

Latest jar and src free download: http://download.csdn.net/detail/u011518709/8248403

the composition structure of the Lucene package: Indexing Module (index) and search module (search) are the main external application portals for external applications
org.apache.lucene.search/Search Portal

org.apache.lucene.index/Index Entry
org.apache.lucene.analysis/Language Analyzer
org.apache.lucene.queryparser/Query Analyzer
org.apache.lucene.document/Storage Structure
Org.apache.lucene.store/underlying io/storage structure
org.apache.lucene.util/Some common data structures

The jar referenced in the code, the copy-related jar file under the relative file above


Package Com.util;import Java.io.file;import java.io.ioexception;import java.io.stringreader;import java.util.List; Import Org.apache.log4j.logger;import Org.apache.lucene.analysis.analyzer;import Org.apache.lucene.analysis.tokenstream;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.document.field.store;import Org.apache.lucene.index.indexreader;import Org.apache.lucene.index.indexwriter;import Org.apache.lucene.index.indexwriterconfig;import Org.apache.lucene.index.logdocmergepolicy;import Org.apache.lucene.index.logmergepolicy;import Org.apache.lucene.index.term;import Org.apache.lucene.index.indexwriterconfig.openmode;import Org.apache.lucene.queryparser.classic.multifieldqueryparser;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.matchalldocsquery;import Org.apache.lucene.search.query;import Org.apache.lucene.search.scoredoc;import Org.apache.lucene.search.termquery;import Org.apache.lucene.search.topdocs;import Org.apache.lucene.search.highlight.highlighter;import Org.apache.lucene.search.highlight.invalidtokenoffsetsexception;import Org.apache.lucene.search.highlight.queryscorer;import Org.apache.lucene.search.highlight.SimpleFragmenter; Import Org.apache.lucene.search.highlight.simplehtmlformatter;import Org.apache.lucene.store.directory;import Org.apache.lucene.store.fsdirectory;import Org.apache.lucene.util.version;import  org.wltea.analyzer.lucene.ikanalyzer;/** * version:lucene4.10.2 * lucene Download: * @author ZP * Date 2014-12-12 */public    Class Index410 {Private final static Logger Logger = Logger.getlogger (Index410.class);     private static String Indexpath = "G://lucene//index"; public static void Main (string[] args) {try {CreateIndex ();//Searchindex ("Code Farmer");//query ();//Deleteindex ();//Forcedeleteindex ();//        Query ();//Highlightersearch ();        } catch (Exception e) {//TODO auto-generated catch block E.printstacktrace (); }}/** * CREATE INDEX */public static void CreateIndex () {//The most fine-grained slicing algorithm--true The word is intelligent slicing Analyzer ana        Lyzer = new Ikanalyzer (false);        Document doc = null;        IndexWriter indexwriter = null;            try {indexwriter = Getindexwriter (analyzer);            Add Index doc = new Document ();            Doc.add (New Stringfield ("id", "1", Store.yes));            Doc.add ("title", "title: Start", Store.yes));            Doc.add (New TextField ("content", "contents: I am now a yard farmer", Store.yes));            Indexwriter.adddocument (DOC);            doc = new Document ();            Doc.add (New Stringfield ("id", "2", Store.yes)); Doc.add (New TextField ("title", "Caption: End",Store.yes));            Doc.add (New TextField ("content", "contents: I am now an expert in lucene development engineers", Store.yes));            Indexwriter.adddocument (DOC);        Indexwriter.commit ();            } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();        Logger.info ("Indexer send exception");            } finally {try {destroywriter (indexwriter);            } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }}}/** * Search document * * @param keyword */@SuppressWarnings ("deprecation") public stat        IC void Searchindex (String keyword) {indexreader indexreader = null;        Indexsearcher indexsearcher = null; try {//1. Create directory on the hard disk under the G:/luence/index index directory dir = fsdirectory.open (new File (Indexpath            ));            2. Create Indexreader Indexreader = Indexreader.open (dir); Instantiating a search device           Indexsearcher = new Indexsearcher (Indexreader); Use the Queryparser query parser to construct the query object Queryparser parse = new Queryparser (version.lucene_4_10_1, "content", new Ikanaly            Zer (false));             Search for documents containing keyword keywords query query = parse.parse (Keyword.trim ());  When using Lucene to construct a search engine, if you want to make a one-time query for multiple domains//The advantage of this method is that you can weight the control of the field//retrieve string[in these four domains] fields =            {"Phonetype", "Name", "category", "Price"};             Query Querys = new Multifieldqueryparser (version.latest, Fields,new Ikanalyzer (False)). Parse (Keyword.trim ());            Topdocs results = indexsearcher.search (query, 1000);            6. Obtain Scoredoc object scoredoc[] score = Results.scoredocs according to Topdocs;                if (Score.length > 0) {logger.info ("Number of query results:" + score.length);                System.out.println ("Number of query results:" + score.length); for (int i = 0; i < score.length; i++) {//7. According to Seacher and Scoredoc objectsGets the specific Document object Document DOC = Indexsearcher.doc (score[i].doc);                    8. Obtain the desired value according to the Document Object System.out.println (Doc.tostring ());                System.out.println (Doc.get ("title") + "[" + Doc.get ("content") + "]"); }} else {}} catch (Exception e) {//Todo:handle Exception LOGGER.I NFO ("The query result is empty!        ");                } finally {if (Indexreader! = null) {try {indexreader.close (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (                ); }}}}/** * page display of the first n results returned by the search * * @param keyWord * Query keywords * @pa    Ram PageSize * Shows the number of records per page * @param currentpage * Current page * @throws parseexception */ @SuppressWarnings ("deprecation") public void Paginationquery (String KeyWord, int pageSize, int currentpage) throws IOException, ParseException {string[] fields = {        "title", "Content"};        Queryparser queryparser = new Multifieldqueryparser (Version.latest,fields, New Ikanalyzer ());         Query query = Queryparser.parse (Keyword.trim ());        Indexreader Indexreader = Indexreader.open (Fsdirectory.open (New File (Indexpath)));         Indexsearcher indexsearcher = new Indexsearcher (Indexreader); Topdocs search Returns the result topdocs Topdocs = indexsearcher.search (query, 100);//returns only the first 100 records topdocs all = Indexsearc        Her.search (New Matchalldocsquery (), 100); int totalcount = topdocs.totalhits; Total number of search results scoredoc[] Scoredocs = Topdocs.scoredocs;        Search returns the result set//query start record position int begin = PageSize * (currentPage-1);         query termination record position int end = Math.min (begin + PageSize, scoredocs.length); Paging query for (int i = begin; I < end; i++) {int doesCID = Scoredocs[i].doc;            System.out.println ("docid=" + DocID);            Document doc = Indexsearcher.doc (DocID);            String title = Doc.get ("title");        System.out.println ("title is:" + title);    } indexreader.close (); } @SuppressWarnings ("deprecation") public static void Highlightersearch () throws IOException, ParseException, inval        idtokenoffsetsexception {Indexreader reader = Indexreader.open (Fsdirectory.open (New File (Indexpath)));         Indexsearcher searcher = new Indexsearcher (reader);        String []fields={"title", "Content"};        Queryparser parser=new multifieldqueryparser (version.latest, fields,//New Ikanalyzer ());         Query query=parser.parse ("");        Term term = new term ("content", "Lucene");         Termquery query = new Termquery (term);        Topdocs Topdocs = searcher.search (query, Integer.max_value);        scoredoc[] Scoredoc = Topdocs.scoredocs; SYSTEM.OUT.PRINTLN ("Total number of query results:" + topdocs.totalhits);                 SYSTEM.OUT.PRINTLN ("Maximum rating:" + topdocs.getmaxscore ());            for (int i=0;i<scoredoc.length;i++) {int docid=scoredoc[i].doc;            Document Document=searcher.doc (DOCID);        SYSTEM.OUT.PRINTLN ("Search keyword:" +term.tostring ());        String content=document.get ("content");            Highlight Simplehtmlformatter formatter=new simplehtmlformatter ("<font color= ' Red ' >", "");            Highlighter highlighter=new highlighter (Formatter, new Queryscorer (query));                          Highlighter.settextfragmenter (New Simplefragmenter (Content.length ())); if (! "".                 Equals (content)) {Tokenstream tokenstream=new Ikanalyzer (). Tokenstream (Content, new StringReader (content));                  String Highlighttext = highlighter.getbestfragment (tokenstream,content);                  System.out.println ("Highlight" + (i + 1) + "Search results are as follows:");                 System.out.println (Highlighttext); /*end: Closing offKey word highlighting */System.out.println ("File contents:" +content);            SYSTEM.OUT.PRINTLN ("Matching correlation:" +scoredoc[i].score); }}}/** * Get IndexWriter object---get indexer * @param dir * @param analyer * @return * @th        Rows IOException */private static IndexWriter Getindexwriter (Analyzer Analyzer) throws IOException {        File Indexfile = new file (Indexpath); if (!indexfile.exists ()) indexfile.mkdir ();//The index library does not exist create a new directory directory = Fsdirectory.open (indexf        ILE); Directory directory = new ramdirectory ();        Build an index in memory indexwriterconfig conf = new Indexwriterconfig (version.lucene_4_10_1, analyzer);        Logmergepolicy mergepolicy = new Logdocmergepolicy ();        Index basic configuration//setting segment when adding documents (document)//values are small, indexing is slower/higher, indexing is faster, >10 is suitable for batch indexing        Mergepolicy.setmergefactor (30);    Set segment maximum number of merged documents (document)    A smaller value facilitates the speed of the index//value, which is suitable for batch indexing and faster search Mergepolicy.setmaxmergedocs (5000);        Conf.setmaxbuffereddocs (10000);        Conf.setmergepolicy (Mergepolicy);         CONF.SETRAMBUFFERSIZEMB (64);        Conf.setopenmode (Openmode.create_or_append);            if (indexwriter.islocked (directory)) {//?        Indexwriter.unlock (directory);        } indexwriter IndexWriter = new IndexWriter (directory, conf);    return indexwriter; }/** * Destroys writer * * @param writer * @throws IOException */private static void Destroywriter (         IndexWriter IndexWriter) throws IOException {if (IndexWriter! = null) {indexwriter.close (); }}/** * Bulk Delete * * @param list * @throws IOException */public static void Deletein Dexs (List list) throws IOException {if (list = = NULL | | list.size () > 0) {logger.debug ("Beans" is Nu            ll ");        Return } for (int i=0 ; I<list.size (); i++) {Deleteindex (); }}/** * Delete a single index--not immediately delete, generate. del file * * @param bean * @throws ioexception */private static        void Deleteindex () throws IOException {//if (bean==null) {//Logger.debug ("Get Search Bean is empty!");        Return        } IndexWriter IndexWriter = Getindexwriter (New Ikanalyzer ()); The parameter is an option that can be a query, or a term,term is an exact lookup value//Here the Id=1 document is deleted and will remain in the "Recycle Bin".        Xxx.del indexwriter.deletedocuments (New term ("id", "1"));    Destroywriter (IndexWriter); }/** * Query document */@SuppressWarnings ("deprecation") public static void query () {//1. Create Directory in hard                    On the disk F:/luence/index index try {indexreader indexreader = Indexreader.open (fsdirectory            . Open (New File (Indexpath)));            System.out.println ("Number of documents stored:" + indexreader.numdocs ());            SYSTEM.OUT.PRINTLN ("Total storage Volume:" + Indexreader.maxdoc ()); SYstem.out.println ("Deleted documents:" + Indexreader.numdeleteddocs ());        } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); }}/** * rollback Recycle Bin * * @throws IOException */public void Recoveryindexbyisdelete () throws Ioexcep        tion {IndexWriter indexwriter = getindexwriter (New Ikanalyzer ());        Indexwriter.rollback ();    Destroywriter (IndexWriter); }/** * Empty Recycle Bin after version 3.6, there is no Undeleteall () method * * @throws IOException */public static void Forcede        Leteindex () throws IOException {IndexWriter indexwriter = getindexwriter (New Ikanalyzer ());        Indexwriter.forcemergedeletes ();    Destroywriter (IndexWriter); }/** * Update index * * @throws IOException */public void update () throws IOException {Indexwrit Er indexwriter = new IndexWriter (Fsdirectory.open (New File (Indexpath)), New Indexwriterconfig (version.late         St       New Ikanalyzer (true));         Document document = new document ();        Document.add (New Field ("id", "ten", Field.store.yes,field.index.not_analyzed_no_norms));        Document.add (New Field ("email", "9481629991", field.store.yes,field.index.not_analyzed));        Document.add (New Field ("name", "Millet", field.store.yes,field.index.not_analyzed_no_norms));         Document.add (New Field ("content", "millet Good", field.store.no,field.index.analyzed)); Here's the update, from the method can be seen, it is actually when the old deleted, and then add a new document into, will match to the term of the document deleted, and then add to the new document Indexwriter.updatedocument ("ID         "," 1 "), document);    Indexwriter.close (); }}


lucene4.10.2 example (Increase and deletion check)

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.