My favorites | Sign in
Project Logo
                
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
*
*/
package search.test;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopFieldDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.LockObtainFailedException;
import org.junit.Before;
import org.junit.Test;

/**
* @author tangfulin
*
*/
public class IndexTest {

private final String idxPath = "/home/tangfulin/svn/data/search2";
private final Analyzer analyzer = new StandardAnalyzer();

IndexWriter writer;
IndexSearcher searcher;

private void buildIdx(){
try {
writer = new IndexWriter(idxPath, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);

for (int i=0; i<10; ++i){
Document doc = new Document();
Field f0 = new Field("num", "" + i, Field.Store.YES, Field.Index.ANALYZED);
Field f1 = new Field("title", "test NO." + i, Field.Store.YES, Field.Index.ANALYZED);
Field f2 = new Field("content", "this is my test NO." +i, Field.Store.YES, Field.Index.ANALYZED);

doc.add(f0);
doc.add(f1);
doc.add(f2);
writer.addDocument(doc);
}

writer.optimize();
writer.close();

} catch (Exception e) {
e.printStackTrace();
}
}

private void printFiles(String path){
File[] files = (new File(path)).listFiles();
System.out.println("-------------");
for (File f : files){
System.out.println("file: " + f.getName() + " \t time:" + f.lastModified()%1000000/1000);
}
System.out.println("-------------");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}

private void cleanFiles(String path){
File[] files = (new File(path)).listFiles();
for (File f : files){
f.delete();
}
}

private IndexSearcher getSearcher(){
Directory dir;
IndexSearcher se;
try {
dir = FSDirectory.getDirectory(idxPath);
se = new IndexSearcher(IndexReader.open(dir, true));

} catch (IOException e) {
e.printStackTrace();
return null;
}

return se;
}

private IndexSearcher reopenSearcher(IndexSearcher searcher){
IndexReader reader = searcher.getIndexReader();
try {
IndexReader newReader = reader.reopen();
if (newReader != reader){
System.out.println("new reader!");
reader.close();
reader = newReader;
searcher = new IndexSearcher(reader);
}
else {
System.out.println("reader stay unchanged!");
}
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

return searcher;
}

private void searchIdx(IndexSearcher searcher, String field, String text){
try {
TopFieldDocs topFieldDocs = searcher.search(new TermQuery(new Term(field, text)), null, 100, Sort.RELEVANCE);
ScoreDoc[] hits = topFieldDocs.scoreDocs;
for (int i = 0; i < hits.length; ++i) {
Document doc = searcher.doc(hits[i].doc);
System.out.println("ScoreDoc No." + i + " : " + hits[i].doc + " score:" + hits[i].score + "fields: " + doc.getFields());
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("-------------");
}

@Before
public void setUp() {
cleanFiles(idxPath);
buildIdx();
}

@Test
public final void indexTest(){

searcher = getSearcher();

searcher = reopenSearcher(searcher);

printFiles(idxPath);
System.out.println("before delete");
searchIdx(searcher, "title", "test");

searcher = reopenSearcher(searcher);

try {
writer = new IndexWriter(idxPath, analyzer, IndexWriter.MaxFieldLength.UNLIMITED);
writer.deleteDocuments(new Term("title", "test"));
//writer.optimize();
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

System.out.println("after delete");
printFiles(idxPath);
searchIdx(searcher, "title", "test");

System.out.println("after searcher reopen");
searcher = reopenSearcher(searcher);
searchIdx(searcher, "title", "test");

}

//@Test
public final void searchTest(){
searcher = getSearcher();
searchIdx(searcher, "title", "test");

}

}
Show details Hide details

Change log

r30 by tangfulin on May 11, 2009   Diff
lucene staff
Go to: 
Project members, sign in to write a code review

Older revisions

All revisions of this file

File info

Size: 4707 bytes, 184 lines
Hosted by Google Code