comparison src/luan/modules/lucene/LuceneIndex.java @ 1346:efd1c6380f2c

minor
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 25 Feb 2019 12:29:33 -0700
parents 6f8988830098
children 643cf1c37723
comparison
equal deleted inserted replaced
1345:6f8988830098 1346:efd1c6380f2c
211 BytesRef br = new BytesRef(); 211 BytesRef br = new BytesRef();
212 NumericUtils.longToPrefixCoded(value,0,br); 212 NumericUtils.longToPrefixCoded(value,0,br);
213 return new Term(key,br); 213 return new Term(key,br);
214 } 214 }
215 215
216 public void delete(Luan luan,String queryStr) 216 public void delete(String queryStr)
217 throws LuanException, IOException, ParseException 217 throws IOException, ParseException
218 { 218 {
219 Query query = SaneQueryParser.parseQuery(mfp,queryStr); 219 Query query = SaneQueryParser.parseQuery(mfp,queryStr);
220 220
221 boolean commit = !writeLock.isHeldByCurrentThread(); 221 boolean commit = !writeLock.isHeldByCurrentThread();
222 writeLock.lock(); 222 writeLock.lock();
231 231
232 public void indexed_only_fields(List<String> fields) { 232 public void indexed_only_fields(List<String> fields) {
233 indexOnly.addAll(fields); 233 indexOnly.addAll(fields);
234 } 234 }
235 235
236 public void save(Luan luan,LuanTable doc,LuanTable boosts) 236 public void save(LuanTable doc,LuanTable boosts)
237 throws LuanException, IOException 237 throws LuanException, IOException
238 { 238 {
239 Object obj = doc.get("id"); 239 Object obj = doc.get("id");
240 Long id; 240 Long id;
241 try { 241 try {
246 246
247 boolean commit = !writeLock.isHeldByCurrentThread(); 247 boolean commit = !writeLock.isHeldByCurrentThread();
248 writeLock.lock(); 248 writeLock.lock();
249 try { 249 try {
250 if( id == null ) { 250 if( id == null ) {
251 id = nextId(luan); 251 id = nextId();
252 doc.put("id",id); 252 doc.put("id",id);
253 writer.addDocument(toLucene(doc,boosts)); 253 writer.addDocument(toLucene(doc,boosts));
254 } else { 254 } else {
255 writer.updateDocument( term("id",id), toLucene(doc,boosts) ); 255 writer.updateDocument( term("id",id), toLucene(doc,boosts) );
256 } 256 }
306 default: 306 default:
307 throw new RuntimeException(); 307 throw new RuntimeException();
308 } 308 }
309 } 309 }
310 310
311 public synchronized long nextId(Luan luan) throws LuanException, IOException { 311 public synchronized long nextId() throws LuanException, IOException {
312 if( ++id > idLim ) { 312 if( ++id > idLim ) {
313 idLim += idBatch; 313 idLim += idBatch;
314 LuanTable doc = new LuanTable(luan); 314 Map doc = new HashMap();
315 doc.rawPut( "type", "next_id" ); 315 doc.put( "type", "next_id" );
316 doc.rawPut( FLD_NEXT_ID, idLim ); 316 doc.put( FLD_NEXT_ID, idLim );
317 writer.updateDocument(new Term("type","next_id"),toLucene(doc,null)); 317 writer.updateDocument(new Term("type","next_id"),toLucene(doc.entrySet(),null));
318 wrote(); 318 wrote();
319 } 319 }
320 return id; 320 return id;
321 } 321 }
322 322
342 */ 342 */
343 public SnapshotDeletionPolicy snapshotDeletionPolicy() { 343 public SnapshotDeletionPolicy snapshotDeletionPolicy() {
344 return snapshotDeletionPolicy; 344 return snapshotDeletionPolicy;
345 } 345 }
346 346
347 public Object snapshot(Luan luan,LuanFunction fn) throws LuanException, IOException { 347 public Object snapshot(LuanFunction fn) throws LuanException, IOException {
348 IndexCommit ic = snapshotDeletionPolicy.snapshot(); 348 IndexCommit ic = snapshotDeletionPolicy.snapshot();
349 try { 349 try {
350 String dir = fileDir.toString(); 350 String dir = fileDir.toString();
351 LuanTable fileNames = new LuanTable(luan,new ArrayList(ic.getFileNames())); 351 LuanTable fileNames = new LuanTable(fn.luan(),new ArrayList(ic.getFileNames()));
352 return fn.call(dir,fileNames); 352 return fn.call(dir,fileNames);
353 } finally { 353 } finally {
354 snapshotDeletionPolicy.release(ic); 354 snapshotDeletionPolicy.release(ic);
355 } 355 }
356 } 356 }
439 439
440 public void ensure_open() throws IOException { 440 public void ensure_open() throws IOException {
441 close(openSearcher()); 441 close(openSearcher());
442 } 442 }
443 443
444 public int advanced_search( final Luan luan, String queryStr, LuanFunction fn, Integer n, String sortStr ) 444 public int advanced_search( String queryStr, LuanFunction fn, Integer n, String sortStr )
445 throws LuanException, IOException, ParseException 445 throws LuanException, IOException, ParseException
446 { 446 {
447 Utils.checkNotNull(queryStr); 447 Utils.checkNotNull(queryStr);
448 Query query = SaneQueryParser.parseQuery(mfp,queryStr); 448 Query query = SaneQueryParser.parseQuery(mfp,queryStr);
449 IndexSearcher searcher = threadLocalSearcher.get(); 449 IndexSearcher searcher = threadLocalSearcher.get();
452 searcher = openSearcher(); 452 searcher = openSearcher();
453 try { 453 try {
454 if( fn!=null && n==null ) { 454 if( fn!=null && n==null ) {
455 if( sortStr != null ) 455 if( sortStr != null )
456 throw new LuanException("sort must be nil when n is nil"); 456 throw new LuanException("sort must be nil when n is nil");
457 final DocFn docFn = new DocFn(luan,searcher,query); 457 final DocFn docFn = new DocFn(fn.luan(),searcher,query);
458 MyCollector col = new MyCollector() { 458 MyCollector col = new MyCollector() {
459 @Override public void collect(int doc) { 459 @Override public void collect(int doc) {
460 try { 460 try {
461 docFn.docID = docBase + doc; 461 docFn.docID = docBase + doc;
462 fn.call(++i,docFn); 462 fn.call(++i,docFn);
478 return thcc.getTotalHits(); 478 return thcc.getTotalHits();
479 } 479 }
480 Sort sort = sortStr==null ? null : SaneQueryParser.parseSort(mfp,sortStr); 480 Sort sort = sortStr==null ? null : SaneQueryParser.parseSort(mfp,sortStr);
481 TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort); 481 TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort);
482 final ScoreDoc[] scoreDocs = td.scoreDocs; 482 final ScoreDoc[] scoreDocs = td.scoreDocs;
483 DocFn docFn = new DocFn(luan,searcher,query); 483 DocFn docFn = new DocFn(fn.luan(),searcher,query);
484 for( int i=0; i<scoreDocs.length; i++ ) { 484 for( int i=0; i<scoreDocs.length; i++ ) {
485 ScoreDoc scoreDoc = scoreDocs[i]; 485 ScoreDoc scoreDoc = scoreDocs[i];
486 docFn.docID = scoreDoc.doc; 486 docFn.docID = scoreDoc.doc;
487 fn.call(i+1,docFn,scoreDoc.score); 487 fn.call(i+1,docFn,scoreDoc.score);
488 } 488 }
576 } else 576 } else
577 throw new LuanException("invalid value type "+value.getClass()+"' for '"+name+"'"); 577 throw new LuanException("invalid value type "+value.getClass()+"' for '"+name+"'");
578 } 578 }
579 579
580 private Document toLucene(LuanTable table,LuanTable boosts) throws LuanException { 580 private Document toLucene(LuanTable table,LuanTable boosts) throws LuanException {
581 return toLucene(table,boosts);
582 }
583
584 private Document toLucene(Iterable<Map.Entry> iterable,LuanTable boosts) throws LuanException {
581 Set<String> indexed = mfp.fields.keySet(); 585 Set<String> indexed = mfp.fields.keySet();
582 Document doc = new Document(); 586 Document doc = new Document();
583 for( Map.Entry<Object,Object> entry : table.iterable() ) { 587 for( Map.Entry<Object,Object> entry : iterable ) {
584 Object key = entry.getKey(); 588 Object key = entry.getKey();
585 if( !(key instanceof String) ) 589 if( !(key instanceof String) )
586 throw new LuanException("key must be string"); 590 throw new LuanException("key must be string");
587 String name = (String)key; 591 String name = (String)key;
588 Object value = entry.getValue(); 592 Object value = entry.getValue();