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

Revision 8160, 6.3 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>quickstartexpand.cc.html</TITLE>
4</HEAD>
5<BODY BGcolor=#ffffff TEXT=#000000>
6<PRE>
7<FONT color=#0000ff>/* quickstartexpand.cc: Simplest possible query expansion
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; -- &lt;relevant docids&gt;&quot;</FONT> &lt;&lt;
41                endl;
42        exit(<FONT color=#ff00ff>1</FONT>);
43    }
44
45    <FONT color=#0000ff>// Catch any Xapian::Error exceptions thrown</FONT>
46    <B><FONT color=#a52a2a>try</FONT></B> {
47        <FONT color=#0000ff>// Open the database</FONT>
48        Xapian::Database database(argv[<FONT color=#ff00ff>1</FONT>]);
49
50        <FONT color=#0000ff>// Start an enquire session</FONT>
51        Xapian::Enquire enquire(database);
52
53        <FONT color=#0000ff>// Prepare the query terms</FONT>
54        vector&lt;string&gt; queryterms;
55        <B><FONT color=#2e8b57>int</FONT></B> optpos;
56        <B><FONT color=#a52a2a>for</FONT></B> (optpos = <FONT color=#ff00ff>2</FONT>; optpos &lt; argc; optpos++) {
57            <B><FONT color=#a52a2a>if</FONT></B>(string(argv[optpos]) == <FONT color=#ff00ff>&quot;--&quot;</FONT>) {
58                optpos++;
59                <B><FONT color=#a52a2a>break</FONT></B>;
60            }
61            queryterms.push_back(argv[optpos]);
62        }
63
64        <FONT color=#0000ff>// Prepare the relevant document list</FONT>
65        Xapian::RSet reldocs;
66        <B><FONT color=#a52a2a>for</FONT></B> (; optpos &lt; argc; optpos++) {
67            Xapian::docid rdid = atoi(argv[optpos]);
68            <B><FONT color=#a52a2a>if</FONT></B> (rdid != <FONT color=#ff00ff>0</FONT>) {
69                reldocs.add_document(rdid);
70            }
71        }
72
73        <FONT color=#0000ff>// Build the query object</FONT>
74        Xapian::Query query(Xapian::Query::OP_OR, queryterms.begin(), queryterms.end());
75
76        Xapian::MSet matches;
77        <B><FONT color=#a52a2a>if</FONT></B> (query.is_defined()) {
78            cout &lt;&lt; <FONT color=#ff00ff>&quot;Performing query `&quot;</FONT> &lt;&lt; query.get_description() &lt;&lt;
79                    <FONT color=#ff00ff>&quot;'&quot;</FONT> &lt;&lt; endl;
80
81            <FONT color=#0000ff>// Give the query object to the enquire session</FONT>
82            enquire.set_query(query);
83
84            <FONT color=#0000ff>// Get the top 10 results of the query</FONT>
85            matches = enquire.get_mset(<FONT color=#ff00ff>0</FONT>, <FONT color=#ff00ff>10</FONT>);
86
87            <FONT color=#0000ff>// Display the results</FONT>
88            cout &lt;&lt; matches.items.size() &lt;&lt; <FONT color=#ff00ff>&quot; results found&quot;</FONT> &lt;&lt; endl;
89
90            <B><FONT color=#a52a2a>for</FONT></B> (Xapian::MSetIterator i = matches.begin();
91                 i != matches.end();
92                 ++i) {
93                Xapian::Document doc = i.get_document();
94                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;
95                        i.get_percent() &lt;&lt; <FONT color=#ff00ff>&quot;% [&quot;</FONT> &lt;&lt;
96                        doc.get_data() &lt;&lt; <FONT color=#ff00ff>&quot;]&quot;</FONT> &lt;&lt; endl;
97            }
98        }
99
100        <FONT color=#0000ff>// Put the top 5 into the rset if rset is empty</FONT>
101        <B><FONT color=#a52a2a>if</FONT></B> (reldocs.empty()) {
102            Xapian::MSetIterator i;
103            <B><FONT color=#2e8b57>int</FONT></B> j;
104            <B><FONT color=#a52a2a>for</FONT></B> (i = matches.begin(),
105                 j = <FONT color=#ff00ff>0</FONT>;
106                 (i != matches.end()) &amp;&amp; (j &lt; <FONT color=#ff00ff>5</FONT>);
107                 ++i, ++j) {
108                reldocs.add_document(*i);
109            }
110        }
111       
112        <FONT color=#0000ff>// Get the suggested expand terms</FONT>
113        Xapian::ESet eterms = enquire.get_eset(<FONT color=#ff00ff>10</FONT>, reldocs);
114
115        <FONT color=#0000ff>// Display the expand terms</FONT>
116        cout &lt;&lt; eterms.size() &lt;&lt; <FONT color=#ff00ff>&quot; suggested additional terms&quot;</FONT> &lt;&lt; endl;
117
118        <B><FONT color=#a52a2a>for</FONT></B> (Xapian::ESetIterator k = eterms.begin();
119             k != eterms.end();
120             ++k) {
121            cout &lt;&lt; <FONT color=#ff00ff>&quot;Term `&quot;</FONT> &lt;&lt; *k &lt;&lt; <FONT color=#ff00ff>&quot;'</FONT><FONT color=#6a5acd>\t</FONT><FONT color=#ff00ff> &quot;</FONT> &lt;&lt;
122                    <FONT color=#ff00ff>&quot;(weight &quot;</FONT> &lt;&lt; k.get_weight() &lt;&lt; <FONT color=#ff00ff>&quot;)&quot;</FONT> &lt;&lt; endl;
123        }
124    } <B><FONT color=#a52a2a>catch</FONT></B>(const Xapian::Error &amp;error) {
125        cout &lt;&lt; <FONT color=#ff00ff>&quot;Exception: &quot;</FONT>  &lt;&lt; error.get_msg() &lt;&lt; endl;
126    }
127}
128</PRE>
129</BODY>
130</HTML>
Note: See TracBrowser for help on using the browser.