-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLuceneIndexerWrapper.java
105 lines (83 loc) · 3.83 KB
/
LuceneIndexerWrapper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import net.semanticmetadata.lire.builders.GlobalDocumentBuilder;
import net.semanticmetadata.lire.imageanalysis.features.global.AutoColorCorrelogram;
import net.semanticmetadata.lire.imageanalysis.features.global.CEDD;
import net.semanticmetadata.lire.imageanalysis.features.global.FCTH;
import net.semanticmetadata.lire.indexers.parallel.ParallelIndexer;
import net.semanticmetadata.lire.builders.DocumentBuilder;
import net.semanticmetadata.lire.searchers.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.document.Document;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.nio.file.Paths;
import java.net.URI;
import java.util.List;
import java.io.*;
public class LuceneIndexerWrapper {
static String BASE_PATH; // Pseudo constant, load from LuceneIndexer
static final int NUM_THREADS = 8;
static final int NUM_RESULTS = 12;
public static void init(String basePath) throws Exception {
BASE_PATH = basePath;
System.setOut(new PrintStream(new BufferedOutputStream(new FileOutputStream(BASE_PATH + "logs/wrapper_stdout", true)), true));
System.setErr(new PrintStream(new BufferedOutputStream(new FileOutputStream(BASE_PATH + "logs/wrapper_stderr", true)), true));
}
public static void updateIndex(String basePath, String indexName) throws Exception {
init(basePath);
System.out.println("updateIndex");
System.out.println("BASE_PATH: " + BASE_PATH);
final String indexPath = BASE_PATH + indexName + "/index";
final String imagePath = BASE_PATH + indexName + "/images";
final int numOfThreads = 8;
// Run indexer only if there are actually some files. Otherwise
// an exception will be thrown.
if (new File(imagePath).list().length > 0) {
ParallelIndexer indexer = new ParallelIndexer(NUM_THREADS,
indexPath, imagePath);
indexer.addExtractor(CEDD.class);
//indexer.addExtractor(FCTH.class);
//indexer.addExtractor(AutoColorCorrelogram.class);
indexer.run();
printDebugInfo(indexName);
}
System.out.println("Finished indexing.");
}
public static void printDebugInfo(String indexName) throws Exception {
IndexReader ir = DirectoryReader.open(FSDirectory.open(Paths.get(new URI("file://" + BASE_PATH + indexName + "/index"))));
System.out.println("dbg numDocs: " + ir.numDocs());
for (int i = 0; i < ir.maxDoc(); i++) {
System.out.println("doc: " + i);
Document doc = ir.document(i);
/*List<IndexableField> allFields = doc.getFields();
for (IndexableField field : allFields)
{
System.out.println(field.name() + " " + field.stringValue());
}*/
String docFileName = doc.getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
System.out.println("docFileName: " + docFileName);
}
}
public static String[] search(InputStream compareImageStream, String basePath, String indexName) throws Exception {
init(basePath);
System.out.println("search");
BufferedImage img = ImageIO.read(compareImageStream);
IndexReader ir = DirectoryReader.open(FSDirectory.open(Paths.get(new URI("file://" + BASE_PATH + indexName + "/index"))));
ImageSearcher searcher = new GenericFastImageSearcher(NUM_RESULTS, CEDD.class);
ImageSearchHits hits = searcher.search(img, ir);
String[] results = new String[hits.length()];
for (int i = 0; i < hits.length(); i++) {
String path = ir.document(hits.documentID(i)).getValues(DocumentBuilder.FIELD_NAME_IDENTIFIER)[0];
String fileName = path.substring(path.lastIndexOf('/') + 1, path.length() );
System.out.println(hits.score(i) + ": \t" + fileName);
results[i] = fileName;
}
return results;
}
}