//创建索引
public static <T> ResultVO<T> addTest (String path) {
IndexWriter writer=null;
try {
Directory dir =FSDirectory.open(Paths.get(path));
//指定一个分词器
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config=new IndexWriterConfig(analyzer);
writer=new IndexWriter(dir,config);
Document doc=new Document();
//数字类型使用point添加到索引中,同时如果需要存储,由于没有Stroe,所以需要再创建一个StoredField进行存储
//IntPoint,DoublePoint,LongPoint等
doc.add(new IntPoint("count", 1));
//存储
doc.add(new StoredField("count",1));
//添加排序支持
doc.add(new NumericDocValuesField("count",1));
//分别设置存储和索引
FieldType fieldType = new FieldType();
fieldType.setStored(true); // 设置为true,存储此字段
fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);//索引设置
doc.add(new Field("id_1", UUID.randomUUID().toString(), fieldType));
//存储+索引
doc.add(new TextField("id_2", UUID.randomUUID().toString(),Store.YES));
//将doc对象写入索引库,此过程创建索引,并将索引和文档对象写入索引库
writer.addDocument(doc);
writer.close();
return new ResultVO<T>(true,"创建成功");
} catch (IOException e) {
e.printStackTrace();
return new ResultVO<T>(false,"创建失败:"+ e.getMessage());
}
}
public static void main(String[] args) throws IOException {
//索引存储路径
String path = "E:\\\\message";
addTest(path);
}