java.lang.unsupportedclassversionerror:org/apache/lucene/index/indexablefield:unsupported Major.minor Version 51.0
Apache Lucene 4.8.0 Released: Java 6 is no longer supported because Lucene4.9 requires a minimum Java version of Java7, I use 4.7.2 because I installed Jdk6
A. Example of lucene4.7 version 1
public class Indexfile {protected string[] ids={"1", "2"}; Protected string[] content={"Amsterdam have lost of add cancals", "I love Add this Girl"}; Protected string[] city={"Amsterdam", "Venice"}; Private Directory dir; /** * Initial Add document * @throws Exception */@Before public void init () throws Exception {String Pathfile = "E:/indexpath";//create an index storage location//1.4 Analyzer Analyzer Analyzer=new StandardAnalyzer, which creates the specified index language vocabulary CENE_47); 1.3 Specify index version and language lexical parser through Indexwriterconfig creation Indexwriterconfig iwc=new indexwriterconfig (version.lucene_47, analyzer) ; 1.2 Location Dir=fsdirectory.open (new File (Pathfile)) through the creation of the directory with the specified index; 1.1 Create IndexWriter, it is used to write the index file,//Can be indexwriter as a specific type of database, used to store various tables, can be regarded as a sheet of document//the method has two parameters, The first dir parameter is the index storage location, the parameter type is directory, the second parameter conf is IndexWriter's configuration class IndexWriter writer = new IndexWriter (dir, IWC); for (int i=0; i < ids.length; I+ +) {<span style= "white-space:pre" ></span>//2.1 Create document specifies documents to be indexed <span style= "White-space:pre" > </span>//can see document as a table in a database, and each field is a colum in the table to hold various types of information, such as title, author, time, etc. document Doc=new document () ; Doc.add (New Stringfield ("id", Ids[i], store.yes)); Doc.add (New TextField ("content", Content[i], store.yes)); Doc.add (New Stringfield ("City", City[i], store.yes)); Writer.adddocument (DOC); }//2.2 Table (Document) is created, of course, added to the database (IndexWriter), and commit writer.commit (); Writer.close (); }/** * Query * @throws Exception */@Test public void Search () throws Exception {String Filepat H= "E:/indexpath"; 1.3 Specify Search Directory Dir=fsdirectory.open (new File (FilePath)); 1.2 Create Indexreader to read the search directory to Memory Indexreader Reader=directoryreader.open (dir); 1.1 Create Indexsearcher prepare to search Indexsearcher searcher=new indexsearcher (reader); 2.3 Search the content index for keyword "Add" term term=new term ("content", "add"); 2.2 Create query Syntax tree termquery query=new termquery (term); 2.1 Get search results, search for the highest similarity of 5 records Topdocs topdocs=searcher.search (query, 5); Scoredoc[] Scoredocs=topdocs.scoredocs; SYSTEM.OUT.PRINTLN ("Total number of query results:" + topdocs.totalhits+ "maximum rating:" +topdocs.getmaxscore ()); for (int i=0; i < scoredocs.length; i++) {int doc = Scoredocs[i].doc; Document document = Searcher.doc (doc);//Hit File System.out.println ("content====" +document.get ("Content")); SYSTEM.OUT.PRINTLN ("ID:" + scoredocs[i].doc + "score:" + scoredocs[i].score+ "index:" +scoredocs[i].shardindex); } reader.close (); }}
Second, lucene4.6 Example 2
public class Constants {public final static String Index_file_path = "f:\\lucene\\test";//index files can be built in this directory by self-documenting Allow self-editing to public final static String Index_store_path = "F:\\lucene\\index";//Index storage location}
public class Luceneindex {/** * CREATE INDEX * @param analyzer * @throws Exception */public static void CreateIndex (Analyzer analy Zer) throws Exception{directory Dire=fsdirectory.open (new File (Constants.index_store_path)); Indexwriterconfig iwc= New Indexwriterconfig (version.lucene_46, Analyzer), IndexWriter iw=new IndexWriter (dire, IWC); This.adddoc (IW); Iw.close ();} /** * Dynamically Add Document * @param IW * @throws Exception */public static void Adddoc (IndexWriter iw) throws exception{file[] fi Les=new file (Constants.index_file_path). Listfiles (); for (File file:files) {document Doc=new document (); String content=luceneindex.getcontent (file); String Name=file.getname (); String Path=file.getabsolutepath ();d Oc.add (New TextField ("content", content, Store.yes));d Oc.add (New TextField (" Name ", name, Store.yes));d Oc.add (New TextField (" path ", Path,store.yes)); System.out.println (name+ "= = =" +content+ "= = =" +path); Iw.adddocument (doc); Iw.commit ();}} /** * Get text content * @param file * @return * @throws Exception */@SuppressWarnings ("Resource") public static String getcontent (file file) throws Exception{fileinputstream fis=new FileInputStream (file); Nputstreamreader isr=new InputStreamReader (FIS); BufferedReader br=new BufferedReader (ISR); StringBuffer sb=new StringBuffer (); String Line=br.readline (); while (Line!=null) {sb.append (line+ "\ n"); line=null;} return sb.tostring ();} /** * Search * @param query * @throws Exception */private static void Search (query query) throws Exception {Directory DIRE=FSD Irectory.open (new File), Indexreader ir=directoryreader.open (dire); Indexsearcher is= New Indexsearcher (IR); Topdocs td=is.search (query, 1000); System.out.println ("Find a total of" +td.totalhits+ "results"); Scoredoc[] SDS =td.scoredocs;for (Scoredoc Sd:sds) {Document d = is.doc (Sd.doc); System.out.println (D.get ("path") + ": [" +d.get ("path") + "]"); }}public static void Main (string[] args) throws Exception, Exception {Analyzer analyzer=new standardanalyzer ( VERSION.LUCENE_46); Luceneindex.createindex (analyzer); Queryparser ParsER = new Queryparser (version.lucene_46, "content", analyzer); Query query = parser.parse ("person"); Luceneindex.search (query);}}
lucene4.7 examples