Java tutorial
/* * Copyright 2013 Jun Ohtani * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package info.johtani.jjug.lucene.sample; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.TextField; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.index.IndexWriterConfig.OpenMode; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; import java.io.File; import java.io.IOException; import java.io.StringReader; import java.util.ArrayList; import java.util.List; /** * Sample for JJUG night seminar * * Indexer sample * * Created by johtani on 14/12/16. */ public class IndexerSample { public static void main(String[] args) { String indexDirectory = "./indexdir"; String[] texts = { "JJUG?Lucene?????johtani?????", "JJUG CCC?Elasticsearch?Kibana????johtani?????", "Elasticsearch?Kibana4????johtani?????" }; IndexWriter writer = null; try { //?? Directory dir = FSDirectory.open(new File(indexDirectory)); //?? StandardAnalyzer analyzer = new StandardAnalyzer(); //????? IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer); //CREATE_OR_APPEND????????????? //CREATE????????? //config.setOpenMode(OpenMode.CREATE_OR_APPEND); config.setOpenMode(OpenMode.CREATE); writer = new IndexWriter(dir, config); //? for (String text : texts) { //???? writer.addDocument(getDocument(text)); } // ???List? //List<Document> docs = new ArrayList<Document>(); //docs.add(document); //writer.addDocuments(docs); //writer????????? //writer.commit(); } catch (IOException e) { e.printStackTrace(); } finally { try { //close?? if (writer != null) { writer.close(); } } catch (IOException e) { //ignore } } System.out.println("Finished!"); } //??? public static Document getDocument(String text) { Document document = new Document(); //"content"??? document.add(new TextField("content", text, Field.Store.YES)); //document.add(new TextField("content", new StringReader(text))); return document; } }