Project

General

Profile

1
package edu.ucsb.nceas.metacat.index;
2

    
3
import org.apache.lucene.analysis.standard.StandardAnalyzer;
4
import org.apache.lucene.document.Document;
5
import org.apache.lucene.document.Field;
6
import org.apache.lucene.document.StringField;
7
import org.apache.lucene.document.TextField;
8
import org.apache.lucene.index.*;
9
import org.apache.lucene.queryparser.classic.ParseException;
10
import org.apache.lucene.queryparser.classic.QueryParser;
11
import org.apache.lucene.search.IndexSearcher;
12
import org.apache.lucene.search.ScoreDoc;
13
import org.apache.lucene.search.TopDocs;
14
import org.apache.lucene.store.Directory;
15
import org.apache.lucene.store.FSDirectory;
16
import org.apache.lucene.util.Version;
17

    
18
import java.io.File;
19
import java.io.IOException;
20
import java.util.Map;
21

    
22
public class LuceneIndex implements GenericIndex {
23
	protected final StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_41);
24
	protected final IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_41, analyzer);
25
	protected final QueryParser parser = new QueryParser(Version.LUCENE_41, MetacatIndex.MCIDFIELD, analyzer);
26
	protected IndexWriter writer = null;
27
	protected DirectoryReader reader = null;
28
	protected Directory index = null;
29

    
30
	public LuceneIndex(File fsIndex) {
31
		try {
32
			this.index = FSDirectory.open(fsIndex);
33
			this.cfg.setOpenMode(IndexWriterConfig.OpenMode.APPEND);
34
			this.writer = new IndexWriter(this.index, this.cfg);
35
			this.reader = DirectoryReader.open(this.index);
36
		} catch (Exception e) {
37
			e.printStackTrace();
38
		}
39
	}
40

    
41
	public void insert(String docID, Map<String, String[]> fields) throws IOException {
42
		this.update(docID, fields);
43
	}
44

    
45
	public void update(String docID, Map<String, String[]> fields) throws IOException {
46
		Document d = new Document();
47
		d.add(new StringField(MetacatIndex.MCIDFIELD, docID, Field.Store.YES));
48
		for (String k : fields.keySet()) {
49
			for (String v : fields.get(k)) {
50
				d.add(new TextField(k, v, Field.Store.NO));
51
			}
52
		}
53
		this.writer.addDocument(d);
54
		this.writer.commit();
55
	}
56

    
57
	public void remove(String docID) throws IOException {
58
		this.writer.deleteDocuments(new Term(MetacatIndex.MCIDFIELD, docID));
59
		this.writer.commit();
60
	}
61

    
62
	public String[] query(String q) {
63
		try {
64
			DirectoryReader newReader = DirectoryReader.openIfChanged(this.reader);
65
			if (newReader != null) { // not sure if this is right...
66
				this.reader.close();
67
				this.reader = newReader;
68
			}
69
			IndexSearcher searcher = new IndexSearcher(this.reader);
70
			TopDocs docs = searcher.search(this.parser.parse(q), 100);
71
			String docIDs[] = new String[docs.scoreDocs.length];
72
			int i = 0;
73
			for (ScoreDoc sd : docs.scoreDocs) {
74
				Document d = searcher.doc(sd.doc);
75
				docIDs[i++] = d.getField(MetacatIndex.MCIDFIELD).toString();
76
			}
77
			return docIDs;
78
		} catch (IOException e) {
79
			e.printStackTrace();
80
		} catch (ParseException e) {
81
			e.printStackTrace();
82
		}
83
		return new String[0];
84
	}
85
}
(9-9/14)