Ticket #857: selectpostlist-skip_to.cc

File selectpostlist-skip_to.cc, 1.5 KB (added by Adrian Ratiu, 9 days ago)
Line 
1// (a AND_MAYBE "x y") OR h with get_mset(0, 1) returns doc 4 as if it matched
2// the phrase "x y", which it doesn't. The correct top document is doc 1.
3//
4// Build: g++ -std=c++17 selectpostlist-skip_to.cc -o selectpostlist-skip_to $(xapian-config --cxxflags --libs)
5#include <xapian.h>
6#include <iostream>
7#include <string>
8#include <vector>
9
10static void add(Xapian::WritableDatabase& db, std::vector<std::string> words) {
11 Xapian::Document doc;
12 for (size_t i = 0; i < words.size(); ++i) doc.add_posting(words[i], i + 1);
13 db.add_document(doc);
14}
15
16int main() {
17 Xapian::WritableDatabase db(std::string(), Xapian::DB_BACKEND_INMEMORY);
18 add(db, {"h"}); // 1
19 add(db, {"a"}); // 2
20 add(db, {"z"}); // 3
21 add(db, {"a", "x", "z", "y"}); // 4: has x and y, but not the phrase "x y"
22 db.commit();
23
24 std::vector<Xapian::Query> xy{Xapian::Query("x"), Xapian::Query("y")};
25 Xapian::Query phrase(Xapian::Query::OP_PHRASE, xy.begin(), xy.end());
26 Xapian::Query q(Xapian::Query::OP_AND_MAYBE, Xapian::Query("a"), phrase);
27 q |= Xapian::Query("h");
28
29 Xapian::Enquire enq(db);
30 enq.set_query(q);
31 Xapian::MSet all = enq.get_mset(0, 10);
32 Xapian::MSet top = enq.get_mset(0, 1);
33 std::cout << "get_mset(0, 10) top: " << *all.begin()
34 << " weight " << all.begin().get_weight() << "\n";
35 std::cout << "get_mset(0, 1) top: " << *top.begin()
36 << " weight " << top.begin().get_weight() << "\n";
37 return *top.begin() == *all.begin() ? 0 : 1;
38}