Merge pull request #22 from kiwix/search_in_libzim

Search in libzim
This commit is contained in:
Matthieu Gautier 2017-04-11 14:02:21 +02:00 committed by GitHub
commit f46e560d7f
3 changed files with 22 additions and 24 deletions

View File

@ -19,7 +19,12 @@ if meson.is_cross_build() and host_machine.system() == 'windows'
# xapian doesn't use uuid on windows. # xapian doesn't use uuid on windows.
uuid_dep = declare_dependency() uuid_dep = declare_dependency()
else else
uuid_dep = declare_dependency(link_args:['-luuid', '-lrt']) xapian_dep = dependency('xapian-core', required : false)
if xapian_dep.found()
uuid_dep = declare_dependency(link_args:['-luuid', '-lrt'])
else
uuid_dep = declare_dependency()
endif
endif endif
all_deps = [thread_dep, kiwixlib_dep, microhttpd_dep, z_dep, uuid_dep] all_deps = [thread_dep, kiwixlib_dep, microhttpd_dep, z_dep, uuid_dep]

View File

@ -19,12 +19,11 @@
#include <getopt.h> #include <getopt.h>
#include <unistd.h> #include <unistd.h>
#include <kiwix/xapianSearcher.h> #include <kiwix/reader.h>
#include <kiwix/searcher.h>
enum supportedBackend { XAPIAN };
void usage() { void usage() {
cout << "Usage: kiwix-search [--verbose|-v] [--backend|-b=xapian] INDEX_PATH SEARCH" << endl; cout << "Usage: kiwix-search [--verbose|-v] ZIM_PATH SEARCH" << endl;
exit(1); exit(1);
} }
@ -33,21 +32,20 @@ int main(int argc, char **argv) {
/* Init the variables */ /* Init the variables */
//const char *indexPath = "/home/itamar/.www.kiwix.org/kiwix/43k0i1j4.default/6d2e587b-d586-dc6a-dc6a-e4ef035a1495d15c.index"; //const char *indexPath = "/home/itamar/.www.kiwix.org/kiwix/43k0i1j4.default/6d2e587b-d586-dc6a-dc6a-e4ef035a1495d15c.index";
//const char *indexPath = "/home/itamar/testindex"; //const char *indexPath = "/home/itamar/testindex";
const char *indexPath = NULL; const char *zimPath = NULL;
const char *search = NULL; const char *search = NULL;
bool verboseFlag = false; bool verboseFlag = false;
int option_index = 0; int option_index = 0;
int c = 0; int c = 0;
supportedBackend backend = XAPIAN;
kiwix::Searcher *searcher = NULL; kiwix::Searcher *searcher = NULL;
kiwix::Reader *reader = NULL;
/* Argument parsing */ /* Argument parsing */
while (42) { while (42) {
static struct option long_options[] = { static struct option long_options[] = {
{"verbose", no_argument, 0, 'v'}, {"verbose", no_argument, 0, 'v'},
{"backend", required_argument, 0, 'b'},
{0, 0, 0, 0} {0, 0, 0, 0}
}; };
@ -58,22 +56,15 @@ int main(int argc, char **argv) {
case 'v': case 'v':
verboseFlag = true; verboseFlag = true;
break; break;
case 'b':
if (!strcmp(optarg, "xapian")) {
backend = XAPIAN;
} else {
usage();
}
break;
} }
} else { } else {
if (optind < argc) { if (optind < argc) {
if (indexPath == NULL) { if (zimPath == NULL) {
indexPath = argv[optind++]; zimPath = argv[optind++];
} else if (search == NULL) { } else if (search == NULL) {
search = argv[optind++]; search = argv[optind++];
} else { } else {
cout << indexPath << endl; cout << zimPath << endl;
usage(); usage();
} }
} else { } else {
@ -83,16 +74,17 @@ int main(int argc, char **argv) {
} }
/* Check if we have enough arguments */ /* Check if we have enough arguments */
if (indexPath == NULL || search == NULL) { if (zimPath == NULL || search == NULL) {
usage(); usage();
} }
/* Try to prepare the indexing */ /* Try to prepare the indexing */
try { try {
/* We will not get the snippets, So we do not need to pass the reader */ /* We will not get the snippets, So we do not need to pass the reader */
searcher = new kiwix::XapianSearcher(indexPath, NULL); reader = new kiwix::Reader(zimPath);
searcher = new kiwix::Searcher(reader);
} catch (...) { } catch (...) {
cerr << "Unable to search through index '" << indexPath << "'." << endl; cerr << "Unable to search through zim '" << zimPath << "'." << endl;
exit(1); exit(1);
} }
@ -100,13 +92,14 @@ int main(int argc, char **argv) {
if (searcher != NULL) { if (searcher != NULL) {
string searchString(search); string searchString(search);
searcher->search(searchString, 0, 10); searcher->search(searchString, 0, 10);
Result* p_result; kiwix::Result* p_result;
while ( (p_result = searcher->getNextResult()) ) { while ( (p_result = searcher->getNextResult()) ) {
cout << p_result->get_title() << endl; cout << p_result->get_title() << endl;
delete p_result; delete p_result;
} }
delete searcher; delete searcher;
delete reader;
// kiwix::XapianSearcher::terminate(); // kiwix::XapianSearcher::terminate();
} else { } else {

View File

@ -62,7 +62,7 @@ extern "C" {
#include <zlib.h> #include <zlib.h>
#include <kiwix/reader.h> #include <kiwix/reader.h>
#include <kiwix/manager.h> #include <kiwix/manager.h>
#include <kiwix/xapianSearcher.h> #include <kiwix/searcher.h>
#include <kiwix/common/pathTools.h> #include <kiwix/common/pathTools.h>
#include <kiwix/common/regexTools.h> #include <kiwix/common/regexTools.h>
#include <kiwix/common/stringTools.h> #include <kiwix/common/stringTools.h>
@ -860,7 +860,7 @@ int main(int argc, char **argv) {
if (!indexPath.empty()) { if (!indexPath.empty()) {
try { try {
kiwix::Searcher *searcher = new kiwix::XapianSearcher(indexPath, reader); kiwix::Searcher *searcher = new kiwix::Searcher(reader);
searcher->setProtocolPrefix("/"); searcher->setProtocolPrefix("/");
searcher->setSearchProtocolPrefix("/search?"); searcher->setSearchProtocolPrefix("/search?");
searcher->setContentHumanReadableId(humanReadableId); searcher->setContentHumanReadableId(humanReadableId);