建立索引的基本过程:
private void createIndexFileForEntities() throws SearchException
{
try
{
cleanDirectory(this.indexEntitiesPath);
File dir = new File(entitiesPath);
File[] files = dir.listFiles((FileFilter) new GlobFilenameFilter("*.xls"));
MemoryDirectory directory = new MemoryDirectory();
// FSDirectory directory = FSDirectory.getDirectory(this.indexEntitiesPath);
if (files != null)
{
IndexWriter writer = new IndexWriter(directory, new StandardAnalyzer(), true);
for (File file : files)
{
List<ImportEntity> entities = EntityExcelReader.getEntities(file.getAbsolutePath());
for (int i = 0; i < entities.size(); i++)
{
ImportEntity de = entities.get(i);
Document doc = new Document();
doc.add(new Field(ONTOLOGY_ID, de.getOntologyID(), Field.Store.YES, Field.Index.UN_TOKENIZED));
List<String> names = de.getAttributeNames();
for (String name : names)
{
String value = de.getAttribute(name);
Field field = null;
if (name.equalsIgnoreCase(AGE) || name.equalsIgnoreCase(SALARY))
{
long lvalue = Long.parseLong(value);
String strvalue = NumberTools.longToString(lvalue);
field = new Field(name, strvalue, Field.Store.YES, Field.Index.UN_TOKENIZED);
}
else if (name.equalsIgnoreCase(BIRTHDAY))
{
String strvalue = formatDateToString(value);
field = new Field(name, strvalue, Field.Store.YES, Field.Index.UN_TOKENIZED);
}
else
{
field = new Field(name, value, Field.Store.YES, Field.Index.TOKENIZED);
}
doc.add(field);
}
doc.setBoost(0.6f);
writer.addDocument(doc);
}
}
writer.optimize();
writer.close();
System.out.println("RAM size:" + directory.sizeInBytes());
FSDirectory fsDirectory = FSDirectory.getDirectory(this.indexEntitiesPath);
directory.copyToDirectory(fsDirectory);
}
}
catch (Exception ex)
{
ex.printStackTrace();
throw new SearchException(ex);
}
}
添加索引的基本过程:
private void appendIndexFileForEntities() throws SearchException
{
try
{
FSDirectory fsDirectory = FSDirectory.getDirectory(this.indexEntitiesPath);
IndexWriter writer = new IndexWriter(fsDirectory, new StandardAnalyzer(), true);
for (int i = 1; i <= 10; i++)
{
Document doc = new Document();
doc.add(new Field(ONTOLOGY_ID, "", Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.add(new Field(NAME, "Theaters" + String.valueOf(1), Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field(AGE, LongTools.longToString(10 * i + 5), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.add(new Field(AGE_TEN, LongTools.longToString(i), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc.add(new Field(SALARY, LongTools.longToString(5), Field.Store.YES, Field.Index.UN_TOKENIZED));
doc
.add(new Field(BIRTHDAY, formatDateToString("2005-11-4"), Field.Store.YES,
Field.Index.UN_TOKENIZED));
doc.add(new Field(DESCRIPTION, "only a test string", Field.Store.YES, Field.Index.TOKENIZED));
doc.setBoost(0.7f);
writer.addDocument(doc);
}
writer.optimize();
writer.close();
}
catch (Exception ex)
{
throw new SearchException(ex);
}
}
利用索引查询的过程:
private void queryIndexFileForEntities() throws SearchException
{
try
{
MemoryDirectory ramDir = new MemoryDirectory(FSDirectory.getDirectory(this.indexEntitiesPath));
IndexReader reader = IndexReader.open(ramDir);
// IndexReader reader = IndexReader.open(this.indexEntitiesPath);
Searcher searcher = new IndexSearcher(reader);
Hits hits = null;
hits = this.queryBySearcher(searcher);
for (int i = 0; i < hits.length(); i++)
{
Document doc = hits.doc(i);
System.out.println("Hit Document Index: " + Integer.toString(i + 1));
System.out.println("doc=" + hits.id(i) + " score=" + hits.score(i));
System.out.println("hits boost:" + doc.getBoost());
System.out.println(getDocumentItem(doc));
}
}
catch (Exception ex)
{
ex.printStackTrace();
throw new SearchException(ex);
}
}
编码函数:
private String formatDateToString(String strdate) throws ParseException
{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Date date = sdf.parse(strdate);
return DateTools.dateToString(date, DateTools.Resolution.DAY);
}
辅助清空目录函数:
private void cleanDirectory(String path) throws SearchException
{
try
{
File dir = new File(path);
File[] files = dir.listFiles();
if (files != null)
{
for (File file : files)
{
if (file.isFile())
{
file.delete(); // remove all the index files under that directory
}
}
}
}
catch (Exception ex)
{
throw new SearchException(ex);
}
}
分享到:
相关推荐
赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
赠送jar包:lucene-analyzers-common-6.6.0.jar; 赠送原API文档:lucene-analyzers-common-6.6.0-javadoc.jar; 赠送源代码:lucene-analyzers-common-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
赠送jar包:lucene-core-7.2.1.jar; 赠送原API文档:lucene-core-7.2.1-javadoc.jar; 赠送源代码:lucene-core-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.2.1.pom; 包含翻译后的API文档:lucene...
赠送jar包:lucene-core-7.7.0.jar; 赠送原API文档:lucene-core-7.7.0-javadoc.jar; 赠送源代码:lucene-core-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-7.7.0.pom; 包含翻译后的API文档:lucene...
赠送jar包:lucene-suggest-6.6.0.jar; 赠送原API文档:lucene-suggest-6.6.0-javadoc.jar; 赠送源代码:lucene-suggest-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-suggest-6.6.0.pom; 包含翻译后的API...
lucene-core-2.9.4,lucene-core-3.0.2,lucene-core-3.0.3,lucene-core-3.4.0
赠送jar包:lucene-backward-codecs-7.3.1.jar; 赠送原API文档:lucene-backward-codecs-7.3.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-analyzers-smartcn-7.7.0.jar; 赠送原API文档:lucene-analyzers-smartcn-7.7.0-javadoc.jar; 赠送源代码:lucene-analyzers-smartcn-7.7.0-sources.jar; 赠送Maven依赖信息文件:lucene-...
赠送jar包:lucene-spatial-extras-7.3.1.jar; 赠送原API文档:lucene-spatial-extras-7.3.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.3.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
赠送jar包:lucene-spatial-extras-7.2.1.jar; 赠送原API文档:lucene-spatial-extras-7.2.1-javadoc.jar; 赠送源代码:lucene-spatial-extras-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
赠送jar包:lucene-spatial-extras-6.6.0.jar; 赠送原API文档:lucene-spatial-extras-6.6.0-javadoc.jar; 赠送源代码:lucene-spatial-extras-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-spatial-extras...
赠送jar包:lucene-backward-codecs-7.2.1.jar; 赠送原API文档:lucene-backward-codecs-7.2.1-javadoc.jar; 赠送源代码:lucene-backward-codecs-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-sandbox-7.2.1.jar; 赠送原API文档:lucene-sandbox-7.2.1-javadoc.jar; 赠送源代码:lucene-sandbox-7.2.1-sources.jar; 赠送Maven依赖信息文件:lucene-sandbox-7.2.1.pom; 包含翻译后的API...
lucene jar包,lucene,lucene-core-3.0.2.jar;lucene-demos-3.0.2.jar
赠送jar包:lucene-core-6.6.0.jar; 赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene...
赠送jar包:lucene-core-6.6.0.jar; 赠送原API文档:lucene-core-6.6.0-javadoc.jar; 赠送源代码:lucene-core-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-core-6.6.0.pom; 包含翻译后的API文档:lucene...
lucene-analyzers-common-4.3.0.jar需要的可以下载,搬运
赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-backward-codecs-6.6.0.jar; 赠送原API文档:lucene-backward-codecs-6.6.0-javadoc.jar; 赠送源代码:lucene-backward-codecs-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-backward-...
赠送jar包:lucene-memory-6.6.0.jar; 赠送原API文档:lucene-memory-6.6.0-javadoc.jar; 赠送源代码:lucene-memory-6.6.0-sources.jar; 赠送Maven依赖信息文件:lucene-memory-6.6.0.pom; 包含翻译后的API文档...