/
SoftDeletesTest2.java
81 lines (76 loc) · 3.12 KB
/
SoftDeletesTest2.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
package io.softDeletes;
import io.FileOperation;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.MMapDirectory;
import java.io.IOException;
import java.nio.file.Paths;
/**
* @author Lu Xugang
* @date 2020/6/15 8:52 下午
*/
public class SoftDeletesTest2 {
private Directory directory;
{
try {
FileOperation.deleteFile("./data");
directory = new MMapDirectory(Paths.get("./data"));
} catch (IOException e) {
e.printStackTrace();
}
}
// 放在方法外 这个变量能高亮显示
private IndexWriter indexWriter;
IndexWriterConfig indexWriterConfig;
public void doIndexAndSearch() throws Exception {
indexWriterConfig = new IndexWriterConfig(new WhitespaceAnalyzer());
String softDeletesField = "softDeleteField";
indexWriterConfig.setSoftDeletesField(softDeletesField);
indexWriterConfig.setUseCompoundFile(false);
indexWriterConfig.setMergePolicy(NoMergePolicy.INSTANCE);
indexWriter = new IndexWriter(directory, indexWriterConfig);
Document doc;
// 文档0
doc = new Document();
doc.add(new StringField("abc", "document1", Field.Store.YES));
doc.add(new NumericDocValuesField(softDeletesField, 4));
indexWriter.addDocument(doc);
// 文档1
doc = new Document();
doc.add(new StringField("abc", "document3", Field.Store.YES));
indexWriter.addDocument(doc);
// 文档2
doc = new Document();
doc.add(new StringField("abc", "document2", Field.Store.YES));
indexWriter.addDocument(doc);
// 生成一个段
indexWriter.commit();
// 文档3
doc = new Document();
doc.add(new StringField("abc", "document2", Field.Store.YES));
indexWriter.addDocument(doc);
// 文档4
Document newDoc = new Document();
newDoc.add(new StringField("abc", "document3", Field.Store.YES));
indexWriter.softUpdateDocument(new Term("abc", "document3"), newDoc, new NumericDocValuesField(softDeletesField, 3));
// 生成一个段
indexWriter.commit();
DirectoryReader readerBeforeMerge = DirectoryReader.open(indexWriter);
ScoreDoc[] scoreDocs = (new IndexSearcher(readerBeforeMerge)).search(new MatchAllDocsQuery(), 100).scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
System.out.println("docId: 文档" + scoreDoc.doc + ", FieldValue of Field abc: " + readerBeforeMerge.document(scoreDoc.doc).get("abc") + "");
}
}
public static void main(String[] args) throws Exception{
SoftDeletesTest2 test = new SoftDeletesTest2();
test.doIndexAndSearch();
}
}