comparison lucene/src/luan/modules/lucene/Lucene.luan @ 545:ddcd4296107a

clean up lucene search
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 14 Jun 2015 01:34:42 -0600
parents c5a93767cc5c
children eaef1005ab87
comparison
equal deleted inserted replaced
544:c5a93767cc5c 545:ddcd4296107a
14 double = NumberFieldParser.DOUBLE; 14 double = NumberFieldParser.DOUBLE;
15 } 15 }
16 16
17 function M.index(indexDir) 17 function M.index(indexDir)
18 local index = {} 18 local index = {}
19 local java_index = LuceneIndex.new(indexDir) 19 local java_index = LuceneIndex.new(indexDir,index)
20 index.indexed_fields = java_index.indexedFieldsMeta.newTable() 20 index.indexed_fields = java_index.indexedFieldsMeta.newTable()
21 index.to_string = java_index.to_string 21 index.to_string = java_index.to_string
22 index.backup = java_index.backup 22 index.backup = java_index.backup
23 index.Writer = java_index.Writer 23 index.writer = java_index.writer
24 index.Searcher = java_index.Searcher 24 index.advanced_search = java_index.advanced_search
25 index.search_in_transaction = java_index.search_in_transaction
25 index.delete_all = java_index.delete_all 26 index.delete_all = java_index.delete_all
26 index.close = java_index.close 27 index.close = java_index.close
27 28
28 function index.save_document(doc) 29 function index.save_document(doc)
29 index.Writer( function(writer) 30 index.writer( function(writer)
30 writer.save_document(doc) 31 writer.save_document(doc)
31 end ) 32 end )
32 end 33 end
33 34
34 function index.delete_documents(terms) 35 function index.delete_documents(terms)
35 index.Writer( function(writer) 36 index.writer( function(writer)
36 writer.delete_documents(terms) 37 writer.delete_documents(terms)
37 end ) 38 end )
38 end 39 end
39 40
40 function index.get_first(query, sort) 41 function index.search(query, from, to, sort)
41 return index.Searcher( function(searcher) 42 local results = {}
42 local results, _, total_hits = searcher.search(query,1,sort) 43 local function fn(i,doc_fn)
43 return results(), total_hits 44 if i >= from then
44 end ) 45 results[#results+1] = doc_fn()
46 end
47 end
48 local total_hits = index.advanced_search(query,fn,to,sort)
49 return results, total_hits
45 end 50 end
46 51
47 function index.get_document(query) 52 function index.get_document(query)
48 local doc, total_hits = index.get_first(query); 53 local doc
49 if total_hits > 1 then 54 local function fn(_,doc_fn)
50 error( "found " .. total_hits .. " documents" ) 55 doc = doc_fn()
51 end 56 end
57 local total_hits = index.advanced_search(query,fn,1)
58 total_hits <= 1 or error( "found " .. total_hits .. " documents" )
52 return doc 59 return doc
53 end 60 end
54 61
55 function index.count(query) 62 function index.count(query)
56 return index.Searcher( function(searcher) 63 return index.advanced_search(query)
57 local _, _, total_hits = searcher.search(query,0)
58 return total_hits
59 end )
60 end 64 end
61 65
62 return index 66 return index
63 end 67 end
64 68