-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsearch.cpp
More file actions
70 lines (64 loc) · 2.56 KB
/
search.cpp
File metadata and controls
70 lines (64 loc) · 2.56 KB
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <xapian.h>
#include <vector>
using namespace std;
int main() {
Xapian::WritableDatabase db("test.idx", Xapian::DB_CREATE_OR_OPEN);
vector<string> terms; // example vector to show it working
terms.push_back("word");
terms.push_back("the");
terms.push_back("hint");
terms.push_back("apple");
terms.push_back("of");
Xapian::TermGenerator termgenerator;
// Use a "stemmer", which reduces words like "root", "rooting", "rooted", etc. to their common stem "root"
// before adding to the index. Queries will need to be stemmed, too (see below).
termgenerator.set_stemmer(Xapian::Stem("en"));
int i = 0;
for(std::vector<string>::iterator it = terms.begin(); it != terms.end(); it++) {
Xapian::Document doc; //Xapian stores each term in a "document" then compares search to the documents
string body = terms[i];
doc.set_data(body); //adding the term to the document
termgenerator.set_document(doc); //this is for indexing so you dont get multiple of the same documents
termgenerator.index_text(body);
string idterm = "Term" + terms[i];
doc.add_boolean_term(idterm);
db.replace_document(idterm, doc);
i++;
}
// Now let's search
int offset = 0;
int pagesize = 10;
Xapian::QueryParser queryparser;
string querystring;
while(true)
{
cout << "Enter a search query (or \"quit\"): ";
getline(cin, querystring);
if(querystring == "quit")
{
break;
}
queryparser.set_stemmer(Xapian::Stem("en"));
queryparser.set_stemming_strategy(queryparser.STEM_SOME);
Xapian::Query query = queryparser.parse_query(querystring);
Xapian::Enquire enquire(db);
enquire.set_query(query);
Xapian::MSet mset = enquire.get_mset(offset, pagesize);
cout << "Showing results " << offset + 1 << "-" << min(offset + pagesize, (int)mset.size())
<< " of " << mset.size() << endl;
for(Xapian::MSetIterator m = mset.begin(); m != mset.end(); ++m)
{
Xapian::docid did = *m;
cout << m.get_rank() + 1 << ": DocID " << did << ", match score: " << m.get_percent() << endl;
string data = m.get_document().get_data();
cout << data << endl << endl << endl;
}
cout << endl << endl;
}
return 0;
}