Lucene Series
The instance code is as follows:
1 Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);2 IndexWriterConfig iwconf = new IndexWriterConfig(Version.LUCENE_36, analyzer);3 iwconf.setOpenMode(IndexWriterConfig.OpenMode.CREATE);4 IndexWriter indexWriter = new IndexWriter(indexDir, iwconf);
Note: standardanalyzer is a built-in "Standard analyzer" in Lucene and can be used as follows:
- The original sentence is segmented by Space
- All uppercase letters can be converted to lowercase letters.
- You can remove useless words, such as "is", "the", "are", and delete all punctuation marks.
In ipve3.x, the initialization of the indexwriter instance is different from the previous versions. The indexwriterconfig class is required. In addition, we can see from the Lucene API that the latest constructor of the indexwriter class currently uses the indexwriterconfig class (Other constructor is not recommended and is discarded ), you need to set the openmode attribute:
1 iwconf. setopenmode (openmode. Create); 2 // or 3 iwconf. setopenmode (openmode. append); 4 // or 5 iwconf. setopenmode (openmode. create_or_append );
Note: This line of code sets the folder for storing the index to be created in overwrite or new mode. If this setting is not set and the index in the original index folder is not deleted, the new index file will be appended to the original index file, which leads to an index result error.