| 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><xapian.h></FONT> |
|---|
| 31 | <FONT color=#a020f0>#include </FONT><FONT color=#ff00ff><iostream></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 < <FONT color=#ff00ff>3</FONT>) { |
|---|
| 39 | cout << <FONT color=#ff00ff>"usage: "</FONT> << argv[<FONT color=#ff00ff>0</FONT>] << |
|---|
| 40 | <FONT color=#ff00ff>" <path to database> <search terms> -- <relevant docids>"</FONT> << |
|---|
| 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<string> 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 < argc; optpos++) { |
|---|
| 57 | <B><FONT color=#a52a2a>if</FONT></B>(string(argv[optpos]) == <FONT color=#ff00ff>"--"</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 < 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 << <FONT color=#ff00ff>"Performing query `"</FONT> << query.get_description() << |
|---|
| 79 | <FONT color=#ff00ff>"'"</FONT> << 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 << matches.items.size() << <FONT color=#ff00ff>" results found"</FONT> << 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 << <FONT color=#ff00ff>"Document ID "</FONT> << *i << <FONT color=#ff00ff>"</FONT><FONT color=#6a5acd>\t</FONT><FONT color=#ff00ff>"</FONT> << |
|---|
| 95 | i.get_percent() << <FONT color=#ff00ff>"% ["</FONT> << |
|---|
| 96 | doc.get_data() << <FONT color=#ff00ff>"]"</FONT> << 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()) && (j < <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 << eterms.size() << <FONT color=#ff00ff>" suggested additional terms"</FONT> << endl; |
|---|
| 117 | |
|---|
| 118 | <B><FONT color=#a52a2a>for</FONT></B> (Xapian::ESetIterator k = eterms.begin(); |
|---|
| 119 | k != eterms.end(); |
|---|
| 120 | ++k) { |
|---|
| 121 | cout << <FONT color=#ff00ff>"Term `"</FONT> << *k << <FONT color=#ff00ff>"'</FONT><FONT color=#6a5acd>\t</FONT><FONT color=#ff00ff> "</FONT> << |
|---|
| 122 | <FONT color=#ff00ff>"(weight "</FONT> << k.get_weight() << <FONT color=#ff00ff>")"</FONT> << endl; |
|---|
| 123 | } |
|---|
| 124 | } <B><FONT color=#a52a2a>catch</FONT></B>(const Xapian::Error &error) { |
|---|
| 125 | cout << <FONT color=#ff00ff>"Exception: "</FONT> << error.get_msg() << endl; |
|---|
| 126 | } |
|---|
| 127 | } |
|---|
| 128 | </PRE> |
|---|
| 129 | </BODY> |
|---|
| 130 | </HTML> |
|---|