static Directory dir;
public static void main(String[] args) {
String path = "index";
try {
dir = FSDirectory.open(new File(path));
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43,
analyzer);
IndexWriter indexWriter = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new Field("id", "", Store.YES, Index.ANALYZED));
doc.add(new Field("name", "", Store.YES, Index.ANALYZED));
indexWriter.addDocument(doc);
indexWriter.commit();
indexWriter.close();
IndexReader indexread = DirectoryReader.open(dir);
IndexSearcher search = new IndexSearcher(indexread);
QueryParser parser = new QueryParser(Version.LUCENE_43, "name",
analyzer);
parser.setDefaultOperator(Operator.OR);
Query query = parser.parse("str");
search.search(query, 5);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (org.apache.lucene.queryparser.classic.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
String path = "index";
try {
dir = FSDirectory.open(new File(path));
Analyzer analyzer = new IKAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(Version.LUCENE_43,
analyzer);
IndexWriter indexWriter = new IndexWriter(dir, iwc);
Document doc = new Document();
doc.add(new Field("id", "", Store.YES, Index.ANALYZED));
doc.add(new Field("name", "", Store.YES, Index.ANALYZED));
indexWriter.addDocument(doc);
indexWriter.commit();
indexWriter.close();
IndexReader indexread = DirectoryReader.open(dir);
IndexSearcher search = new IndexSearcher(indexread);
QueryParser parser = new QueryParser(Version.LUCENE_43, "name",
analyzer);
parser.setDefaultOperator(Operator.OR);
Query query = parser.parse("str");
search.search(query, 5);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (org.apache.lucene.queryparser.classic.ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}