xapian-core  1.4.26
simplesearch.cc
Go to the documentation of this file.
1 
6 /* Copyright (C) 2007-2022 Olly Betts
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21  */
22 
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
26 
27 #include <xapian.h>
28 
29 #include <iostream>
30 #include <string>
31 
32 #include <cstdlib> // For exit().
33 #include <cstring>
34 
35 using namespace std;
36 
37 int
38 main(int argc, char **argv)
39 try {
40  // We require at least two command line arguments.
41  if (argc < 3) {
42  int rc = 1;
43  if (argv[1]) {
44  if (strcmp(argv[1], "--version") == 0) {
45  cout << "simplesearch\n";
46  exit(0);
47  }
48  if (strcmp(argv[1], "--help") == 0) {
49  rc = 0;
50  }
51  }
52  cout << "Usage: " << argv[0] << " PATH_TO_DATABASE QUERY\n";
53  exit(rc);
54  }
55 
56  // Open the database for searching.
57  Xapian::Database db(argv[1]);
58 
59  // Start an enquire session.
60  Xapian::Enquire enquire(db);
61 
62  // Combine the rest of the command line arguments with spaces between
63  // them, so that simple queries don't have to be quoted at the shell
64  // level.
65  string query_string(argv[2]);
66  argv += 3;
67  while (*argv) {
68  query_string += ' ';
69  query_string += *argv++;
70  }
71 
72  // Parse the query string to produce a Xapian::Query object.
74  Xapian::Stem stemmer("english");
75  qp.set_stemmer(stemmer);
76  qp.set_database(db);
78  Xapian::Query query = qp.parse_query(query_string);
79  cout << "Parsed query is: " << query.get_description() << '\n';
80 
81  // Find the top 10 results for the query.
82  enquire.set_query(query);
83  Xapian::MSet matches = enquire.get_mset(0, 10);
84 
85  // Display the results.
86  cout << matches.get_matches_estimated() << " results found.\n";
87  cout << "Matches 1-" << matches.size() << ":\n\n";
88 
89  for (Xapian::MSetIterator i = matches.begin(); i != matches.end(); ++i) {
90  cout << i.get_rank() + 1 << ": " << i.get_weight() << " docid=" << *i
91  << " [" << i.get_document().get_data() << "]\n\n";
92  }
93 } catch (const Xapian::Error &e) {
94  cout << e.get_description() << '\n';
95  exit(1);
96 }
Xapian::doccount size() const
Return number of items in this MSet object.
Definition: omenquire.cc:318
This class is used to access a database, or a group of databases.
Definition: database.h:68
Class representing a stemming algorithm.
Definition: stem.h:62
Build a Xapian::Query object from a user query string.
Definition: queryparser.h:778
Class representing a list of search results.
Definition: mset.h:44
STL namespace.
MSet get_mset(Xapian::doccount first, Xapian::doccount maxitems, Xapian::doccount checkatleast=0, const RSet *omrset=0, const MatchDecider *mdecider=0) const
Get (a portion of) the match set for the current query.
Definition: omenquire.cc:938
static Xapian::Stem stemmer
Definition: stemtest.cc:41
void set_stemmer(const Xapian::Stem &stemmer)
Set the stemmer.
Definition: queryparser.cc:85
void set_stemming_strategy(stem_strategy strategy)
Set the stemming strategy.
Definition: queryparser.cc:91
Iterator over a Xapian::MSet.
Definition: mset.h:368
Public interfaces for the Xapian library.
MSetIterator begin() const
Return iterator pointing to the first item in this MSet.
Definition: mset.h:624
MSetIterator end() const
Return iterator pointing to just after the last item in this MSet.
Definition: mset.h:629
Query parse_query(const std::string &query_string, unsigned flags=FLAG_DEFAULT, const std::string &default_prefix=std::string())
Parse a query.
Definition: queryparser.cc:162
void set_query(const Xapian::Query &query, Xapian::termcount qlen=0)
Set the query to run.
Definition: omenquire.cc:793
std::string get_description() const
Return a string describing this object.
Definition: error.cc:93
static Xapian::Query query(Xapian::Query::op op, const string &t1=string(), const string &t2=string(), const string &t3=string(), const string &t4=string(), const string &t5=string(), const string &t6=string(), const string &t7=string(), const string &t8=string(), const string &t9=string(), const string &t10=string())
Definition: api_anydb.cc:63
Xapian::doccount get_matches_estimated() const
Estimate of the total number of matching documents.
Definition: omenquire.cc:253
void set_database(const Database &db)
Specify the database being searched.
Definition: queryparser.cc:142
std::string get_description() const
Return a string describing this object.
Definition: query.cc:232
This class provides an interface to the information retrieval system for the purpose of searching...
Definition: enquire.h:152
All exceptions thrown by Xapian are subclasses of Xapian::Error.
Definition: error.h:43
Class representing a query.
Definition: query.h:46
int main(int argc, char **argv)
Definition: simplesearch.cc:38