Mercurial Hosting > luan
comparison src/luan/modules/lucene/LuceneIndex.java @ 1342:60599adc27b8
add lucene search options
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Wed, 20 Feb 2019 12:09:51 -0700 |
parents | a015a0b5c388 |
children | 7d9a1f8894b0 |
comparison
equal
deleted
inserted
replaced
1341:a015a0b5c388 | 1342:60599adc27b8 |
---|---|
51 import org.apache.lucene.search.IndexSearcher; | 51 import org.apache.lucene.search.IndexSearcher; |
52 import org.apache.lucene.search.TotalHitCountCollector; | 52 import org.apache.lucene.search.TotalHitCountCollector; |
53 import org.apache.lucene.search.ScoreDoc; | 53 import org.apache.lucene.search.ScoreDoc; |
54 import org.apache.lucene.search.Collector; | 54 import org.apache.lucene.search.Collector; |
55 import org.apache.lucene.search.Scorer; | 55 import org.apache.lucene.search.Scorer; |
56 import org.apache.lucene.search.BooleanClause; | 56 import org.apache.lucene.search.Explanation; |
57 import org.apache.lucene.search.highlight.Formatter; | 57 import org.apache.lucene.search.highlight.Formatter; |
58 import org.apache.lucene.search.highlight.Highlighter; | 58 import org.apache.lucene.search.highlight.Highlighter; |
59 import org.apache.lucene.search.highlight.InvalidTokenOffsetsException; | 59 import org.apache.lucene.search.highlight.InvalidTokenOffsetsException; |
60 import org.apache.lucene.search.highlight.Fragmenter; | 60 import org.apache.lucene.search.highlight.Fragmenter; |
61 import org.apache.lucene.search.highlight.NullFragmenter; | 61 import org.apache.lucene.search.highlight.NullFragmenter; |
353 | 353 |
354 | 354 |
355 | 355 |
356 private static class DocFn extends LuanFunction { | 356 private static class DocFn extends LuanFunction { |
357 final IndexSearcher searcher; | 357 final IndexSearcher searcher; |
358 final Query query; | |
358 int docID; | 359 int docID; |
359 | 360 |
360 DocFn(Luan luan,IndexSearcher searcher) { | 361 DocFn(Luan luan,IndexSearcher searcher,Query query) { |
361 super(luan); | 362 super(luan); |
362 this.searcher = searcher; | 363 this.searcher = searcher; |
364 this.query = query; | |
363 } | 365 } |
364 | 366 |
365 @Override public Object call(Object[] args) throws LuanException { | 367 @Override public Object call(Object[] args) throws LuanException { |
366 try { | 368 try { |
367 return toTable(luan(),searcher.doc(docID)); | 369 LuanTable doc = toTable(luan(),searcher.doc(docID)); |
370 if( args.length > 0 && "explain".equals(args[0]) ) { | |
371 Explanation explanation = searcher.explain(query,docID); | |
372 return new Object[]{doc,explanation}; | |
373 } else { | |
374 return doc; | |
375 } | |
368 } catch(IOException e) { | 376 } catch(IOException e) { |
369 throw new LuanException(e); | 377 throw new LuanException(e); |
370 } | 378 } |
371 } | 379 } |
372 } | 380 } |
419 searcher = openSearcher(); | 427 searcher = openSearcher(); |
420 try { | 428 try { |
421 if( fn!=null && n==null ) { | 429 if( fn!=null && n==null ) { |
422 if( sortStr != null ) | 430 if( sortStr != null ) |
423 throw new LuanException("sort must be nil when n is nil"); | 431 throw new LuanException("sort must be nil when n is nil"); |
424 final DocFn docFn = new DocFn(luan,searcher); | 432 final DocFn docFn = new DocFn(luan,searcher,query); |
425 MyCollector col = new MyCollector() { | 433 MyCollector col = new MyCollector() { |
426 @Override public void collect(int doc) { | 434 @Override public void collect(int doc) { |
427 try { | 435 try { |
428 docFn.docID = docBase + doc; | 436 docFn.docID = docBase + doc; |
429 fn.call(++i,docFn); | 437 fn.call(++i,docFn); |
445 return thcc.getTotalHits(); | 453 return thcc.getTotalHits(); |
446 } | 454 } |
447 Sort sort = sortStr==null ? null : SaneQueryParser.parseSort(mfp,sortStr); | 455 Sort sort = sortStr==null ? null : SaneQueryParser.parseSort(mfp,sortStr); |
448 TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort); | 456 TopDocs td = sort==null ? searcher.search(query,n) : searcher.search(query,n,sort); |
449 final ScoreDoc[] scoreDocs = td.scoreDocs; | 457 final ScoreDoc[] scoreDocs = td.scoreDocs; |
450 DocFn docFn = new DocFn(luan,searcher); | 458 DocFn docFn = new DocFn(luan,searcher,query); |
451 for( int i=0; i<scoreDocs.length; i++ ) { | 459 for( int i=0; i<scoreDocs.length; i++ ) { |
452 docFn.docID = scoreDocs[i].doc; | 460 ScoreDoc scoreDoc = scoreDocs[i]; |
453 fn.call(i+1,docFn); | 461 docFn.docID = scoreDoc.doc; |
462 fn.call(i+1,docFn,scoreDoc.score); | |
454 } | 463 } |
455 return td.totalHits; | 464 return td.totalHits; |
456 } finally { | 465 } finally { |
457 if( !inTransaction ) | 466 if( !inTransaction ) |
458 close(searcher); | 467 close(searcher); |
612 public String highlightTerm(String originalText,TokenGroup tokenGroup) { | 621 public String highlightTerm(String originalText,TokenGroup tokenGroup) { |
613 return originalText; | 622 return originalText; |
614 } | 623 } |
615 }; | 624 }; |
616 | 625 |
617 public LuanFunction highlighter(String queryStr,LuanFunction formatter,final Integer fragmentSize,String dotdotdot) throws ParseException { | 626 public LuanFunction highlighter(String queryStr,final LuanFunction formatter,final Integer fragmentSize,String dotdotdot) |
627 throws ParseException | |
628 { | |
618 Query query = SaneQueryParser.parseQuery(mfp,queryStr); | 629 Query query = SaneQueryParser.parseQuery(mfp,queryStr); |
619 Formatter fmt = new Formatter() { | 630 Formatter fmt = new Formatter() { |
620 public String highlightTerm(String originalText,TokenGroup tokenGroup) { | 631 public String highlightTerm(String originalText,TokenGroup tokenGroup) { |
621 if( tokenGroup.getTotalScore() <= 0 ) | 632 if( tokenGroup.getTotalScore() <= 0 ) |
622 return originalText; | 633 return originalText; |