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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304
|
/** @file
* @brief tests which work with any backend
*/
/* Copyright 1999,2000,2001 BrightStation PLC
* Copyright 2002 Ananova Ltd
* Copyright 2002-2024 Olly Betts
* Copyright 2006,2008 Lemur Consulting Ltd
* Copyright 2011 Action Without Borders
*
* 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
*/
#include <config.h>
#include "api_anydb.h"
#include <algorithm>
#include <string>
#define XAPIAN_DEPRECATED(X) X
#include <xapian.h>
#include "testsuite.h"
#include "testutils.h"
#include "apitest.h"
#include <list>
using namespace std;
static void
print_mset_weights(const Xapian::MSet &mset)
{
Xapian::MSetIterator i = mset.begin();
for ( ; i != mset.end(); ++i) {
tout << " " << i.get_weight();
}
}
static void
print_mset_percentages(const Xapian::MSet &mset)
{
Xapian::MSetIterator i = mset.begin();
for ( ; i != mset.end(); ++i) {
tout << " " << mset.convert_to_percent(i);
}
}
static Xapian::Query
query(Xapian::Query::op op,
const string & t1 = string(), const string & t2 = string(),
const string & t3 = string(), const string & t4 = string(),
const string & t5 = string(), const string & t6 = string(),
const string & t7 = string(), const string & t8 = string(),
const string & t9 = string(), const string & t10 = string())
{
vector<string> v;
Xapian::Stem stemmer("english");
if (!t1.empty()) v.push_back(stemmer(t1));
if (!t2.empty()) v.push_back(stemmer(t2));
if (!t3.empty()) v.push_back(stemmer(t3));
if (!t4.empty()) v.push_back(stemmer(t4));
if (!t5.empty()) v.push_back(stemmer(t5));
if (!t6.empty()) v.push_back(stemmer(t6));
if (!t7.empty()) v.push_back(stemmer(t7));
if (!t8.empty()) v.push_back(stemmer(t8));
if (!t9.empty()) v.push_back(stemmer(t9));
if (!t10.empty()) v.push_back(stemmer(t10));
return Xapian::Query(op, v.begin(), v.end());
}
static Xapian::Query
query(Xapian::Query::op op, Xapian::termcount parameter,
const string & t1 = string(), const string & t2 = string(),
const string & t3 = string(), const string & t4 = string(),
const string & t5 = string(), const string & t6 = string(),
const string & t7 = string(), const string & t8 = string(),
const string & t9 = string(), const string & t10 = string())
{
vector<string> v;
Xapian::Stem stemmer("english");
if (!t1.empty()) v.push_back(stemmer(t1));
if (!t2.empty()) v.push_back(stemmer(t2));
if (!t3.empty()) v.push_back(stemmer(t3));
if (!t4.empty()) v.push_back(stemmer(t4));
if (!t5.empty()) v.push_back(stemmer(t5));
if (!t6.empty()) v.push_back(stemmer(t6));
if (!t7.empty()) v.push_back(stemmer(t7));
if (!t8.empty()) v.push_back(stemmer(t8));
if (!t9.empty()) v.push_back(stemmer(t9));
if (!t10.empty()) v.push_back(stemmer(t10));
return Xapian::Query(op, v.begin(), v.end(), parameter);
}
static Xapian::Query
query(const string &t)
{
return Xapian::Query(Xapian::Stem("english")(t));
}
// #######################################################################
// # Tests start here
// tests that the backend doesn't return zero docids
DEFINE_TESTCASE(zerodocid1, backend) {
// open the database (in this case a simple text file
// we prepared earlier)
Xapian::Database mydb(get_database("apitest_onedoc"));
Xapian::Enquire enquire(mydb);
// make a simple query, with one word in it - "word".
enquire.set_query(Xapian::Query("word"));
// retrieve the top ten results (we only expect one)
Xapian::MSet mymset = enquire.get_mset(0, 10);
// We've done the query, now check that the result is what
// we expect (1 document, with non-zero docid)
TEST_MSET_SIZE(mymset, 1);
TEST_AND_EXPLAIN(*(mymset.begin()) != 0,
"A query on a database returned a zero docid");
}
// tests that an empty query returns no matches
DEFINE_TESTCASE(emptyquery1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query());
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 0);
TEST_EQUAL(mymset.get_matches_lower_bound(), 0);
TEST_EQUAL(mymset.get_matches_upper_bound(), 0);
TEST_EQUAL(mymset.get_matches_estimated(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 0);
vector<Xapian::Query> v;
enquire.set_query(Xapian::Query(Xapian::Query::OP_AND, v.begin(), v.end()));
mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 0);
TEST_EQUAL(mymset.get_matches_lower_bound(), 0);
TEST_EQUAL(mymset.get_matches_upper_bound(), 0);
TEST_EQUAL(mymset.get_matches_estimated(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 0);
}
// tests the document count for a simple query
DEFINE_TESTCASE(simplequery1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("word"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 2);
}
// tests for the right documents and weights returned with simple query
DEFINE_TESTCASE(simplequery2, backend) {
// open the database (in this case a simple text file
// we prepared earlier)
Xapian::Database db = get_database("apitest_simpledata");
Xapian::Enquire enquire(db);
enquire.set_query(Xapian::Query("word"));
// retrieve the top results
Xapian::MSet mymset = enquire.get_mset(0, 10);
// We've done the query, now check that the result is what
// we expect (documents 2 and 4)
mset_expect_order(mymset, 2, 4);
// Check the weights
Xapian::MSetIterator i = mymset.begin();
// These weights are for BM25Weight(1,0,1,0.5,0.5)
TEST_EQUAL_DOUBLE(i.get_weight(), 1.04648168717725);
i++;
TEST_EQUAL_DOUBLE(i.get_weight(), 0.640987686595914);
}
// tests for the right document count for another simple query
DEFINE_TESTCASE(simplequery3, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
// Check that 6 documents were returned.
TEST_MSET_SIZE(mymset, 6);
}
// test that a multidb with 3 dbs query returns correct docids
DEFINE_TESTCASE(multidb2, backend && !multi) {
Xapian::Database mydb2(get_database("apitest_simpledata"));
mydb2.add_database(get_database("apitest_simpledata2"));
mydb2.add_database(get_database("apitest_termorder"));
Xapian::Enquire enquire(mydb2);
// make a query
Xapian::Query myquery = query(Xapian::Query::OP_OR, "inmemory", "word");
enquire.set_weighting_scheme(Xapian::BoolWeight());
enquire.set_query(myquery);
// retrieve the top ten results
Xapian::MSet mymset = enquire.get_mset(0, 10);
mset_expect_order(mymset, 2, 3, 4, 10);
}
// tests that when specifying maxitems to get_mset, no more than
// that are returned.
DEFINE_TESTCASE(msetmaxitems1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 1);
TEST_MSET_SIZE(mymset, 1);
mymset = enquire.get_mset(0, 5);
TEST_MSET_SIZE(mymset, 5);
}
// tests the returned weights are as expected (regression test for remote
// backend which was using the average weight rather than the actual document
// weight for computing weights - fixed in 1.0.0).
DEFINE_TESTCASE(expandweights1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
Xapian::ESet eset = enquire.get_eset(3, myrset, enquire.USE_EXACT_TERMFREQ);
TEST_EQUAL(eset.size(), 3);
TEST_REL(eset.get_ebound(), >=, eset.size());
TEST_EQUAL_DOUBLE(eset[0].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[1].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[2].get_weight(), 4.73383620844021);
// Test non-default k too.
eset = enquire.get_eset(3, myrset, enquire.USE_EXACT_TERMFREQ, 2.0);
TEST_EQUAL(eset.size(), 3);
TEST_REL(eset.get_ebound(), >=, eset.size());
TEST_EQUAL_DOUBLE(eset[0].get_weight(), 5.88109547674955);
TEST_EQUAL_DOUBLE(eset[1].get_weight(), 5.88109547674955);
TEST_EQUAL_DOUBLE(eset[2].get_weight(), 5.44473599216144);
}
// Just like test_expandweights1 but without USE_EXACT_TERMFREQ.
DEFINE_TESTCASE(expandweights2, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
Xapian::ESet eset = enquire.get_eset(3, myrset);
TEST_EQUAL(eset.size(), 3);
TEST_REL(eset.get_ebound(), >=, eset.size());
// With a multi backend, the top three terms all happen to occur in both
// shard so their termfreq is exactly known even without
// USE_EXACT_TERMFREQ and so the weights should be the same for all
// test harness backends.
TEST_EQUAL_DOUBLE(eset[0].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[1].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[2].get_weight(), 4.73383620844021);
}
DEFINE_TESTCASE(expandweights3, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
// Set min_wt to 6.0
Xapian::ESet eset = enquire.get_eset(50, myrset, 0, 0, 6.0);
TEST_EQUAL(eset.size(), 2);
TEST_REL(eset.get_ebound(), >=, eset.size());
// With a multi backend, the top two terms all happen to occur in both
// shard so their termfreq is exactly known even without
// USE_EXACT_TERMFREQ and so the weights should be the same for all
// test harness backends.
TEST_EQUAL_DOUBLE(eset[0].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[1].get_weight(), 6.08904001099445);
}
// tests that negative weights are returned
DEFINE_TESTCASE(expandweights4, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("paragraph"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
Xapian::ESet eset = enquire.get_eset(37, myrset, 0, 0, -100);
// Now include negative weights
TEST_EQUAL(eset.size(), 37);
TEST_REL(eset.get_ebound(), >=, eset.size());
TEST_REL(eset[36].get_weight(), <, 0);
TEST_REL(eset[36].get_weight(), >=, -100);
}
// test for Bo1EWeight
DEFINE_TESTCASE(expandweights5, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
enquire.set_expansion_scheme("bo1");
Xapian::ESet eset = enquire.get_eset(3, myrset);
TEST_EQUAL(eset.size(), 3);
TEST_REL(eset.get_ebound(), >=, eset.size());
TEST_EQUAL_DOUBLE(eset[0].get_weight(), 7.21765284821702);
TEST_EQUAL_DOUBLE(eset[1].get_weight(), 6.661623193760022);
TEST_EQUAL_DOUBLE(eset[2].get_weight(), 5.58090119783738);
}
// test that "prob" and "trad" can be set as the expansion scheme.
DEFINE_TESTCASE(expandweights6, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
enquire.set_expansion_scheme("prob");
Xapian::ESet eset = enquire.get_eset(3, myrset, enquire.USE_EXACT_TERMFREQ);
TEST_EQUAL(eset.size(), 3);
TEST_REL(eset.get_ebound(), >=, eset.size());
TEST_EQUAL_DOUBLE(eset[0].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[1].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[2].get_weight(), 4.73383620844021);
// Older scheme name "trad" (alias for "prob").
enquire.set_expansion_scheme("trad");
eset = enquire.get_eset(3, myrset, enquire.USE_EXACT_TERMFREQ);
TEST_EQUAL(eset.size(), 3);
TEST_REL(eset.get_ebound(), >=, eset.size());
TEST_EQUAL_DOUBLE(eset[0].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[1].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[2].get_weight(), 4.73383620844021);
}
// test that invalid scheme names are not accepted
DEFINE_TESTCASE(expandweights7, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
enquire.set_expansion_scheme("no_such_scheme"));
}
// test that "expand_k" can be passed as a parameter to get_eset
DEFINE_TESTCASE(expandweights8, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
// Set expand_k to 1.0 and min_wt to 0
Xapian::ESet eset = enquire.get_eset(50, myrset, 0, 1.0, 0, 0);
// With a multi backend, the top three terms all happen to occur in both
// shard so their termfreq is exactly known even without
// USE_EXACT_TERMFREQ and so the weights should be the same for all
// test harness backends.
TEST_EQUAL_DOUBLE(eset[0].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[1].get_weight(), 6.08904001099445);
TEST_EQUAL_DOUBLE(eset[2].get_weight(), 4.73383620844021);
TEST_REL(eset.back().get_weight(),>=,0);
}
// tests that when specifying maxitems to get_eset, no more than
// that are returned.
DEFINE_TESTCASE(expandmaxitems1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
tout << "mymset.size() = " << mymset.size() << '\n';
TEST(mymset.size() >= 2);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
Xapian::ESet myeset = enquire.get_eset(1, myrset);
TEST_EQUAL(myeset.size(), 1);
TEST_REL(myeset.get_ebound(), >=, myeset.size());
}
// tests that a pure boolean query has all weights set to 0
DEFINE_TESTCASE(boolquery1, backend) {
Xapian::Query myboolquery(query("this"));
// open the database (in this case a simple text file
// we prepared earlier)
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(myboolquery);
enquire.set_weighting_scheme(Xapian::BoolWeight());
// retrieve the top results
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST_NOT_EQUAL(mymset.size(), 0);
TEST_EQUAL(mymset.get_max_possible(), 0);
for (Xapian::MSetIterator i = mymset.begin(); i != mymset.end(); ++i) {
TEST_EQUAL(i.get_weight(), 0);
}
}
// tests that get_mset() specifying "this" works as expected
DEFINE_TESTCASE(msetfirst1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(query("this"));
Xapian::MSet mymset1 = enquire.get_mset(0, 6);
Xapian::MSet mymset2 = enquire.get_mset(3, 3);
TEST(mset_range_is_same(mymset1, 3, mymset2, 0, 3));
// Regression test - we weren't adjusting the index into items[] by
// firstitem in api/omenquire.cc.
TEST_EQUAL(mymset1[5].get_document().get_data(),
mymset2[2].get_document().get_data());
}
// tests the converting-to-percent functions
DEFINE_TESTCASE(topercent1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 20);
int last_pct = 100;
Xapian::MSetIterator i = mymset.begin();
for ( ; i != mymset.end(); ++i) {
int pct = mymset.convert_to_percent(i);
TEST_AND_EXPLAIN(pct == i.get_percent(),
"convert_to_%(msetitor) != convert_to_%(wt)");
TEST_AND_EXPLAIN(pct == mymset.convert_to_percent(i.get_weight()),
"convert_to_%(msetitor) != convert_to_%(wt)");
TEST_AND_EXPLAIN(pct >= 0 && pct <= 100,
"percentage out of range: " << pct);
TEST_AND_EXPLAIN(pct <= last_pct, "percentage increased down mset");
last_pct = pct;
}
}
// tests the percentage values returned
DEFINE_TESTCASE(topercent2, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
int pct;
// First, test a search in which the top document scores 100%.
enquire.set_query(query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 20);
Xapian::MSetIterator i = mymset.begin();
TEST(i != mymset.end());
pct = mymset.convert_to_percent(i);
TEST_EQUAL(pct, 100);
TEST_EQUAL(mymset.get_matches_lower_bound(), 6);
TEST_EQUAL(mymset.get_matches_upper_bound(), 6);
TEST_EQUAL(mymset.get_matches_estimated(), 6);
TEST_EQUAL_DOUBLE(mymset.get_max_attained(), 0.0553904060041786);
TEST_EQUAL(mymset.size(), 6);
mset_expect_order(mymset, 2, 1, 3, 5, 6, 4);
// A search in which the top document doesn't have 100%
Xapian::Query q = query(Xapian::Query::OP_OR,
"this", "line", "paragraph", "rubbish");
enquire.set_query(q);
mymset = enquire.get_mset(0, 20);
i = mymset.begin();
TEST(i != mymset.end());
pct = mymset.convert_to_percent(i);
TEST_REL(pct,>,60);
TEST_REL(pct,<,76);
++i;
TEST(i != mymset.end());
pct = mymset.convert_to_percent(i);
TEST_REL(pct,>,40);
TEST_REL(pct,<,50);
TEST_EQUAL(mymset.get_matches_lower_bound(), 6);
TEST_EQUAL(mymset.get_matches_upper_bound(), 6);
TEST_EQUAL(mymset.get_matches_estimated(), 6);
TEST_EQUAL_DOUBLE(mymset.get_max_attained(), 1.67412192414056);
TEST_EQUAL(mymset.size(), 6);
mset_expect_order(mymset, 3, 1, 4, 2, 5, 6);
}
class EvenParityExpandFunctor : public Xapian::ExpandDecider {
public:
bool operator()(const string& tname) const override {
unsigned long sum = 0;
for (unsigned ch : tname) {
sum += ch;
}
// if (verbose) {
// tout << tname << "==> " << sum << "\n";
// }
return (sum % 2) == 0;
}
};
// tests the expand decision functor
DEFINE_TESTCASE(expandfunctor1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST(mymset.size() >= 2);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
EvenParityExpandFunctor myfunctor;
Xapian::ESet myeset_orig = enquire.get_eset(1000, myrset);
unsigned int neweset_size = 0;
Xapian::ESetIterator j = myeset_orig.begin();
for ( ; j != myeset_orig.end(); ++j) {
if (myfunctor(*j)) neweset_size++;
}
Xapian::ESet myeset = enquire.get_eset(neweset_size, myrset, &myfunctor);
#if 0
// Compare myeset with the hand-filtered version of myeset_orig.
if (verbose) {
tout << "orig_eset: ";
copy(myeset_orig.begin(), myeset_orig.end(),
ostream_iterator<Xapian::ESetItem>(tout, " "));
tout << "\n";
tout << "new_eset: ";
copy(myeset.begin(), myeset.end(),
ostream_iterator<Xapian::ESetItem>(tout, " "));
tout << "\n";
}
#endif
Xapian::ESetIterator orig = myeset_orig.begin();
Xapian::ESetIterator filt = myeset.begin();
for (; orig != myeset_orig.end() && filt != myeset.end(); ++orig, ++filt) {
// skip over items that shouldn't be in myeset
while (orig != myeset_orig.end() && !myfunctor(*orig)) {
++orig;
}
TEST_AND_EXPLAIN(*orig == *filt &&
orig.get_weight() == filt.get_weight(),
"Mismatch in items " << *orig << " vs. " << *filt
<< " after filtering");
}
while (orig != myeset_orig.end() && !myfunctor(*orig)) {
++orig;
}
TEST_EQUAL(orig, myeset_orig.end());
TEST_AND_EXPLAIN(filt == myeset.end(),
"Extra items in the filtered eset.");
}
DEFINE_TESTCASE(expanddeciderfilterprefix2, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST(mymset.size() >= 2);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
Xapian::ESet myeset_orig = enquire.get_eset(1000, myrset);
unsigned int neweset_size = 0;
// Choose the first char in the first term as prefix.
Xapian::ESetIterator j = myeset_orig.begin();
TEST(myeset_orig.size() >= 1);
string prefix(*j, 0, 1);
Xapian::ExpandDeciderFilterPrefix myfunctor(prefix);
for ( ; j != myeset_orig.end(); ++j) {
if (myfunctor(*j)) neweset_size++;
}
Xapian::ESet myeset = enquire.get_eset(neweset_size, myrset, &myfunctor);
Xapian::ESetIterator orig = myeset_orig.begin();
Xapian::ESetIterator filt = myeset.begin();
for (; orig != myeset_orig.end() && filt != myeset.end(); ++orig, ++filt) {
// skip over items that shouldn't be in myeset
while (orig != myeset_orig.end() && !myfunctor(*orig)) {
++orig;
}
TEST_AND_EXPLAIN(*orig == *filt &&
orig.get_weight() == filt.get_weight(),
"Mismatch in items " << *orig << " vs. " << *filt
<< " after filtering");
}
while (orig != myeset_orig.end() && !myfunctor(*orig)) {
++orig;
}
TEST_EQUAL(orig, myeset_orig.end());
TEST_AND_EXPLAIN(filt == myeset.end(),
"Extra items in the filtered eset.");
}
// tests the percent cutoff option
DEFINE_TESTCASE(pctcutoff1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(query(Xapian::Query::OP_OR,
"this", "line", "paragraph", "rubbish"));
Xapian::MSet mymset1 = enquire.get_mset(0, 100);
if (verbose) {
tout << "Original mset pcts:";
print_mset_percentages(mymset1);
tout << "\n";
}
unsigned int num_items = 0;
int my_pct = 100;
int changes = 0;
Xapian::MSetIterator i = mymset1.begin();
int c = 0;
for ( ; i != mymset1.end(); ++i, ++c) {
int new_pct = mymset1.convert_to_percent(i);
if (new_pct != my_pct) {
changes++;
if (changes > 3) break;
num_items = c;
my_pct = new_pct;
}
}
TEST_AND_EXPLAIN(changes > 3, "MSet not varied enough to test");
if (verbose) {
tout << "Cutoff percent: " << my_pct << "\n";
}
enquire.set_cutoff(my_pct);
Xapian::MSet mymset2 = enquire.get_mset(0, 100);
if (verbose) {
tout << "Percentages after cutoff:";
print_mset_percentages(mymset2);
tout << "\n";
}
TEST_AND_EXPLAIN(mymset2.size() >= num_items,
"Match with % cutoff lost too many items");
TEST_AND_EXPLAIN(mymset2.size() == num_items ||
(mymset2.convert_to_percent(mymset2[num_items]) == my_pct &&
mymset2.convert_to_percent(mymset2.back()) == my_pct),
"Match with % cutoff returned too many items");
}
// Tests the percent cutoff option combined with collapsing
DEFINE_TESTCASE(pctcutoff2, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query(Xapian::Query::OP_AND_NOT, Xapian::Query("this"), Xapian::Query("banana")));
Xapian::MSet mset = enquire.get_mset(0, 100);
if (verbose) {
tout << "Original mset pcts:";
print_mset_percentages(mset);
tout << "\n";
}
TEST(mset.size() >= 2);
TEST(mset[0].get_percent() - mset[1].get_percent() >= 2);
int cutoff = mset[0].get_percent() + mset[1].get_percent();
cutoff /= 2;
enquire.set_cutoff(cutoff);
enquire.set_collapse_key(1234); // Value which is always empty.
Xapian::MSet mset2 = enquire.get_mset(0, 1);
TEST_EQUAL(mset2.size(), 1);
TEST_REL(mset2.get_matches_lower_bound(),>=,1);
TEST_REL(mset2.get_uncollapsed_matches_lower_bound(),>=,
mset2.get_matches_lower_bound());
TEST_REL(mset2.get_uncollapsed_matches_lower_bound(),<=,mset.size());
TEST_REL(mset2.get_uncollapsed_matches_upper_bound(),>=,mset.size());
TEST_REL(mset2.get_uncollapsed_matches_lower_bound(),<=,mset2.get_uncollapsed_matches_estimated());
TEST_REL(mset2.get_uncollapsed_matches_upper_bound(),>=,mset2.get_uncollapsed_matches_estimated());
}
// Test that the percent cutoff option returns all the answers it should.
DEFINE_TESTCASE(pctcutoff3, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
Xapian::MSet mset1 = enquire.get_mset(0, 10);
if (verbose) {
tout << "Original mset pcts:";
print_mset_percentages(mset1);
tout << "\n";
}
int percent = 100;
for (Xapian::MSetIterator i = mset1.begin(); i != mset1.end(); ++i) {
int new_percent = mset1.convert_to_percent(i);
if (new_percent != percent) {
tout.str(string());
tout << "Testing " << percent << "% cutoff\n";
enquire.set_cutoff(percent);
Xapian::MSet mset2 = enquire.get_mset(0, 10);
TEST_EQUAL(mset2.back().get_percent(), percent);
TEST_EQUAL(mset2.size(), i.get_rank());
percent = new_percent;
}
}
}
// tests the cutoff option
DEFINE_TESTCASE(cutoff1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(query(Xapian::Query::OP_OR,
"this", "line", "paragraph", "rubbish"));
Xapian::MSet mymset1 = enquire.get_mset(0, 100);
if (verbose) {
tout << "Original mset weights:";
print_mset_weights(mymset1);
tout << "\n";
}
unsigned int num_items = 0;
double my_wt = -100;
int changes = 0;
Xapian::MSetIterator i = mymset1.begin();
int c = 0;
for ( ; i != mymset1.end(); ++i, ++c) {
double new_wt = i.get_weight();
if (new_wt != my_wt) {
changes++;
if (changes > 3) break;
num_items = c;
my_wt = new_wt;
}
}
TEST_AND_EXPLAIN(changes > 3, "MSet not varied enough to test");
if (verbose) {
tout << "Cutoff weight: " << my_wt << "\n";
}
enquire.set_cutoff(0, my_wt);
Xapian::MSet mymset2 = enquire.get_mset(0, 100);
if (verbose) {
tout << "Weights after cutoff:";
print_mset_weights(mymset2);
tout << "\n";
}
TEST_AND_EXPLAIN(mymset2.size() >= num_items,
"Match with cutoff lost too many items");
TEST_AND_EXPLAIN(mymset2.size() == num_items ||
(mymset2[num_items].get_weight() == my_wt &&
mymset2.back().get_weight() == my_wt),
"Match with cutoff returned too many items");
}
// tests the allow query terms expand option
DEFINE_TESTCASE(allowqterms1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
string term = "paragraph";
enquire.set_query(Xapian::Query(term));
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST(mymset.size() >= 2);
Xapian::RSet myrset;
Xapian::MSetIterator i = mymset.begin();
myrset.add_document(*i);
myrset.add_document(*(++i));
Xapian::ESet myeset = enquire.get_eset(1000, myrset);
Xapian::ESetIterator j = myeset.begin();
for ( ; j != myeset.end(); ++j) {
TEST_NOT_EQUAL(*j, term);
}
Xapian::ESet myeset2 = enquire.get_eset(1000, myrset, Xapian::Enquire::INCLUDE_QUERY_TERMS);
j = myeset2.begin();
for ( ; j != myeset2.end(); ++j) {
if (*j == term) break;
}
TEST(j != myeset2.end());
}
// tests that the MSet max_attained works
DEFINE_TESTCASE(maxattain1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(query("this"));
Xapian::MSet mymset = enquire.get_mset(0, 100);
double mymax = 0;
Xapian::MSetIterator i = mymset.begin();
for ( ; i != mymset.end(); ++i) {
if (i.get_weight() > mymax) mymax = i.get_weight();
}
TEST_EQUAL(mymax, mymset.get_max_attained());
}
// tests a reversed boolean query
DEFINE_TESTCASE(reversebool1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
enquire.set_weighting_scheme(Xapian::BoolWeight());
Xapian::MSet mymset1 = enquire.get_mset(0, 100);
TEST_AND_EXPLAIN(mymset1.size() > 1,
"Mset was too small to test properly");
enquire.set_docid_order(Xapian::Enquire::ASCENDING);
Xapian::MSet mymset2 = enquire.get_mset(0, 100);
enquire.set_docid_order(Xapian::Enquire::DESCENDING);
Xapian::MSet mymset3 = enquire.get_mset(0, 100);
// mymset1 and mymset2 should be identical
TEST_EQUAL(mymset1.size(), mymset2.size());
{
Xapian::MSetIterator i = mymset1.begin();
Xapian::MSetIterator j = mymset2.begin();
for ( ; i != mymset1.end(); ++i, j++) {
TEST(j != mymset2.end());
// if this fails, then setting match_sort_forward=true was not
// the same as the default.
TEST_EQUAL(*i, *j);
}
TEST(j == mymset2.end());
}
// mymset1 and mymset3 should be same but reversed
TEST_EQUAL(mymset1.size(), mymset3.size());
{
Xapian::MSetIterator i = mymset1.begin();
Xapian::MSetIterator j = mymset3.end();
for ( ; i != mymset1.end(); ++i) {
--j;
// if this fails, then setting match_sort_forward=false didn't
// reverse the results.
TEST_EQUAL(*i, *j);
}
}
}
// tests a reversed boolean query, where the full mset isn't returned
DEFINE_TESTCASE(reversebool2, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
enquire.set_weighting_scheme(Xapian::BoolWeight());
Xapian::MSet mymset1 = enquire.get_mset(0, 100);
TEST_AND_EXPLAIN(mymset1.size() > 1,
"Mset was too small to test properly");
enquire.set_docid_order(Xapian::Enquire::ASCENDING);
Xapian::doccount msize = mymset1.size() / 2;
Xapian::MSet mymset2 = enquire.get_mset(0, msize);
enquire.set_docid_order(Xapian::Enquire::DESCENDING);
Xapian::MSet mymset3 = enquire.get_mset(0, msize);
// mymset2 should be first msize items of mymset1
TEST_EQUAL(msize, mymset2.size());
{
Xapian::MSetIterator i = mymset1.begin();
Xapian::MSetIterator j = mymset2.begin();
for ( ; j != mymset2.end(); ++i, ++j) {
TEST(i != mymset1.end());
// if this fails, then setting match_sort_forward=true was not
// the same as the default.
TEST_EQUAL(*i, *j);
}
// mymset1 should be larger.
TEST(i != mymset1.end());
}
// mymset3 should be last msize items of mymset1, in reverse order
TEST_EQUAL(msize, mymset3.size());
{
Xapian::MSetIterator i = mymset1.end();
Xapian::MSetIterator j;
for (j = mymset3.begin(); j != mymset3.end(); ++j) {
// if this fails, then setting match_sort_forward=false didn't
// reverse the results.
--i;
TEST_EQUAL(*i, *j);
}
}
}
// tests that get_matching_terms() returns the terms in the right order
DEFINE_TESTCASE(getmterms1, backend) {
list<string> answers_list;
answers_list.push_back("one");
answers_list.push_back("two");
answers_list.push_back("three");
answers_list.push_back("four");
Xapian::Database mydb(get_database("apitest_termorder"));
Xapian::Enquire enquire(mydb);
Xapian::Query myquery(Xapian::Query::OP_OR,
Xapian::Query(Xapian::Query::OP_AND,
Xapian::Query("one", 1, 1),
Xapian::Query("three", 1, 3)),
Xapian::Query(Xapian::Query::OP_OR,
Xapian::Query("four", 1, 4),
Xapian::Query("two", 1, 2)));
enquire.set_query(myquery);
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 1);
list<string> list(enquire.get_matching_terms_begin(mymset.begin()),
enquire.get_matching_terms_end(mymset.begin()));
TEST(list == answers_list);
}
// tests that get_matching_terms() returns the terms only once
DEFINE_TESTCASE(getmterms2, backend) {
list<string> answers_list;
answers_list.push_back("one");
answers_list.push_back("two");
answers_list.push_back("three");
Xapian::Database mydb(get_database("apitest_termorder"));
Xapian::Enquire enquire(mydb);
Xapian::Query myquery(Xapian::Query::OP_OR,
Xapian::Query(Xapian::Query::OP_AND,
Xapian::Query("one", 1, 1),
Xapian::Query("three", 1, 3)),
Xapian::Query(Xapian::Query::OP_OR,
Xapian::Query("one", 1, 4),
Xapian::Query("two", 1, 2)));
enquire.set_query(myquery);
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 1);
list<string> list(enquire.get_matching_terms_begin(mymset.begin()),
enquire.get_matching_terms_end(mymset.begin()));
TEST(list == answers_list);
}
// test that running a query twice returns the same results
DEFINE_TESTCASE(repeatquery1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
enquire.set_query(query(Xapian::Query::OP_OR, "this", "word"));
Xapian::MSet mymset1 = enquire.get_mset(0, 10);
Xapian::MSet mymset2 = enquire.get_mset(0, 10);
TEST_EQUAL(mymset1, mymset2);
}
// test that prefetching documents works (at least, gives same results)
DEFINE_TESTCASE(fetchdocs1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("this"));
enquire.set_query(query(Xapian::Query::OP_OR, "this", "word"));
Xapian::MSet mymset1 = enquire.get_mset(0, 10);
Xapian::MSet mymset2 = enquire.get_mset(0, 10);
TEST_EQUAL(mymset1, mymset2);
mymset2.fetch(mymset2[0], mymset2[mymset2.size() - 1]);
mymset2.fetch(mymset2.begin(), mymset2.end());
mymset2.fetch(mymset2.begin());
mymset2.fetch();
Xapian::MSetIterator it1 = mymset1.begin();
Xapian::MSetIterator it2 = mymset2.begin();
while (it1 != mymset1.end() && it2 != mymset2.end()) {
TEST_EQUAL(it1.get_document().get_data(),
it2.get_document().get_data());
TEST_NOT_EQUAL(it1.get_document().get_data(), "");
TEST_NOT_EQUAL(it2.get_document().get_data(), "");
it1++;
it2++;
}
TEST_EQUAL(it1, mymset1.end());
TEST_EQUAL(it1, mymset2.end());
}
// test that searching for a term not in the database fails nicely
DEFINE_TESTCASE(absentterm1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_weighting_scheme(Xapian::BoolWeight());
enquire.set_query(Xapian::Query("frink"));
Xapian::MSet mymset = enquire.get_mset(0, 10);
mset_expect_order(mymset);
}
// as absentterm1, but setting query from a vector of terms
DEFINE_TESTCASE(absentterm2, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
vector<string> terms;
terms.push_back("frink");
Xapian::Query query(Xapian::Query::OP_OR, terms.begin(), terms.end());
enquire.set_query(query);
Xapian::MSet mymset = enquire.get_mset(0, 10);
mset_expect_order(mymset);
}
// test that rsets do sensible things
DEFINE_TESTCASE(rset1, backend) {
Xapian::Database mydb(get_database("apitest_rset"));
Xapian::Enquire enquire(mydb);
Xapian::Query myquery = query(Xapian::Query::OP_OR, "giraffe", "tiger");
enquire.set_query(myquery);
Xapian::MSet mymset1 = enquire.get_mset(0, 10);
Xapian::RSet myrset;
myrset.add_document(1);
Xapian::MSet mymset2 = enquire.get_mset(0, 10, &myrset);
// We should have the same documents turn up, but 1 and 3 should
// have higher weights with the RSet.
TEST_MSET_SIZE(mymset1, 3);
TEST_MSET_SIZE(mymset2, 3);
}
/// Test that an RSet affects MSet result order.
DEFINE_TESTCASE(rset2, backend) {
Xapian::Database mydb(get_database("apitest_rset"));
Xapian::Enquire enquire(mydb);
Xapian::Query myquery = query(Xapian::Query::OP_OR, "cuddly", "people");
enquire.set_query(myquery);
// Test with the default BM25Weight, then with TradWeight.
for (int i = 0; i < 2; ++i) {
Xapian::MSet mymset1 = enquire.get_mset(0, 10);
Xapian::RSet myrset;
myrset.add_document(2);
Xapian::MSet mymset2 = enquire.get_mset(0, 10, &myrset);
mset_expect_order(mymset1, 1, 2);
// Document 2 should have higher weight than document 1 despite the wdf
// of "people" being 1 because "people" indexes a document in the RSet
// whereas "cuddly" (wdf=2) does not.
mset_expect_order(mymset2, 2, 1);
enquire.set_weighting_scheme(Xapian::TradWeight());
}
}
// test that rsets behave correctly with multiDBs
DEFINE_TESTCASE(rsetmultidb1, backend && !multi) {
Xapian::Database mydb1(get_database("apitest_rset", "apitest_simpledata2"));
Xapian::Database mydb2(get_database("apitest_rset"));
mydb2.add_database(get_database("apitest_simpledata2"));
Xapian::Enquire enquire1(mydb1);
Xapian::Enquire enquire2(mydb2);
Xapian::Query myquery = query(Xapian::Query::OP_OR, "cuddly", "multiple");
enquire1.set_query(myquery);
enquire2.set_query(myquery);
Xapian::RSet myrset1;
Xapian::RSet myrset2;
myrset1.add_document(4);
myrset2.add_document(2);
Xapian::MSet mymset1a = enquire1.get_mset(0, 10);
Xapian::MSet mymset1b = enquire1.get_mset(0, 10, &myrset1);
Xapian::MSet mymset2a = enquire2.get_mset(0, 10);
Xapian::MSet mymset2b = enquire2.get_mset(0, 10, &myrset2);
mset_expect_order(mymset1a, 1, 4);
mset_expect_order(mymset1b, 4, 1);
mset_expect_order(mymset2a, 1, 2);
mset_expect_order(mymset2b, 2, 1);
TEST(mset_range_is_same_weights(mymset1a, 0, mymset2a, 0, 2));
TEST(mset_range_is_same_weights(mymset1b, 0, mymset2b, 0, 2));
TEST_NOT_EQUAL(mymset1a, mymset1b);
TEST_NOT_EQUAL(mymset2a, mymset2b);
}
// regression tests - used to cause assertion in stats.h to fail
// Doesn't actually fail for multi but it doesn't make sense to run there.
DEFINE_TESTCASE(rsetmultidb3, backend && !multi) {
Xapian::Enquire enquire(get_database("apitest_simpledata2"));
enquire.set_query(query(Xapian::Query::OP_OR, "cuddly", "people"));
Xapian::MSet mset = enquire.get_mset(0, 10); // used to fail assertion
}
/// Simple test of the elite set operator.
DEFINE_TESTCASE(eliteset1, backend && !multi) {
Xapian::Database mydb(get_database("apitest_simpledata"));
Xapian::Enquire enquire(mydb);
Xapian::Query myquery1 = query(Xapian::Query::OP_OR, "word");
Xapian::Query myquery2 = query(Xapian::Query::OP_ELITE_SET, 1,
"simple", "word");
enquire.set_query(myquery1, 2); // So the query lengths are the same.
Xapian::MSet mymset1 = enquire.get_mset(0, 10);
enquire.set_query(myquery2);
Xapian::MSet mymset2 = enquire.get_mset(0, 10);
TEST_EQUAL(mymset1, mymset2);
}
/// Multi-backend variant of eliteset1.
DEFINE_TESTCASE(elitesetmulti1, multi) {
Xapian::Database mydb(get_database("apitest_simpledata"));
Xapian::Enquire enquire(mydb);
Xapian::Query myquery2 = query(Xapian::Query::OP_ELITE_SET, 1,
"simple", "word");
enquire.set_query(myquery2);
Xapian::MSet mymset2 = enquire.get_mset(0, 10);
// For a sharded database, the elite set is resolved per shard and can
// select different terms because the max term weights vary with the
// per-shard term statistics. I can't see a feasible way to create
// an equivalent MSet to compare with so for now at least we hard-code
// the expected values.
TEST_EQUAL(mymset2.size(), 3);
TEST_EQUAL(mymset2.get_matches_lower_bound(), 3);
TEST_EQUAL(mymset2.get_matches_estimated(), 3);
TEST_EQUAL(mymset2.get_matches_upper_bound(), 3);
TEST_EQUAL_DOUBLE(mymset2.get_max_possible(), 1.1736756775723788948);
TEST_EQUAL_DOUBLE(mymset2.get_max_attained(), 1.0464816871772451012);
mset_expect_order(mymset2, 2, 4, 5);
TEST_EQUAL_DOUBLE(mymset2[0].get_weight(), 1.0464816871772451012);
TEST_EQUAL_DOUBLE(mymset2[1].get_weight(), 0.64098768659591376373);
TEST_EQUAL_DOUBLE(mymset2[2].get_weight(), 0.46338869498075929698);
}
/// Test that the elite set operator works if the set contains
/// sub-expressions (regression test)
DEFINE_TESTCASE(eliteset2, backend && !multi) {
Xapian::Database mydb(get_database("apitest_simpledata"));
Xapian::Enquire enquire(mydb);
Xapian::Query myquery1 = query(Xapian::Query::OP_AND, "word", "search");
vector<Xapian::Query> qs;
qs.push_back(query("this"));
qs.push_back(query(Xapian::Query::OP_AND, "word", "search"));
Xapian::Query myquery2(Xapian::Query::OP_ELITE_SET,
qs.begin(), qs.end(), 1);
enquire.set_query(myquery1);
Xapian::MSet mymset1 = enquire.get_mset(0, 10);
enquire.set_query(myquery2);
Xapian::MSet mymset2 = enquire.get_mset(0, 10);
TEST_EQUAL(mymset1, mymset2);
}
/// Multi-backend variant of eliteset2.
DEFINE_TESTCASE(elitesetmulti2, multi) {
Xapian::Database mydb(get_database("apitest_simpledata"));
Xapian::Enquire enquire(mydb);
Xapian::Query myquery1 = query(Xapian::Query::OP_AND, "word", "search");
vector<Xapian::Query> qs;
qs.push_back(query("this"));
qs.push_back(query(Xapian::Query::OP_AND, "word", "search"));
Xapian::Query myquery2(Xapian::Query::OP_ELITE_SET,
qs.begin(), qs.end(), 1);
enquire.set_query(myquery2);
Xapian::MSet mymset2 = enquire.get_mset(0, 10);
// For a sharded database, the elite set is resolved per shard and can
// select different terms because the max term weights vary with the
// per-shard term statistics. I can't see a feasible way to create
// an equivalent MSet to compare with so for now at least we hard-code
// the expected values.
TEST_EQUAL(mymset2.size(), 4);
TEST_EQUAL(mymset2.get_matches_lower_bound(), 4);
TEST_EQUAL(mymset2.get_matches_estimated(), 4);
TEST_EQUAL(mymset2.get_matches_upper_bound(), 4);
TEST_EQUAL_DOUBLE(mymset2.get_max_possible(), 2.6585705165783908299);
TEST_EQUAL_DOUBLE(mymset2.get_max_attained(), 1.9700834242150864206);
mset_expect_order(mymset2, 2, 1, 3, 5);
TEST_EQUAL_DOUBLE(mymset2[0].get_weight(), 1.9700834242150864206);
TEST_EQUAL_DOUBLE(mymset2[1].get_weight(), 0.051103097360122341775);
TEST_EQUAL_DOUBLE(mymset2[2].get_weight(), 0.043131803408968119595);
TEST_EQUAL_DOUBLE(mymset2[3].get_weight(), 0.043131803408968119595);
}
/// Test that elite set doesn't affect query results if we have fewer
/// terms than the threshold
DEFINE_TESTCASE(eliteset3, backend) {
Xapian::Database mydb1(get_database("apitest_simpledata"));
Xapian::Enquire enquire1(mydb1);
Xapian::Database mydb2(get_database("apitest_simpledata"));
Xapian::Enquire enquire2(mydb2);
// make a query
Xapian::Stem stemmer("english");
string term1 = stemmer("word");
string term2 = stemmer("rubbish");
string term3 = stemmer("banana");
vector<string> terms;
terms.push_back(term1);
terms.push_back(term2);
terms.push_back(term3);
Xapian::Query myquery1(Xapian::Query::OP_OR, terms.begin(), terms.end());
enquire1.set_query(myquery1);
Xapian::Query myquery2(Xapian::Query::OP_ELITE_SET, terms.begin(), terms.end(), 3);
enquire2.set_query(myquery2);
// retrieve the results
Xapian::MSet mymset1 = enquire1.get_mset(0, 10);
Xapian::MSet mymset2 = enquire2.get_mset(0, 10);
TEST_EQUAL(mymset1, mymset2);
TEST_EQUAL(mymset1.get_termfreq(term1),
mymset2.get_termfreq(term1));
TEST_EQUAL(mymset1.get_termweight(term1),
mymset2.get_termweight(term1));
TEST_EQUAL(mymset1.get_termfreq(term2),
mymset2.get_termfreq(term2));
TEST_EQUAL(mymset1.get_termweight(term2),
mymset2.get_termweight(term2));
TEST_EQUAL(mymset1.get_termfreq(term3),
mymset2.get_termfreq(term3));
TEST_EQUAL(mymset1.get_termweight(term3),
mymset2.get_termweight(term3));
}
/// Test that elite set doesn't pick terms with 0 frequency
DEFINE_TESTCASE(eliteset4, backend && !multi) {
Xapian::Database mydb1(get_database("apitest_simpledata"));
Xapian::Enquire enquire1(mydb1);
Xapian::Database mydb2(get_database("apitest_simpledata"));
Xapian::Enquire enquire2(mydb2);
Xapian::Query myquery1 = query("rubbish");
Xapian::Query myquery2 = query(Xapian::Query::OP_ELITE_SET, 1,
"word", "rubbish", "fibble");
enquire1.set_query(myquery1);
enquire2.set_query(myquery2);
// retrieve the results
Xapian::MSet mymset1 = enquire1.get_mset(0, 10);
Xapian::MSet mymset2 = enquire2.get_mset(0, 10);
TEST_NOT_EQUAL(mymset2.size(), 0);
TEST_EQUAL(mymset1, mymset2);
}
/// Multi-backend variant of eliteset4.
DEFINE_TESTCASE(elitesetmulti4, multi) {
Xapian::Database mydb2(get_database("apitest_simpledata"));
Xapian::Enquire enquire2(mydb2);
Xapian::Query myquery2 = query(Xapian::Query::OP_ELITE_SET, 1,
"word", "rubbish", "fibble");
enquire2.set_query(myquery2);
// retrieve the results
Xapian::MSet mymset2 = enquire2.get_mset(0, 10);
// For a sharded database, the elite set is resolved per shard and can
// select different terms because the max term weights vary with the
// per-shard term statistics. I can't see a feasible way to create
// an equivalent MSet to compare with so for now at least we hard-code
// the expected values.
TEST_EQUAL(mymset2.size(), 3);
TEST_EQUAL(mymset2.get_matches_lower_bound(), 3);
TEST_EQUAL(mymset2.get_matches_estimated(), 3);
TEST_EQUAL(mymset2.get_matches_upper_bound(), 3);
TEST_EQUAL_DOUBLE(mymset2.get_max_possible(), 1.4848948390060121572);
TEST_EQUAL_DOUBLE(mymset2.get_max_attained(), 1.4848948390060121572);
mset_expect_order(mymset2, 3, 2, 4);
TEST_EQUAL_DOUBLE(mymset2[0].get_weight(), 1.4848948390060121572);
TEST_EQUAL_DOUBLE(mymset2[1].get_weight(), 1.0464816871772451012);
TEST_EQUAL_DOUBLE(mymset2[2].get_weight(), 0.64098768659591376373);
}
/// Regression test for problem with excess precision.
DEFINE_TESTCASE(eliteset5, backend) {
Xapian::Database mydb1(get_database("apitest_simpledata"));
Xapian::Enquire enquire1(mydb1);
vector<string> v;
for (int i = 0; i != 3; ++i) {
v.push_back("simpl");
v.push_back("queri");
v.push_back("rubbish");
v.push_back("rubbish");
v.push_back("rubbish");
v.push_back("word");
v.push_back("word");
v.push_back("word");
}
for (Xapian::termcount n = 1; n != v.size(); ++n) {
Xapian::Query myquery1 = Xapian::Query(Xapian::Query::OP_ELITE_SET,
v.begin(), v.end(), n);
myquery1 = Xapian::Query(Xapian::Query::OP_SCALE_WEIGHT,
myquery1,
0.004);
enquire1.set_query(myquery1);
// On architectures with excess precision (or, at least, on x86), the
// following call used to result in a segfault (at least when n=1).
enquire1.get_mset(0, 10);
}
}
/// Test that the termfreq returned by termlists is correct.
DEFINE_TESTCASE(termlisttermfreq1, backend) {
Xapian::Database mydb(get_database("apitest_simpledata"));
Xapian::Enquire enquire(mydb);
Xapian::Stem stemmer("english");
Xapian::RSet rset1;
Xapian::RSet rset2;
rset1.add_document(5);
rset2.add_document(6);
Xapian::ESet eset1 = enquire.get_eset(1000, rset1);
Xapian::ESet eset2 = enquire.get_eset(1000, rset2);
// search for weight of term 'another'
string theterm = stemmer("another");
double wt1 = 0;
double wt2 = 0;
{
Xapian::ESetIterator i = eset1.begin();
for ( ; i != eset1.end(); ++i) {
if (*i == theterm) {
wt1 = i.get_weight();
break;
}
}
}
{
Xapian::ESetIterator i = eset2.begin();
for ( ; i != eset2.end(); ++i) {
if (*i == theterm) {
wt2 = i.get_weight();
break;
}
}
}
TEST_NOT_EQUAL(wt1, 0);
TEST_NOT_EQUAL(wt2, 0);
TEST_EQUAL(wt1, wt2);
}
/// Test the termfreq and termweight info returned for query terms
DEFINE_TESTCASE(qterminfo1, backend) {
Xapian::Database mydb1(get_database("apitest_simpledata", "apitest_simpledata2"));
Xapian::Enquire enquire1(mydb1);
Xapian::Database mydb2(get_database("apitest_simpledata"));
mydb2.add_database(get_database("apitest_simpledata2"));
Xapian::Enquire enquire2(mydb2);
// make a query
Xapian::Stem stemmer("english");
string term1 = stemmer("word");
string term2 = stemmer("inmemory");
string term3 = stemmer("flibble");
Xapian::Query myquery(Xapian::Query::OP_OR,
Xapian::Query(term1),
Xapian::Query(Xapian::Query::OP_OR,
Xapian::Query(term2),
Xapian::Query(term3)));
enquire1.set_query(myquery);
enquire2.set_query(myquery);
for (int i = 1; i <= 2; ++i) {
// Retrieve the results.
Xapian::MSet mymset1a = enquire1.get_mset(0, 0);
Xapian::MSet mymset2a = enquire2.get_mset(0, 0);
TEST_EQUAL(mymset1a.get_termfreq(term1),
mymset2a.get_termfreq(term1));
TEST_EQUAL(mymset1a.get_termfreq(term2),
mymset2a.get_termfreq(term2));
TEST_EQUAL(mymset1a.get_termfreq(term3),
mymset2a.get_termfreq(term3));
TEST_EQUAL(mymset1a.get_termfreq(term1), 3);
TEST_EQUAL(mymset1a.get_termfreq(term2), 1);
TEST_EQUAL(mymset1a.get_termfreq(term3), 0);
TEST_NOT_EQUAL(mymset1a.get_termweight(term1), 0);
TEST_NOT_EQUAL(mymset1a.get_termweight(term2), 0);
// Non-existent terms should have zero weight.
TEST_EQUAL(mymset1a.get_termweight(term3), 0);
TEST_EQUAL(mymset1a.get_termfreq(stemmer("banana")), 1);
TEST_EXCEPTION(Xapian::InvalidArgumentError,
mymset1a.get_termweight(stemmer("banana")));
TEST_EQUAL(mymset1a.get_termfreq("sponge"), 0);
TEST_EXCEPTION(Xapian::InvalidArgumentError,
mymset1a.get_termweight("sponge"));
// Repeat tests with TradWeight. (Regression test to ensure
// non-existent terms get zero weight with TradWeight.)
enquire1.set_weighting_scheme(Xapian::TradWeight());
enquire2.set_weighting_scheme(Xapian::TradWeight());
}
}
/// Regression test for bug #37.
DEFINE_TESTCASE(qterminfo2, backend) {
Xapian::Database db(get_database("apitest_simpledata"));
Xapian::Enquire enquire(db);
// make a query
Xapian::Stem stemmer("english");
string term1 = stemmer("paragraph");
string term2 = stemmer("another");
enquire.set_query(Xapian::Query(term1));
Xapian::MSet mset0 = enquire.get_mset(0, 10);
TEST_NOT_EQUAL(mset0.get_termweight("paragraph"), 0);
Xapian::Query query(Xapian::Query::OP_AND_NOT, term1,
Xapian::Query(Xapian::Query::OP_AND, term1, term2));
enquire.set_query(query);
// retrieve the results
// Note: get_mset() used to throw "AssertionError" in debug builds
Xapian::MSet mset = enquire.get_mset(0, 10);
TEST_NOT_EQUAL(mset.get_termweight("paragraph"), 0);
}
// tests that when specifying that no items are to be returned, those
// statistics which should be the same are.
DEFINE_TESTCASE(msetzeroitems1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(query("this"));
Xapian::MSet mymset1 = enquire.get_mset(0, 0);
Xapian::MSet mymset2 = enquire.get_mset(0, 1);
TEST_EQUAL(mymset1.get_max_possible(), mymset2.get_max_possible());
}
// test that the matches_* of a simple query are as expected
DEFINE_TESTCASE(matches1, backend) {
bool remote = get_dbtype().find("remote") != string::npos;
Xapian::Database db = get_database("apitest_simpledata");
Xapian::Enquire enquire(db);
Xapian::Query myquery;
Xapian::MSet mymset;
myquery = query("word");
enquire.set_query(myquery);
mymset = enquire.get_mset(0, 10);
TEST_EQUAL(mymset.get_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_matches_estimated(), 2);
TEST_EQUAL(mymset.get_matches_upper_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 2);
myquery = query(Xapian::Query::OP_OR, "inmemory", "word");
enquire.set_query(myquery);
mymset = enquire.get_mset(0, 10);
TEST_EQUAL(mymset.get_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_matches_estimated(), 2);
TEST_EQUAL(mymset.get_matches_upper_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 2);
myquery = query(Xapian::Query::OP_AND, "inmemory", "word");
enquire.set_query(myquery);
mymset = enquire.get_mset(0, 10);
TEST_EQUAL(mymset.get_matches_lower_bound(), 0);
TEST_EQUAL(mymset.get_matches_estimated(), 0);
TEST_EQUAL(mymset.get_matches_upper_bound(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 0);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 0);
myquery = query(Xapian::Query::OP_AND, "simple", "word");
enquire.set_query(myquery);
mymset = enquire.get_mset(0, 10);
TEST_EQUAL(mymset.get_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_matches_estimated(), 2);
TEST_EQUAL(mymset.get_matches_upper_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 2);
myquery = query(Xapian::Query::OP_AND, "simple", "word");
enquire.set_query(myquery);
mymset = enquire.get_mset(0, 0);
if (db.size() == 1) {
// This isn't true for sharded DBs since there one sub-database has 3
// documents and simple and word both have termfreq of 2, so the
// matcher can tell at least one document must match!)
TEST_EQUAL(mymset.get_matches_lower_bound(), 0);
}
TEST_REL(mymset.get_matches_lower_bound(),<=,mymset.get_matches_estimated());
TEST_EQUAL(mymset.get_matches_estimated(), 1);
TEST_EQUAL(mymset.get_matches_upper_bound(), 2);
TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),<=,mymset.get_uncollapsed_matches_estimated());
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 2);
mymset = enquire.get_mset(0, 1);
TEST_EQUAL(mymset.get_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_matches_estimated(), 2);
TEST_EQUAL(mymset.get_matches_upper_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 2);
mymset = enquire.get_mset(0, 2);
TEST_EQUAL(mymset.get_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_matches_estimated(), 2);
TEST_EQUAL(mymset.get_matches_upper_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 2);
myquery = query(Xapian::Query::OP_AND, "paragraph", "another");
enquire.set_query(myquery);
mymset = enquire.get_mset(0, 0);
TEST_EQUAL(mymset.get_matches_lower_bound(), 1);
TEST_EQUAL(mymset.get_matches_estimated(), 2);
TEST_EQUAL(mymset.get_matches_upper_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 2);
mymset = enquire.get_mset(0, 1);
TEST_EQUAL(mymset.get_matches_lower_bound(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 1);
if (db.size() > 1 && remote) {
// The matcher can tell there's only one match in this case.
TEST_EQUAL(mymset.get_matches_estimated(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 1);
TEST_EQUAL(mymset.get_matches_upper_bound(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 1);
} else {
TEST_EQUAL(mymset.get_matches_estimated(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 2);
TEST_EQUAL(mymset.get_matches_upper_bound(), 2);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 2);
}
mymset = enquire.get_mset(0, 2);
TEST_EQUAL(mymset.get_matches_lower_bound(), 1);
TEST_EQUAL(mymset.get_matches_estimated(), 1);
TEST_EQUAL(mymset.get_matches_upper_bound(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 1);
mymset = enquire.get_mset(1, 20);
TEST_EQUAL(mymset.get_matches_lower_bound(), 1);
TEST_EQUAL(mymset.get_matches_estimated(), 1);
TEST_EQUAL(mymset.get_matches_upper_bound(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_estimated(), 1);
TEST_EQUAL(mymset.get_uncollapsed_matches_upper_bound(), 1);
}
// tests that wqf affects the document weights
DEFINE_TESTCASE(wqf1, backend) {
// Both queries have length 2; in q1 word has wqf=2, in q2 word has wqf=1
Xapian::Query q1("word", 2);
Xapian::Query q2("word");
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(q1);
Xapian::MSet mset1 = enquire.get_mset(0, 10);
enquire.set_query(q2);
Xapian::MSet mset2 = enquire.get_mset(0, 2);
// Check the weights
TEST(mset1.begin().get_weight() > mset2.begin().get_weight());
}
// tests that query length affects the document weights
DEFINE_TESTCASE(qlen1, backend) {
Xapian::Query q1("word");
Xapian::Query q2("word");
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(q1);
Xapian::MSet mset1 = enquire.get_mset(0, 10);
enquire.set_query(q2);
Xapian::MSet mset2 = enquire.get_mset(0, 2);
// Check the weights
// TEST(mset1.begin().get_weight() < mset2.begin().get_weight());
TEST(mset1.begin().get_weight() == mset2.begin().get_weight());
}
// tests that opening a non-existent termlist throws the correct exception
DEFINE_TESTCASE(termlist1, backend) {
Xapian::Database db(get_database("apitest_onedoc"));
TEST_EXCEPTION(Xapian::InvalidArgumentError,
Xapian::TermIterator t = db.termlist_begin(0));
TEST_EXCEPTION(Xapian::DocNotFoundError,
Xapian::TermIterator t = db.termlist_begin(2));
/* Cause the database to be used properly, showing up problems
* with the link being in a bad state. CME */
Xapian::TermIterator temp = db.termlist_begin(1);
TEST_EXCEPTION(Xapian::DocNotFoundError,
Xapian::TermIterator t = db.termlist_begin(999999999));
}
// tests that a Xapian::TermIterator works as an STL iterator
DEFINE_TESTCASE(termlist2, backend) {
Xapian::Database db(get_database("apitest_onedoc"));
Xapian::TermIterator t = db.termlist_begin(1);
Xapian::TermIterator tend = db.termlist_end(1);
// test operator= creates a copy which compares equal
Xapian::TermIterator t_copy = t;
TEST_EQUAL(t, t_copy);
// test copy constructor creates a copy which compares equal
Xapian::TermIterator t_clone(t);
TEST_EQUAL(t, t_clone);
vector<string> v(t, tend);
t = db.termlist_begin(1);
tend = db.termlist_end(1);
vector<string>::const_iterator i;
for (i = v.begin(); i != v.end(); ++i) {
TEST_NOT_EQUAL(t, tend);
TEST_EQUAL(*i, *t);
t++;
}
TEST_EQUAL(t, tend);
}
static Xapian::TermIterator
test_termlist3_helper()
{
Xapian::Database db(get_database("apitest_onedoc"));
return db.termlist_begin(1);
}
// tests that a Xapian::TermIterator still works when the DB is deleted
DEFINE_TESTCASE(termlist3, backend) {
Xapian::TermIterator u = test_termlist3_helper();
Xapian::Database db(get_database("apitest_onedoc"));
Xapian::TermIterator t = db.termlist_begin(1);
Xapian::TermIterator tend = db.termlist_end(1);
while (t != tend) {
TEST_EQUAL(*t, *u);
t++;
u++;
}
}
// tests skip_to
DEFINE_TESTCASE(termlist4, backend) {
Xapian::Database db(get_database("apitest_onedoc"));
Xapian::TermIterator i = db.termlist_begin(1);
i.skip_to("");
i.skip_to("\xff");
}
// tests punctuation is OK in terms (particularly in remote queries)
DEFINE_TESTCASE(puncterms1, backend) {
Xapian::Database db(get_database("apitest_punc"));
Xapian::Enquire enquire(db);
Xapian::Query q1("semi;colon");
enquire.set_query(q1);
Xapian::MSet m1 = enquire.get_mset(0, 10);
Xapian::Query q2("col:on");
enquire.set_query(q2);
Xapian::MSet m2 = enquire.get_mset(0, 10);
Xapian::Query q3("com,ma");
enquire.set_query(q3);
Xapian::MSet m3 = enquire.get_mset(0, 10);
}
// test that searching for a term with a space or backslash in it works
DEFINE_TESTCASE(spaceterms1, backend) {
Xapian::Enquire enquire(get_database("apitest_space"));
Xapian::MSet mymset;
Xapian::doccount count;
Xapian::MSetIterator m;
Xapian::Stem stemmer("english");
enquire.set_query(stemmer("space man"));
mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 1);
count = 0;
for (m = mymset.begin(); m != mymset.end(); ++m) ++count;
TEST_EQUAL(count, 1);
for (Xapian::valueno value_no = 1; value_no < 7; ++value_no) {
TEST_NOT_EQUAL(mymset.begin().get_document().get_data(), "");
TEST_NOT_EQUAL(mymset.begin().get_document().get_value(value_no), "");
}
enquire.set_query(stemmer("tab\tby"));
mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 1);
count = 0;
for (m = mymset.begin(); m != mymset.end(); ++m) ++count;
TEST_EQUAL(count, 1);
for (Xapian::valueno value_no = 0; value_no < 7; ++value_no) {
string value = mymset.begin().get_document().get_value(value_no);
TEST_NOT_EQUAL(value, "");
if (value_no == 0) {
TEST(value.size() > 262);
TEST_EQUAL(static_cast<unsigned char>(value[262]), 255);
}
}
enquire.set_query(stemmer("back\\slash"));
mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 1);
count = 0;
for (m = mymset.begin(); m != mymset.end(); ++m) ++count;
TEST_EQUAL(count, 1);
}
// test that XOR queries work
DEFINE_TESTCASE(xor1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
Xapian::Stem stemmer("english");
vector<string> terms;
terms.push_back(stemmer("this"));
terms.push_back(stemmer("word"));
terms.push_back(stemmer("of"));
Xapian::Query query(Xapian::Query::OP_XOR, terms.begin(), terms.end());
enquire.set_weighting_scheme(Xapian::BoolWeight());
enquire.set_query(query);
Xapian::MSet mymset = enquire.get_mset(0, 10);
// Docid this word of Match?
// 1 * *
// 2 * * * *
// 3 * *
// 4 * *
// 5 * *
// 6 * *
mset_expect_order(mymset, 1, 2, 5, 6);
}
/// Test that weighted XOR queries work (bug fixed in 1.2.1 and 1.0.21).
DEFINE_TESTCASE(xor2, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
Xapian::Stem stemmer("english");
vector<string> terms;
terms.push_back(stemmer("this"));
terms.push_back(stemmer("word"));
terms.push_back(stemmer("of"));
Xapian::Query query(Xapian::Query::OP_XOR, terms.begin(), terms.end());
enquire.set_query(query);
Xapian::MSet mymset = enquire.get_mset(0, 10);
// Docid LEN this word of Match?
// 1 28 2 *
// 2 81 5 8 1 *
// 3 15 1 2
// 4 31 1 1
// 5 15 1 *
// 6 15 1 *
mset_expect_order(mymset, 2, 1, 5, 6);
}
// test Xapian::Database::get_document()
DEFINE_TESTCASE(getdoc1, backend) {
Xapian::Database db(get_database("apitest_onedoc"));
Xapian::Document doc(db.get_document(1));
TEST_EXCEPTION(Xapian::InvalidArgumentError, db.get_document(0));
TEST_EXCEPTION(Xapian::DocNotFoundError, db.get_document(999999999));
TEST_EXCEPTION(Xapian::DocNotFoundError, db.get_document(123456789));
TEST_EXCEPTION(Xapian::DocNotFoundError, db.get_document(3));
TEST_EXCEPTION(Xapian::DocNotFoundError, db.get_document(2));
// Check that Document works as a handle on modification
// (this was broken for the first try at Xapian::Document prior to 0.7).
Xapian::Document doc2 = doc;
doc.set_data("modified!");
TEST_EQUAL(doc.get_data(), "modified!");
TEST_EQUAL(doc.get_data(), doc2.get_data());
}
// test whether operators with no elements work as a null query
DEFINE_TESTCASE(emptyop1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
vector<Xapian::Query> nullvec;
Xapian::Query query1(Xapian::Query::OP_XOR, nullvec.begin(), nullvec.end());
enquire.set_query(query1);
Xapian::MSet mymset = enquire.get_mset(0, 10);
TEST_MSET_SIZE(mymset, 0);
// In Xapian < 1.3.0, this gave InvalidArgumentError (because
// query1.empty()) but elsewhere we treat an empty query as just not
// matching any documents, so we now do the same here too.
TEST_EQUAL(enquire.get_matching_terms_begin(1),
enquire.get_matching_terms_end(1));
}
// Regression test for check_at_least SEGV when there are no matches.
DEFINE_TESTCASE(checkatleast1, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("thom"));
Xapian::MSet mymset = enquire.get_mset(0, 10, 11);
TEST_EQUAL(0, mymset.size());
}
// Regression test - if check_at_least was set we returned (check_at_least - 1)
// results, rather than the requested msize. Fixed in 1.0.2.
DEFINE_TESTCASE(checkatleast2, backend) {
Xapian::Enquire enquire(get_database("apitest_simpledata"));
enquire.set_query(Xapian::Query("paragraph"));
Xapian::MSet mymset = enquire.get_mset(0, 3, 10);
TEST_MSET_SIZE(mymset, 3);
TEST_EQUAL(mymset.get_matches_lower_bound(), 5);
TEST_EQUAL(mymset.get_uncollapsed_matches_lower_bound(), 5);
mymset = enquire.get_mset(0, 2, 4);
TEST_MSET_SIZE(mymset, 2);
TEST_REL(mymset.get_matches_lower_bound(),>=,4);
TEST_REL(mymset.get_matches_lower_bound(),>=,4);
TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),>=,4);
TEST_REL(mymset.get_uncollapsed_matches_lower_bound(),>=,4);
}
// Feature tests - check_at_least with various sorting options.
DEFINE_TESTCASE(checkatleast3, backend) {
Xapian::Enquire enquire(get_database("etext"));
enquire.set_query(Xapian::Query("prussian")); // 60 matches.
for (int order = 0; order < 3; ++order) {
switch (order) {
case 0:
enquire.set_docid_order(Xapian::Enquire::ASCENDING);
break;
case 1:
enquire.set_docid_order(Xapian::Enquire::DESCENDING);
break;
case 2:
enquire.set_docid_order(Xapian::Enquire::DONT_CARE);
break;
}
for (int sort = 0; sort < 7; ++sort) {
bool reverse = (sort & 1);
switch (sort) {
case 0:
enquire.set_sort_by_relevance();
break;
case 1: case 2:
enquire.set_sort_by_value(0, reverse);
break;
case 3: case 4:
enquire.set_sort_by_value_then_relevance(0, reverse);
break;
case 5: case 6:
enquire.set_sort_by_relevance_then_value(0, reverse);
break;
}
Xapian::MSet mset = enquire.get_mset(0, 100, 500);
TEST_MSET_SIZE(mset, 60);
TEST_EQUAL(mset.get_matches_lower_bound(), 60);
TEST_EQUAL(mset.get_matches_estimated(), 60);
TEST_EQUAL(mset.get_matches_upper_bound(), 60);
TEST_EQUAL(mset.get_uncollapsed_matches_lower_bound(), 60);
TEST_EQUAL(mset.get_uncollapsed_matches_estimated(), 60);
TEST_EQUAL(mset.get_uncollapsed_matches_upper_bound(), 60);
mset = enquire.get_mset(0, 50, 100);
TEST_MSET_SIZE(mset, 50);
TEST_EQUAL(mset.get_matches_lower_bound(), 60);
TEST_EQUAL(mset.get_matches_estimated(), 60);
TEST_EQUAL(mset.get_matches_upper_bound(), 60);
TEST_EQUAL(mset.get_uncollapsed_matches_lower_bound(), 60);
TEST_EQUAL(mset.get_uncollapsed_matches_estimated(), 60);
TEST_EQUAL(mset.get_uncollapsed_matches_upper_bound(), 60);
mset = enquire.get_mset(0, 10, 50);
TEST_MSET_SIZE(mset, 10);
TEST_REL(mset.get_matches_lower_bound(),>=,50);
TEST_REL(mset.get_uncollapsed_matches_lower_bound(),>=,50);
}
}
}
// tests all document postlists
DEFINE_TESTCASE(allpostlist1, backend) {
Xapian::Database db(get_database("apitest_manydocs"));
Xapian::PostingIterator i = db.postlist_begin("");
unsigned int j = 1;
while (i != db.postlist_end("")) {
TEST_EQUAL(*i, j);
i++;
j++;
}
TEST_EQUAL(j, 513);
i = db.postlist_begin("");
j = 1;
while (i != db.postlist_end("")) {
TEST_EQUAL(*i, j);
i++;
j++;
if (j == 50) {
j += 10;
i.skip_to(j);
}
}
TEST_EQUAL(j, 513);
}
static void test_emptyterm1_helper(Xapian::Database & db)
{
// Don't bother with postlist_begin() because allpostlist tests cover that.
TEST_EXCEPTION(Xapian::InvalidArgumentError, db.positionlist_begin(1, ""));
TEST_EQUAL(db.get_doccount(), db.get_termfreq(""));
TEST_EQUAL(db.get_doccount() != 0, db.term_exists(""));
TEST_EQUAL(db.get_doccount(), db.get_collection_freq(""));
}
// tests results of passing an empty term to various methods
DEFINE_TESTCASE(emptyterm1, backend) {
Xapian::Database db(get_database("apitest_manydocs"));
TEST_EQUAL(db.get_doccount(), 512);
test_emptyterm1_helper(db);
db = get_database("apitest_onedoc");
TEST_EQUAL(db.get_doccount(), 1);
test_emptyterm1_helper(db);
db = get_database("");
TEST_EQUAL(db.get_doccount(), 0);
test_emptyterm1_helper(db);
}
// Test for alldocs postlist with a sparse database.
DEFINE_TESTCASE(alldocspl1, backend) {
Xapian::Database db = get_database("alldocspl1",
[](Xapian::WritableDatabase& wdb,
const string&) {
Xapian::Document doc;
doc.set_data("5");
doc.add_value(0, "5");
wdb.replace_document(5, doc);
});
Xapian::PostingIterator i = db.postlist_begin("");
TEST(i != db.postlist_end(""));
TEST_EQUAL(*i, 5);
TEST_EQUAL(i.get_doclength(), 0);
TEST_EQUAL(i.get_unique_terms(), 0);
TEST_EQUAL(i.get_wdf(), 1);
++i;
TEST(i == db.postlist_end(""));
}
// Test reading and writing a modified alldocspostlist.
DEFINE_TESTCASE(alldocspl2, writable) {
Xapian::PostingIterator i, end;
{
Xapian::WritableDatabase db = get_writable_database();
Xapian::Document doc;
doc.set_data("5");
doc.add_value(0, "5");
db.replace_document(5, doc);
// Test iterating before committing the changes.
i = db.postlist_begin("");
end = db.postlist_end("");
TEST(i != end);
TEST_EQUAL(*i, 5);
TEST_EQUAL(i.get_doclength(), 0);
TEST_EQUAL(i.get_unique_terms(), 0);
TEST_EQUAL(i.get_wdf(), 1);
++i;
TEST(i == end);
db.commit();
// Test iterating after committing the changes.
i = db.postlist_begin("");
end = db.postlist_end("");
TEST(i != end);
TEST_EQUAL(*i, 5);
TEST_EQUAL(i.get_doclength(), 0);
TEST_EQUAL(i.get_unique_terms(), 0);
TEST_EQUAL(i.get_wdf(), 1);
++i;
TEST(i == end);
// Add another document.
doc = Xapian::Document();
doc.set_data("5");
doc.add_value(0, "7");
db.replace_document(7, doc);
// Test iterating through before committing the changes.
i = db.postlist_begin("");
end = db.postlist_end("");
TEST(i != end);
TEST_EQUAL(*i, 5);
TEST_EQUAL(i.get_doclength(), 0);
TEST_EQUAL(i.get_unique_terms(), 0);
TEST_EQUAL(i.get_wdf(), 1);
++i;
TEST(i != end);
TEST_EQUAL(*i, 7);
TEST_EQUAL(i.get_doclength(), 0);
TEST_EQUAL(i.get_unique_terms(), 0);
TEST_EQUAL(i.get_wdf(), 1);
++i;
TEST(i == end);
// Delete the first document.
db.delete_document(5);
// Test iterating through before committing the changes.
i = db.postlist_begin("");
end = db.postlist_end("");
TEST(i != end);
TEST_EQUAL(*i, 7);
TEST_EQUAL(i.get_doclength(), 0);
TEST_EQUAL(i.get_unique_terms(), 0);
TEST_EQUAL(i.get_wdf(), 1);
++i;
TEST(i == end);
// Test iterating through after committing the changes, and dropping the
// reference to the main DB.
db.commit();
i = db.postlist_begin("");
end = db.postlist_end("");
}
TEST(i != end);
TEST_EQUAL(*i, 7);
TEST_EQUAL(i.get_doclength(), 0);
TEST_EQUAL(i.get_unique_terms(), 0);
TEST_EQUAL(i.get_wdf(), 1);
++i;
TEST(i == end);
}
// Feature test for Query::OP_SCALE_WEIGHT.
DEFINE_TESTCASE(scaleweight1, backend) {
Xapian::Database db(get_database("apitest_phrase"));
Xapian::Enquire enq(db);
Xapian::QueryParser qp;
static const char * const queries[] = {
"pad",
"milk fridge",
"leave milk on fridge",
"ordered milk operator",
"ordered phrase operator",
"leave \"milk on fridge\"",
"notpresent",
"leave \"milk notpresent\"",
};
static const double multipliers[] = {
-1000000, -2.5, -1, -0.5, 0, 0.5, 1, 2.5, 1000000,
0, 0
};
for (auto qstr : queries) {
tout.str(string());
Xapian::Query query1 = qp.parse_query(qstr);
tout << "query1: " << query1.get_description() << '\n';
for (const double *multp = multipliers; multp[0] != multp[1]; ++multp) {
double mult = *multp;
if (mult < 0) {
TEST_EXCEPTION(Xapian::InvalidArgumentError,
Xapian::Query(Xapian::Query::OP_SCALE_WEIGHT,
query1, mult));
continue;
}
Xapian::Query query2(Xapian::Query::OP_SCALE_WEIGHT, query1, mult);
tout << "query2: " << query2.get_description() << '\n';
enq.set_query(query1);
Xapian::MSet mset1 = enq.get_mset(0, 20);
enq.set_query(query2);
Xapian::MSet mset2 = enq.get_mset(0, 20);
TEST_EQUAL(mset1.size(), mset2.size());
Xapian::MSetIterator i1, i2;
if (mult > 0) {
for (i1 = mset1.begin(), i2 = mset2.begin();
i1 != mset1.end() && i2 != mset2.end(); ++i1, ++i2) {
TEST_EQUAL_DOUBLE(i1.get_weight() * mult, i2.get_weight());
TEST_EQUAL(*i1, *i2);
}
} else {
// Weights in mset2 are 0; so it should be sorted by docid.
vector<Xapian::docid> ids1;
vector<Xapian::docid> ids2;
for (i1 = mset1.begin(), i2 = mset2.begin();
i1 != mset1.end() && i2 != mset2.end(); ++i1, ++i2) {
TEST_NOT_EQUAL_DOUBLE(i1.get_weight(), 0);
TEST_EQUAL_DOUBLE(i2.get_weight(), 0);
ids1.push_back(*i1);
ids2.push_back(*i2);
}
sort(ids1.begin(), ids1.end());
TEST_EQUAL(ids1, ids2);
}
}
}
}
// Test Query::OP_SCALE_WEIGHT being used to multiply some of the weights of a
// search by zero.
DEFINE_TESTCASE(scaleweight2, backend) {
Xapian::Database db(get_database("apitest_phrase"));
Xapian::Enquire enq(db);
Xapian::MSetIterator i;
Xapian::Query query1("fridg");
Xapian::Query query2(Xapian::Query::OP_SCALE_WEIGHT, query1, 2.5);
Xapian::Query query3("milk");
Xapian::Query query4(Xapian::Query::OP_SCALE_WEIGHT, query3, 0);
Xapian::Query query5(Xapian::Query::OP_OR, query2, query4);
// query5 should first return the same results as query1, in the same
// order, and then return the results of query3 which aren't also results
// of query1, in ascending docid order. We test that this happens.
// First, build a vector of docids matching the first part of the query,
// and append the non-duplicate docids matching the second part of the
// query.
vector<Xapian::docid> ids1;
set<Xapian::docid> idsin1;
vector<Xapian::docid> ids3;
enq.set_query(query1);
Xapian::MSet mset1 = enq.get_mset(0, 20);
enq.set_query(query3);
Xapian::MSet mset3 = enq.get_mset(0, 20);
TEST_NOT_EQUAL(mset1.size(), 0);
for (i = mset1.begin(); i != mset1.end(); ++i) {
ids1.push_back(*i);
idsin1.insert(*i);
}
TEST_NOT_EQUAL(mset3.size(), 0);
for (i = mset3.begin(); i != mset3.end(); ++i) {
if (idsin1.find(*i) != idsin1.end())
continue;
ids3.push_back(*i);
}
sort(ids3.begin(), ids3.end());
ids1.insert(ids1.end(), ids3.begin(), ids3.end());
// Now, run the combined query and build a vector of the matching docids.
vector<Xapian::docid> ids5;
enq.set_query(query5);
Xapian::MSet mset5 = enq.get_mset(0, 20);
for (i = mset5.begin(); i != mset5.end(); ++i) {
ids5.push_back(*i);
}
TEST_EQUAL(ids1, ids5);
}
// Feature test for Database::get_uuid().
DEFINE_TESTCASE(uuid1, backend && !multi) {
SKIP_TEST_FOR_BACKEND("inmemory");
Xapian::Database db = get_database("apitest_simpledata");
string uuid1 = db.get_uuid();
TEST_EQUAL(uuid1.size(), 36);
// A database with no sub-databases has an empty UUID.
Xapian::Database db2;
TEST(db2.get_uuid().empty());
db2.add_database(db);
TEST_EQUAL(uuid1, db2.get_uuid());
// Multi-database has multiple UUIDs (we don't define the format exactly
// so this assumes something about the implementation).
db2.add_database(db);
TEST_EQUAL(uuid1 + ":" + uuid1, db2.get_uuid());
#ifdef XAPIAN_HAS_INMEMORY_BACKEND
// This relies on InMemory databases not supporting uuids.
// A multi-database containing a database with no uuid has no uuid.
db2.add_database(Xapian::Database(string(), Xapian::DB_BACKEND_INMEMORY));
TEST(db2.get_uuid().empty());
#endif
}
|