2
** Copyright (C) 2010 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
4
** This program is free software; you can redistribute it and/or modify
5
** it under the terms of the GNU General Public License as published by
6
** the Free Software Foundation; either version 3 of the License, or
7
** (at your option) any later version.
9
** This program is distributed in the hope that it will be useful,
10
** but WITHOUT ANY WARRANTY; without even the implied warranty of
11
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
** GNU General Public License for more details.
14
** You should have received a copy of the GNU General Public License
15
** along with this program; if not, write to the Free Software Foundation,
16
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
#include <glib/gstdio.h>
26
#include "mu-query-xapian.h"
28
#include "mu-msg-xapian.h"
29
#include "mu-msg-xapian-priv.hh"
34
static void add_prefix (const MuMsgField* field, Xapian::QueryParser* qparser);
36
struct _MuQueryXapian {
37
Xapian::Database* _db;
38
Xapian::QueryParser* _qparser;
39
Xapian::Sorter* _sorters[MU_MSG_FIELD_TYPE_NUM];
43
init_mu_query_xapian (MuQueryXapian *mqx, const char* dbpath)
49
mqx->_db = new Xapian::Database(dbpath);
50
mqx->_qparser = new Xapian::QueryParser;
52
mqx->_qparser->set_database(*mqx->_db);
53
mqx->_qparser->set_default_op(Xapian::Query::OP_OR);
54
mqx->_qparser->set_stemming_strategy
55
(Xapian::QueryParser::STEM_SOME);
57
memset (mqx->_sorters, 0, sizeof(mqx->_sorters));
58
mu_msg_field_foreach ((MuMsgFieldForEachFunc)add_prefix,
59
(gpointer)mqx->_qparser);
62
} MU_XAPIAN_CATCH_BLOCK;
68
} MU_XAPIAN_CATCH_BLOCK;
75
uninit_mu_query_xapian (MuQueryXapian *mqx)
81
for (int i = 0; i != MU_MSG_FIELD_TYPE_NUM; ++i)
82
delete mqx->_sorters[i];
84
} MU_XAPIAN_CATCH_BLOCK;
88
get_query (MuQueryXapian * mqx, const char* searchexpr, int *err = 0) {
91
return mqx->_qparser->parse_query
93
Xapian::QueryParser::FLAG_BOOLEAN |
94
Xapian::QueryParser::FLAG_PHRASE |
95
// Xapian::QueryParser::FLAG_LOVEHATE |
96
// Xapian::QueryParser::FLAG_BOOLEAN_ANY_CASE |
97
Xapian::QueryParser::FLAG_WILDCARD |
98
Xapian::QueryParser::FLAG_PURE_NOT |
99
Xapian::QueryParser::FLAG_PARTIAL);
101
} MU_XAPIAN_CATCH_BLOCK;
106
return Xapian::Query();
110
add_prefix (const MuMsgField* field, Xapian::QueryParser* qparser)
112
if (!mu_msg_field_is_xapian_enabled(field))
115
const std::string prefix (mu_msg_field_xapian_prefix(field));
117
qparser->add_boolean_prefix(std::string(mu_msg_field_name(field)),
119
qparser->add_boolean_prefix(std::string(mu_msg_field_shortcut(field)),
122
/* make the empty string match this field too*/
123
qparser->add_prefix ("", prefix);
127
mu_query_xapian_new (const char* xpath)
131
g_return_val_if_fail (xpath, NULL);
133
if (!mu_util_check_dir (xpath, TRUE, FALSE)) {
134
g_warning ("'%s' is not a readable xapian dir",
139
mqx = g_new (MuQueryXapian, 1);
141
if (!init_mu_query_xapian (mqx, xpath)) {
142
g_warning ("failed to initalize xapian query");
152
mu_query_xapian_destroy (MuQueryXapian *self)
157
uninit_mu_query_xapian (self);
163
mu_query_xapian_run (MuQueryXapian *self, const char* searchexpr,
164
const MuMsgField* sortfield, gboolean ascending)
166
g_return_val_if_fail (self, NULL);
167
g_return_val_if_fail (searchexpr, NULL);
170
Xapian::Query q(get_query(self, searchexpr));
171
Xapian::Enquire enq (*self->_db);
174
enq.set_sort_by_value (
175
(Xapian::valueno)mu_msg_field_id(sortfield),
179
enq.set_cutoff (0,0);
181
return mu_msg_xapian_new (enq, 10000);
183
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
187
mu_query_xapian_as_string (MuQueryXapian *self, const char* searchexpr)
189
g_return_val_if_fail (self, NULL);
190
g_return_val_if_fail (searchexpr, NULL);
193
Xapian::Query q(get_query(self, searchexpr));
194
return g_strdup(q.get_description().c_str());
196
} MU_XAPIAN_CATCH_BLOCK_RETURN(NULL);
201
needs_quotes (const char* str)
204
const char *keywords[] = {
205
"ANO", "OR", "NOT", "NEAR", "ADJ"
208
for (i = 0; i != G_N_ELEMENTS(keywords); ++i)
209
if (g_strcasecmp (str, keywords[i]) == 0)
217
mu_query_xapian_combine (const gchar **params, gboolean connect_or)
222
g_return_val_if_fail (params && params[0], NULL);
224
str = g_string_sized_new (64); /* just a guess */
226
for (i = 0; params && params[i]; ++i) {
229
const char* cnx = "";
232
elm = (const gchar*)params[i];
233
if (!elm) /* shouldn't happen */
237
cnx = connect_or ? " OR " : " AND ";
239
do_quote = needs_quotes (elm);
240
g_string_append_printf (str, "%s%s%s%s",
241
do_quote ? "\"" : "",
243
do_quote ? "\"" : "",
247
return g_string_free (str, FALSE);