File: quickstartsearch.cc.html

package info (click to toggle)
xapian-core 1.2.19-1%2Bdeb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 19,420 kB
  • ctags: 12,656
  • sloc: cpp: 101,561; sh: 11,340; ansic: 8,193; perl: 757; makefile: 635; tcl: 308; python: 40
file content (78 lines) | stat: -rw-r--r-- 3,814 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
<HTML>
<HEAD>
<TITLE>quickstartsearch.cc.html</TITLE>
</HEAD>
<BODY BGcolor=#ffffff TEXT=#000000>
<PRE>
<FONT color=#0000ff>/* quickstartsearch.cc: Simplest possible searcher
 *
 * ----START-LICENCE----
 * Copyright 1999,2000,2001 BrightStation PLC
 * Copyright 2003,2004 Olly Betts
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301
 * USA
 */</FONT>

<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;xapian.h&gt;</FONT>
<FONT color=#a020f0>#include </FONT><FONT color=#ff00ff>&lt;iostream&gt;</FONT>
<B><FONT color=#a52a2a>using namespace </FONT></B>std;

<B><FONT color=#2e8b57>int</FONT></B> main(<B><FONT color=#2e8b57>int</FONT></B> argc, <B><FONT color=#2e8b57>char</FONT></B> **argv)
{
    <FONT color=#0000ff>// Simplest possible options parsing: we just require two or more
    // parameters.</FONT>
    <B><FONT color=#a52a2a>if</FONT></B> (argc &lt; <FONT color=#ff00ff>3</FONT>) {
        cout &lt;&lt; <FONT color=#ff00ff>&quot;usage: &quot;</FONT> &lt;&lt; argv[<FONT color=#ff00ff>0</FONT>] &lt;&lt;
                <FONT color=#ff00ff>&quot; &lt;path to database&gt; &lt;search terms&gt;&quot;</FONT> &lt;&lt; endl;
        exit(<FONT color=#ff00ff>1</FONT>);
    }

    <FONT color=#0000ff>// Catch any Xapian::Error exceptions thrown</FONT>
    <B><FONT color=#a52a2a>try</FONT></B> {
        <FONT color=#0000ff>// Make the database</FONT>
	Xapian::Database db(argv[<FONT color=#ff00ff>1</FONT>]);

        <FONT color=#0000ff>// Start an enquire session</FONT>
        Xapian::Enquire enquire(db);

        <FONT color=#0000ff>// Build the query object</FONT>
        Xapian::Query query(Xapian::Query::OP_OR, argv + 2, argv + argc);
        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;

        <FONT color=#0000ff>// Give the query object to the enquire session</FONT>
        enquire.set_query(query);

        <FONT color=#0000ff>// Get the top 10 results of the query</FONT>
        Xapian::MSet matches = enquire.get_mset(<FONT color=#ff00ff>0</FONT>, <FONT color=#ff00ff>10</FONT>);

        <FONT color=#0000ff>// Display the results</FONT>
        cout &lt;&lt; matches.size() &lt;&lt; <FONT color=#ff00ff>&quot; results found&quot;</FONT> &lt;&lt; endl;

        <B><FONT color=#a52a2a>for</FONT></B> (Xapian::MSetIterator i = matches.begin();
             i != matches.end();
             ++i) {
            Xapian::Document doc = i.get_document();
            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;
                    i.get_percent() &lt;&lt; <FONT color=#ff00ff>&quot;% [&quot;</FONT> &lt;&lt;
                    doc.get_data() &lt;&lt; <FONT color=#ff00ff>&quot;]&quot;</FONT> &lt;&lt; endl;
        }
    } <B><FONT color=#a52a2a>catch</FONT></B>(const Xapian::Error &amp;error) {
        cout &lt;&lt; <FONT color=#ff00ff>&quot;Exception: &quot;</FONT>  &lt;&lt; error.get_msg() &lt;&lt; endl;
    }
}
</PRE>
</BODY>
</HTML>