root / tags / 1.0.8 / xapian-core / docs / quickstartsearch.cc.html

Revision 8160, 3.7 kB (checked in by olly, 21 months ago)

* ./: svn:eol-style not svn:eolstyle.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1<HTML>
2<HEAD>
3<TITLE>quickstartsearch.cc.html</TITLE>
4</HEAD>
5<BODY BGcolor=#ffffff TEXT=#000000>
6<PRE>
7<FONT color=#0000ff>/* quickstartsearch.cc: Simplest possible searcher
8 *
9 * ----START-LICENCE----
10 * Copyright 1999,2000,2001 BrightStation PLC
11 * Copyright 2003,2004 Olly Betts
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License as
15 * published by the Free Software Foundation; either version 2 of the
16 * License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 * USA
27 * -----END-LICENCE-----
28 */</FONT>
29
30<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;xapian.h&gt;</FONT>
31<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;iostream&gt;</FONT>
32<B><FONT color=#a52a2a>using namespace </FONT></B>std;
33
34<B><FONT color=#2e8b57>int</FONT></B> main(<B><FONT color=#2e8b57>int</FONT></B> argc, <B><FONT color=#2e8b57>char</FONT></B> **argv)
35{
36    <FONT color=#0000ff>// Simplest possible options parsing: we just require two or more
37    // parameters.</FONT>
38    <B><FONT color=#a52a2a>if</FONT></B> (argc &lt; <FONT color=#ff00ff>3</FONT>) {
39        cout &lt;&lt; <FONT color=#ff00ff>&quot;usage: &quot;</FONT> &lt;&lt; argv[<FONT color=#ff00ff>0</FONT>] &lt;&lt;
40                <FONT color=#ff00ff>&quot; &lt;path to database&gt; &lt;search terms&gt;&quot;</FONT> &lt;&lt; endl;
41        exit(<FONT color=#ff00ff>1</FONT>);
42    }
43
44    <FONT color=#0000ff>// Catch any Xapian::Error exceptions thrown</FONT>
45    <B><FONT color=#a52a2a>try</FONT></B> {
46        <FONT color=#0000ff>// Make the database</FONT>
47        Xapian::Database db(argv[<FONT color=#ff00ff>1</FONT>]);
48
49        <FONT color=#0000ff>// Start an enquire session</FONT>
50        Xapian::Enquire enquire(db);
51
52        <FONT color=#0000ff>// Build the query object</FONT>
53        Xapian::Query query(Xapian::Query::OP_OR, argv + 2, argv + argc);
54        cout &lt;&lt; <FONT color=#ff00ff>&quot;Performing query `&quot;</FONT> &lt;&lt; query.get_description() &lt;&lt; <FONT color=#ff00ff>&quot;'&quot;</FONT> &lt;&lt; endl;
55
56        <FONT color=#0000ff>// Give the query object to the enquire session</FONT>
57        enquire.set_query(query);
58
59        <FONT color=#0000ff>// Get the top 10 results of the query</FONT>
60        Xapian::MSet matches = enquire.get_mset(<FONT color=#ff00ff>0</FONT>, <FONT color=#ff00ff>10</FONT>);
61
62        <FONT color=#0000ff>// Display the results</FONT>
63        cout &lt;&lt; matches.size() &lt;&lt; <FONT color=#ff00ff>&quot; results found&quot;</FONT> &lt;&lt; endl;
64
65        <B><FONT color=#a52a2a>for</FONT></B> (Xapian::MSetIterator i = matches.begin();
66             i != matches.end();
67             ++i) {
68            Xapian::Document doc = i.get_document();
69            cout &lt;&lt; <FONT color=#ff00ff>&quot;Document ID &quot;</FONT> &lt;&lt; *i &lt;&lt; <FONT color=#ff00ff>&quot;</FONT><FONT color=#6a5acd>\t</FONT><FONT color=#ff00ff>&quot;</FONT> &lt;&lt;
70                    i.get_percent() &lt;&lt; <FONT color=#ff00ff>&quot;% [&quot;</FONT> &lt;&lt;
71                    doc.get_data() &lt;&lt; <FONT color=#ff00ff>&quot;]&quot;</FONT> &lt;&lt; endl;
72        }
73    } <B><FONT color=#a52a2a>catch</FONT></B>(const Xapian::Error &amp;error) {
74        cout &lt;&lt; <FONT color=#ff00ff>&quot;Exception: &quot;</FONT>  &lt;&lt; error.get_msg() &lt;&lt; endl;
75    }
76}
77</PRE>
78</BODY>
79</HTML>
Note: See TracBrowser for help on using the browser.