Skip to content

Commit

Permalink
....
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangyongbing24 committed Jul 30, 2019
0 parents commit 5b46686
Show file tree
Hide file tree
Showing 15 changed files with 414 additions and 0 deletions.
Binary file added Indexer/_0.cfe
Binary file not shown.
Binary file added Indexer/_0.cfs
Binary file not shown.
Binary file added Indexer/_0.si
Binary file not shown.
Binary file added Indexer/segments_1
Binary file not shown.
Empty file added Indexer/write.lock
Empty file.
Binary file added index/_0.cfe
Binary file not shown.
Binary file added index/_0.cfs
Binary file not shown.
Binary file added index/_0.si
Binary file not shown.
Binary file added index/segments_1
Binary file not shown.
Empty file added index/write.lock
Empty file.
139 changes: 139 additions & 0 deletions src/com/yiibai/lucene/IndexFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
package com.yiibai.lucene;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.*;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;

public class IndexFiles {
private IndexFiles() {
}

public static void main(String[] args) {
String usage = "java org.apache.lucene.demo.IndexFiles [-index INDEX_PATH] [-docs DOCS_PATH] [-update]\n\nThis indexes the documents in DOCS_PATH, creating a Lucene indexin INDEX_PATH that can be searched with SearchFiles";
String indexPath = "index";
String docsPath = "/home/jiangyongbing/mydir/CommandMemo";
boolean create = true;

for(int i = 0; i < args.length; ++i) {
if ("-index".equals(args[i])) {
indexPath = args[i + 1];
++i;
} else if ("-docs".equals(args[i])) {
docsPath = args[i + 1];
++i;
} else if ("-update".equals(args[i])) {
create = false;
}
}

if (docsPath == null) {
System.err.println("Usage: " + usage);
System.exit(1);
}

Path docDir = Paths.get(docsPath);
if (!Files.isReadable(docDir)) {
System.out.println("Document directory '" + docDir.toAbsolutePath() + "' does not exist or is not readable, please check the path");
System.exit(1);
}

Date start = new Date();

try {
System.out.println("Indexing to directory '" + indexPath + "'...");
Directory dir = FSDirectory.open(Paths.get(indexPath));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
if (create) {
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
} else {
iwc.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
}

IndexWriter writer = new IndexWriter(dir, iwc);
indexDocs(writer, docDir);
writer.close();
Date end = new Date();
System.out.println(end.getTime() - start.getTime() + " total milliseconds");
} catch (IOException var12) {
System.out.println(" caught a " + var12.getClass() + "\n with message: " + var12.getMessage());
}

}

static void indexDocs(final IndexWriter writer, Path path) throws IOException {
if (Files.isDirectory(path, new LinkOption[0])) {
Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
try {
IndexFiles.indexDoc(writer, file, attrs.lastModifiedTime().toMillis());
} catch (IOException var4) {
}

return FileVisitResult.CONTINUE;
}
});
} else {
indexDoc(writer, path, Files.getLastModifiedTime(path).toMillis());
}

}

static void indexDoc(IndexWriter writer, Path file, long lastModified) throws IOException {
InputStream stream = Files.newInputStream(file);
Throwable var5 = null;

try {
Document doc = new Document();
Field pathField = new StringField("path", file.toString(), Field.Store.YES);
doc.add(pathField);
doc.add(new LongPoint("modified", new long[]{lastModified}));
doc.add(new TextField("contents", new BufferedReader(new InputStreamReader(stream, StandardCharsets.UTF_8))));
if (writer.getConfig().getOpenMode() == IndexWriterConfig.OpenMode.CREATE) {
System.out.println("adding " + file);
writer.addDocument(doc);
} else {
System.out.println("updating " + file);
writer.updateDocument(new Term("path", file.toString()), doc);
}
} catch (Throwable var15) {
var5 = var15;
throw var15;
} finally {
if (stream != null) {
if (var5 != null) {
try {
stream.close();
} catch (Throwable var14) {
var5.addSuppressed(var14);
}
} else {
stream.close();
}
}

}

}
}







42 changes: 42 additions & 0 deletions src/com/yiibai/lucene/Indexer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.yiibai.lucene;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableFieldType;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Paths;

public class Indexer {
public IndexWriter writer;

public Indexer(String indexDirectoryPath) throws IOException {
Directory indexDirectory =
FSDirectory.open(Paths.get(indexDirectoryPath));
IndexWriterConfig config = new IndexWriterConfig(new StandardAnalyzer());

writer = new IndexWriter(indexDirectory,config);

}

public void close() throws IOException{
writer.close();
}

public Document getDocument(File file) throws IOException{
Document document = new Document();

Field contentField = new StringField("path", file.toString(), Field.Store.YES);

return null;
}
}
11 changes: 11 additions & 0 deletions src/com/yiibai/lucene/LuceneConstants.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.yiibai.lucene;

public class LuceneConstants {
public static final String CONTENTS = "contents";

public static final String FILE_NAME = "filename";

public static final String FILE_PATH = "filepath";

public static final int MAX_SEARCH = 10;
}
Loading

0 comments on commit 5b46686

Please sign in to comment.