3 -- Simple example script demonstrating query expansion.
5 -- Copyright (C) 2011 Xiaona Han
7 -- This program is free software; you can redistribute it and/or
8 -- modify it under the terms of the GNU General Public License as
9 -- published by the Free Software Foundation; either version 2 of the
10 -- License, or (at your option) any later version.
12 -- This program is distributed in the hope that it will be useful,
13 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
14 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 -- GNU General Public License for more details.
17 -- You should have received a copy of the GNU General Public License
18 -- along with this program; if not, write to the Free Software
19 -- Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
24 -- Require at least two command line arguments.
26 io
.stderr
:write("Usage:" .. arg
[0] .. " PATH_TO_DATABASE QUERY [-- [DOCID...]]\n")
30 -- Open the database for searching.
31 database
= xapian
.Database(arg
[1])
33 -- Start an enquire session.
34 enquire
= xapian
.Enquire(database
)
36 -- Combine command line arguments up to "--" with spaces between
37 -- them, so that simple queries don't have to be quoted at the shell level.
40 while index
<= #arg
do
41 local para
= arg
[index
]
46 query_string
= query_string
.. ' ' .. para
49 -- Create an RSet with the listed docids in.
50 reldocs
= xapian
.RSet()
52 --for index in range(index, #arg) do
53 for i
= index
, #arg
do
54 reldocs
:add_document(tonumber(arg
[index
]))
58 -- Parse the query string to produce a Xapian::Query object.
59 qp
= xapian
.QueryParser()
60 stemmer
= xapian
.Stem("english")
61 qp
:set_stemmer(stemmer
)
62 qp
:set_database(database
)
63 qp
:set_stemming_strategy(xapian
.QueryParser_STEM_SOME
)
64 query
= qp
:parse_query(query_string
)
66 if not query
:empty() then
67 print("Parsed query is: " .. tostring(query
))
69 -- Find the top 10 results for the query.
70 enquire
:set_query(query
)
71 matches
= enquire
:get_mset(0, 10)
73 -- Display the size of the results.
74 print(string.format("%i results found.", matches
:get_matches_estimated()))
75 print(string.format("Results 1-%i:", matches
:size()))
77 -- Display the results
78 for m
in matches
:items() do
79 print(m
:get_rank() + 1, m
:get_percent() .. "%", m
:get_docid(), m
:get_document():get_data())
82 -- Put the top 5 (at most) docs into the rset if rset is empty
83 if reldocs
:empty() then
85 for m
in matches
:items() do
86 reldocs
:add_document(m
:get_docid())
95 -- Get the suggested expand terms
96 eterms
= enquire
:get_eset(10, reldocs
)
97 print (string.format("%i suggested additional terms", eterms
:size()))
98 for m
in eterms
:terms() do
99 print(string.format("%s: %f", m
:get_term(), m
:get_weight()))