/
SearchService.java
179 lines (149 loc) · 6.28 KB
/
SearchService.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
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
package allwrite.service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.util.Version;
import org.mortbay.log.Log;
import org.slim3.memcache.Memcache;
import org.slim3.datastore.Datastore;
import org.slim3.datastore.EntityNotFoundRuntimeException;
import util.NGramAnalyzerForQuery;
import util.NotAuthorizedException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.memcache.MemcacheServicePb.MemcacheGetRequest;
import allwrite.model.Index;
import allwrite.model.Note;
import allwrite.model.UserInfo;
import allwrite.meta.IndexMeta;
import allwrite.meta.NoteMeta;
public class SearchService {
static final Logger log = Logger.getLogger(SearchService.class.getName());
private static final int MAX_RESULT_SIZE = 30;
private IndexMeta i = new IndexMeta();
public Query getQuery(){
String q = "[????-??-??]";
Term term = new Term("text", q);
WildcardQuery query = new WildcardQuery(term);
return query;
}
public Object[] getIndexes(UserInfo user){
Map<Object, Object> map = Memcache.getAll(user.getIndexes());
if (map.size() < user.getIndexes().size()){
// some indexes are out of Memcache.
List<Index> idxList = Datastore.get(i,user.getIndexes());
for(Index idx: idxList){
if (! map.containsValue(idx)){
Memcache.put(idx.getKey(), idx); // put to Memcache
}
}
return idxList.toArray();
}else{
return map.values().toArray();
}
}
public List<Map<String, String>> Search(Map<String, Object> input) throws CorruptIndexException, IOException, ClassNotFoundException, ParseException, NotAuthorizedException {
String email = (String)input.get("email");
String token = (String)input.get("auth");
// long start;
// start = System.currentTimeMillis();
// log.info("Search start:" + start);
UserInfo user = Util.findUser(email);
if (!user.getTokens().contains(token)){
throw new NotAuthorizedException();
}
// get indexes.
Object[] result = getIndexes(user);
// log.info("Search get Indexes:" + (System.currentTimeMillis() - start));
// create a MultiReader from indexes.
IndexReader[] idxReaders = new IndexReader[result.length];
int c=0;
if (result.length == 0){
return null;
}
for(Object iO: result){
try{
Index idx = (Index)iO;
IndexReader r = IndexReader.open(idx.getIndex(), true); // create Read only indexreader.
idxReaders[c] = r;
c++;
}catch(ClassCastException e){
continue; // just skip
}
}
// log.info("Search get multiple index:" + (System.currentTimeMillis() - start));
MultiReader mReader = new MultiReader(idxReaders, true);
IndexSearcher searcher = new IndexSearcher(mReader);
// log.info("Search get MultiReader:" + (System.currentTimeMillis() - start));
Query query;
String q;
if (input.containsKey("schedule")){
q = "\\[????\\-??\\-??\\]";
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
query = new QueryParser(Version.LUCENE_CURRENT, "text", analyzer).parse(q);
}else{
q = (String)input.get("q");
query = new QueryParser(Version.LUCENE_CURRENT, "text", new NGramAnalyzerForQuery(1,3)).parse(q);
}
System.out.println("Query:" + query.toString());
// log.info("Search start:" + (System.currentTimeMillis() - start));
TopDocs td = searcher.search(query, MAX_RESULT_SIZE);
// log.info("Search end:" + (System.currentTimeMillis() - start));
// get note keys
Document doc;
c = 0;
String[] keys = new String[td.scoreDocs.length];
System.out.println("result: " + td.scoreDocs.length);
for(ScoreDoc sd: td.scoreDocs){
doc = searcher.doc(sd.doc);
keys[c] = doc.get("key");
c++;
}
// log.info("Search get doc:" + (System.currentTimeMillis() - start));
List<Note> notes = findNotes(keys);
List<Map<String,String>> retval = new ArrayList<Map<String,String>>();
for(Note note: notes){
Map<String, String> map = new HashMap<String, String>();
map.put("title", note.getTitle());
map.put("text", note.getText());
map.put("created", note.getCreatedDate().toString());
map.put("noteid", note.getNoteId());
retval.add(map);
}
// log.info("Search create Return:" + (System.currentTimeMillis() - start));
return retval;
}
public static List<Note> findNotes(String[] keys){
NoteMeta n = new NoteMeta();
List<Note> notes = new ArrayList<Note>(keys.length);
List<Key> keyList = new ArrayList<Key>(keys.length);
for(String key: keys){
Key mkey = KeyFactory.stringToKey(key);
keyList.add(mkey);
}
try{
notes = Datastore.get(n, keyList);
}catch(EntityNotFoundRuntimeException e){
log.warning("some entity is not found in findNotes. index is broken?");
return null;
}
return notes;
}
}