Experiment with how Lucene is used.
Reference: Http://www.importnew.com/12715.html (example relatively simple)
Http://www.yiibai.com/lucene/lucene_first_application.html (example more complex)
Here's an example: Http://www.tuicool.com/articles/aqIZNnE
I use the version is relatively high, is 6.2.1 version, document review:
Http://lucene.apache.org/core/6_2_1/core/index.html
First create a MAVEN project in IntelliJ. The name is Lucene-demo. (main reference http://www.importnew.com/12715.html)
Among them Pom.xml are as follows:
<?xml version= "1.0" encoding= "UTF-8"? ><project xmlns= "http://maven.apache.org/POM/4.0.0"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" > <modelversion>4.0.0 </modelVersion> <groupId>com.myapp</groupId> <artifactId>lucene-demo</artifactId> & Lt;version>1.0-snapshot</version> <dependencies> <!--https://Mvnrepository.com/artifact/org.apache.lucene/lucene-core --<dependency> <groupId>org.apache.lucene</groupId> <artifactid>lucene-core< ;/artifactid> <version>6.2.1</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>6.2.1</version> </dependency> </dependencies></project>
Said a package:com.myapp.lucene, the inside class Lucenedemo, the contents are as follows:
PackageCom.myapp.lucene;ImportOrg.apache.lucene.analysis.standard.StandardAnalyzer;Importorg.apache.lucene.document.Document;ImportOrg.apache.lucene.document.Field;ImportOrg.apache.lucene.document.StringField;ImportOrg.apache.lucene.document.TextField;ImportOrg.apache.lucene.index.DirectoryReader;ImportOrg.apache.lucene.index.IndexReader;ImportOrg.apache.lucene.index.IndexWriter;ImportOrg.apache.lucene.index.IndexWriterConfig;Importorg.apache.lucene.queryparser.classic.ParseException;ImportOrg.apache.lucene.queryparser.classic.QueryParser;ImportOrg.apache.lucene.search.IndexSearcher;ImportOrg.apache.lucene.search.Query;ImportOrg.apache.lucene.search.ScoreDoc;ImportOrg.apache.lucene.search.TopScoreDocCollector;Importorg.apache.lucene.store.RAMDirectory;Importorg.apache.lucene.store.Directory;Importjava.io.IOException;/*** Created by Baidu on 16/10/20.*/ Public classLucenedemo {//0. Specify the analyzer for tokenizing text. //The same analyzer should is used for indexing and searching StaticStandardAnalyzer Analyzer; StaticDirectory Index; Static voidPreparedoc ()throwsioexception{//0. Init AnalyzerAnalyzer =NewStandardAnalyzer (); //1. CREATE INDEXindex =Newramdirectory (); Indexwriterconfig Config=NewIndexwriterconfig (Analyzer); IndexWriter W=Newindexwriter (index, config); Adddoc (W,"Lucence Tutorial", "123456"); Adddoc (W,"Hi Hi Hi", "222"); Adddoc (W,"OK lucence", "123"); W.close (); } Static voidAdddoc (IndexWriter w, string text, string more)throwsioexception{Document Doc=NewDocument (); Doc.add (NewTextField ("Text", text, Field.Store.YES)); Doc.add (NewStringfield ("More", more, Field.Store.YES)); W.adddocument (DOC); } Static voidSearch (String str)throwsParseException, IOException {//2. QueryQuery q =NewQueryparser ("Text", Analyzer). Parse (str); //3. Search intListNum = 10; Indexreader Reader=Directoryreader.open (index); Indexsearcher Searcher=NewIndexsearcher (reader); TopScoreDocCollector collector=topscoredoccollector.create (ListNum); Searcher.search (q, collector); Scoredoc[] Hits=Collector.topdocs (). Scoredocs; //4. DisplaySystem.out.printf ("Found%d docs.\n", hits.length); for(inti=0; i) { intDocId =Hits[i].doc; Document Doc=Searcher.doc (docId); System.out.printf ("Doc%d:text:%s, more:%s\n", I+1, Doc.get ("text"), Doc.get ("more")); } reader.close (); } Public Static voidMain (string[] args) {Try{Preparedoc (); Search ("Lucence"); } Catch(IOException e) {e.printstacktrace (); } Catch(ParseException e) {e.printstacktrace (); } }}
Then run and be able to succeed:
Found 21:text:lucence tutorial, more:1234562:text:ok lucence, more:1230
Because Ramdirectory is used, there should be no actual directories and files created.
In addition, there are several points to note in the Code and logic:
Note that we use TextField for content that requires word breakers, and we use Stringfield for content like IDs that do not require participle.
The coding process, reported several times wrong, about exception need wrap or throws situation.
Some API versions are upgraded, and the parameters are not the same as before. The actual code is changed according to the actual requirements. are generally simplified.
Experimental study and use of Lucene