package com.test.cache.file;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
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.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class IndexTest {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
public static void writeIndex() {
String date = dateFormat.format(new Date());
File file = getIndexDir(date);
try {
Directory dir = FSDirectory.open(file);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43,
analyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, iwc);
Document doc = new Document();
byte[] pbContents = "warm up lucene".getBytes();
Field id = new LongField("id", 123456, Store.YES);
Field msg = new StoredField("msg", pbContents);
doc.add(id);
doc.add(msg);
writer.addDocument(doc);
writer.commit();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static File getIndexDir(String date) {
File idxDir = new File("../data/" + date);
if (!idxDir.exists()) {
idxDir.mkdirs();
}
return idxDir;
}
public static void search() {
String date = dateFormat.format(new Date());
File file = getIndexDir(date);
try {
IndexReader reader = DirectoryReader.open(FSDirectory.open(file));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
QueryParser parser = new QueryParser(Version.LUCENE_43, "id",
analyzer);
Query query = parser.parse("123456");
TopDocs tops = searcher.search(query, 10);
for(ScoreDoc sc : tops.scoreDocs){
Document doc = searcher.doc(sc.doc);
System.out.println(doc.getField("id").stringValue());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
writeIndex();
search();
}
}
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexWriterConfig.OpenMode;
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.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class IndexTest {
private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMdd");
public static void writeIndex() {
String date = dateFormat.format(new Date());
File file = getIndexDir(date);
try {
Directory dir = FSDirectory.open(file);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43,
analyzer);
iwc.setOpenMode(OpenMode.CREATE);
IndexWriter writer = new IndexWriter(dir, iwc);
Document doc = new Document();
byte[] pbContents = "warm up lucene".getBytes();
Field id = new LongField("id", 123456, Store.YES);
Field msg = new StoredField("msg", pbContents);
doc.add(id);
doc.add(msg);
writer.addDocument(doc);
writer.commit();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static File getIndexDir(String date) {
File idxDir = new File("../data/" + date);
if (!idxDir.exists()) {
idxDir.mkdirs();
}
return idxDir;
}
public static void search() {
String date = dateFormat.format(new Date());
File file = getIndexDir(date);
try {
IndexReader reader = DirectoryReader.open(FSDirectory.open(file));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_43);
QueryParser parser = new QueryParser(Version.LUCENE_43, "id",
analyzer);
Query query = parser.parse("123456");
TopDocs tops = searcher.search(query, 10);
for(ScoreDoc sc : tops.scoreDocs){
Document doc = searcher.doc(sc.doc);
System.out.println(doc.getField("id").stringValue());
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
writeIndex();
search();
}
}