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
|
/** @file
* @brief Xapian::Query internals
*/
/* Copyright (C) 2011-2026 Olly Betts
*
* 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, see
* <https://www.gnu.org/licenses/>.
*/
#ifndef XAPIAN_INCLUDED_QUERYINTERNAL_H
#define XAPIAN_INCLUDED_QUERYINTERNAL_H
#include "api/editdistance.h"
#include "queryvector.h"
#include "stringutils.h"
#include "xapian/intrusive_ptr.h"
#include "xapian/query.h"
/// Default set_size for OP_ELITE_SET:
const Xapian::termcount DEFAULT_ELITE_SET_SIZE = 10;
namespace Xapian {
namespace Internal {
class PostList;
struct PostListAndEstimate;
class QueryOptimiser;
class QueryTerm : public Query::Internal {
std::string term;
Xapian::termcount wqf;
Xapian::termpos pos;
public:
// Construct a "MatchAll" QueryTerm.
QueryTerm() : term(), wqf(1), pos(0) { }
QueryTerm(std::string_view term_,
Xapian::termcount wqf_,
Xapian::termpos pos_)
: term(term_), wqf(wqf_), pos(pos_) { }
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
const std::string & get_term() const { return term; }
termcount get_wqf() const { return wqf; }
termpos get_pos() const { return pos; }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
bool postlist_sub_and_like(AndContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
termcount get_length() const noexcept XAPIAN_PURE_FUNCTION {
return wqf;
}
void serialise(std::string & result) const;
std::string get_description() const;
void gather_terms(void * void_terms) const;
};
class QueryPostingSource : public Query::Internal {
Xapian::Internal::opt_intrusive_ptr<PostingSource> source;
public:
explicit QueryPostingSource(PostingSource * source_);
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void serialise(std::string & result) const;
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
std::string get_description() const;
};
class QueryScaleWeight : public Query::Internal {
double scale_factor;
Query subquery;
public:
QueryScaleWeight(double factor, const Query & subquery_);
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
bool postlist_sub_and_like(AndContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
termcount get_length() const noexcept XAPIAN_PURE_FUNCTION {
return subquery.internal->get_length();
}
void serialise(std::string & result) const;
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
size_t get_num_subqueries() const noexcept XAPIAN_PURE_FUNCTION;
const Query get_subquery(size_t n) const;
std::string get_description() const;
void gather_terms(void * void_terms) const;
};
class QueryValueBase : public Query::Internal {
protected:
Xapian::valueno slot;
public:
explicit QueryValueBase(Xapian::valueno slot_)
: slot(slot_) { }
Xapian::valueno get_slot() const { return slot; }
};
class QueryValueRange : public QueryValueBase {
std::string begin, end;
public:
QueryValueRange(Xapian::valueno slot_,
std::string_view begin_,
std::string_view end_)
: QueryValueBase(slot_), begin(begin_), end(end_) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void serialise(std::string & result) const;
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
std::string get_description() const;
};
class QueryValueLE : public QueryValueBase {
std::string limit;
public:
QueryValueLE(Xapian::valueno slot_, std::string_view limit_)
: QueryValueBase(slot_), limit(limit_) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void serialise(std::string & result) const;
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
std::string get_description() const;
};
class QueryValueGE : public QueryValueBase {
std::string limit;
public:
QueryValueGE(Xapian::valueno slot_, std::string_view limit_)
: QueryValueBase(slot_), limit(limit_) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void serialise(std::string & result) const;
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
std::string get_description() const;
};
class QueryBranch : public Query::Internal {
virtual Xapian::Query::op get_op() const = 0;
protected:
QueryVector subqueries;
explicit QueryBranch(size_t n_subqueries) : subqueries(n_subqueries) { }
void serialise_(std::string & result, Xapian::termcount parameter = 0) const;
void do_bool_or_like(OrContext& ctx,
QueryOptimiser* qopt,
TermFreqs* termfreqs,
size_t first = 0) const;
/** Process OR-like subqueries.
*
* @param keep_zero_weight By default zero-weight subqueries are kept,
* but in some situations (such as on the right
* side of OP_AND_MAYBE when not under
* OP_SYNONYM) they can be ignored.
*/
void do_or_like(OrContext& ctx, QueryOptimiser* qopt, double factor,
TermFreqs* termfreqs,
Xapian::termcount elite_set_size = 0, size_t first = 0,
bool keep_zero_weight = true) const;
PostListAndEstimate do_synonym(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
PostListAndEstimate do_max(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
const std::string get_description_helper(const char * op,
Xapian::termcount window = 0) const;
public:
termcount get_length() const noexcept XAPIAN_PURE_FUNCTION;
void serialise(std::string & result) const;
void gather_terms(void * void_terms) const;
virtual void add_subquery(const Xapian::Query & subquery) = 0;
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
size_t get_num_subqueries() const noexcept XAPIAN_PURE_FUNCTION;
const Query get_subquery(size_t n) const;
virtual Query::Internal * done() = 0;
};
class QueryAndLike : public QueryBranch {
protected:
explicit QueryAndLike(size_t num_subqueries_)
: QueryBranch(num_subqueries_) { }
public:
void add_subquery(const Xapian::Query & subquery);
Query::Internal * done();
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
bool postlist_sub_and_like(AndContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
};
class QueryOrLike : public QueryBranch {
protected:
explicit QueryOrLike(size_t num_subqueries_)
: QueryBranch(num_subqueries_) { }
public:
void add_subquery(const Xapian::Query & subquery);
Query::Internal * done();
};
class QueryAnd : public QueryAndLike {
Xapian::Query::op get_op() const;
public:
explicit QueryAnd(size_t n_subqueries) : QueryAndLike(n_subqueries) { }
std::string get_description() const;
};
class QueryOr : public QueryOrLike {
Xapian::Query::op get_op() const;
public:
explicit QueryOr(size_t n_subqueries) : QueryOrLike(n_subqueries) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void postlist_sub_or_like(OrContext& ctx, QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs,
bool keep_zero_weight) const;
void postlist_sub_bool_or_like(OrContext& ctx,
QueryOptimiser* qopt,
TermFreqs* termfreqs) const;
std::string get_description() const;
};
class QueryAndNot : public QueryBranch {
Xapian::Query::op get_op() const;
public:
explicit QueryAndNot(size_t n_subqueries) : QueryBranch(n_subqueries) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
bool postlist_sub_and_like(AndContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void add_subquery(const Xapian::Query & subquery);
Query::Internal * done();
std::string get_description() const;
};
class QueryXor : public QueryOrLike {
Xapian::Query::op get_op() const;
public:
explicit QueryXor(size_t n_subqueries) : QueryOrLike(n_subqueries) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void postlist_sub_xor(XorContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
std::string get_description() const;
};
class QueryAndMaybe : public QueryBranch {
Xapian::Query::op get_op() const;
public:
explicit QueryAndMaybe(size_t n_subqueries) : QueryBranch(n_subqueries) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
bool postlist_sub_and_like(AndContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void add_subquery(const Xapian::Query & subquery);
Query::Internal * done();
std::string get_description() const;
};
class QueryFilter : public QueryAndLike {
Xapian::Query::op get_op() const;
public:
explicit QueryFilter(size_t n_subqueries) : QueryAndLike(n_subqueries) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
bool postlist_sub_and_like(AndContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
std::string get_description() const;
};
class QueryWindowed : public QueryAndLike {
protected:
Xapian::termcount window;
QueryWindowed(size_t n_subqueries, Xapian::termcount window_)
: QueryAndLike(n_subqueries), window(window_) { }
bool postlist_windowed(Xapian::Query::op op, AndContext& ctx,
QueryOptimiser* qopt, double factor,
TermFreqs* termfreqs) const;
public:
size_t get_window() const { return window; }
Query::Internal * done();
};
class QueryNear : public QueryWindowed {
Xapian::Query::op get_op() const;
public:
QueryNear(size_t n_subqueries, Xapian::termcount window_)
: QueryWindowed(n_subqueries, window_) { }
void serialise(std::string & result) const;
bool postlist_sub_and_like(AndContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
std::string get_description() const;
};
class QueryPhrase : public QueryWindowed {
Xapian::Query::op get_op() const;
public:
QueryPhrase(size_t n_subqueries, Xapian::termcount window_)
: QueryWindowed(n_subqueries, window_) { }
void serialise(std::string & result) const;
bool postlist_sub_and_like(AndContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
std::string get_description() const;
};
class QueryEliteSet : public QueryOrLike {
Xapian::Query::op get_op() const;
Xapian::termcount set_size;
public:
QueryEliteSet(size_t n_subqueries, Xapian::termcount set_size_)
: QueryOrLike(n_subqueries),
set_size(set_size_ ? set_size_ : DEFAULT_ELITE_SET_SIZE) { }
void serialise(std::string & result) const;
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void postlist_sub_or_like(OrContext& ctx,
QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs,
bool keep_zero_weight) const;
std::string get_description() const;
};
class QuerySynonym : public QueryOrLike {
Xapian::Query::op get_op() const;
public:
explicit QuerySynonym(size_t n_subqueries) : QueryOrLike(n_subqueries) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
Query::Internal * done();
std::string get_description() const;
};
class QueryMax : public QueryOrLike {
Xapian::Query::op get_op() const;
public:
explicit QueryMax(size_t n_subqueries) : QueryOrLike(n_subqueries) { }
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
std::string get_description() const;
};
class QueryWildcard : public Query::Internal {
std::string pattern;
Xapian::termcount max_expansion;
int flags;
Query::op combiner;
/** Fixed head and tail lengths, and min/max length term that can match.
*
* All in bytes.
*/
size_t head = 0, tail = 0, min_len = 0, max_len = 0;
// If candidate.size() >= min_len && candidate.size() < min_check_len then
// we don't need to actually do the pattern check since the length and
// head/tail checks are enough.
//
// This optimises cases `*` or `*?` or `?*` (which covers a lot of common
// cases) and also a pattern where there's no `*` and a single `?` where
// we only need to check the pattern when candidate.size() > min_len.
//
// Note that we can't handle cases like `*??` here since `?` matches a
// single UTF-8 character, which can be more than one byte.
size_t min_check_len = size_t(-1);
std::string prefix, suffix;
bool test_wildcard_(const std::string& candidate, size_t o, size_t p,
size_t i) const;
public:
QueryWildcard(std::string_view pattern_,
Xapian::termcount max_expansion_,
int flags_,
Query::op combiner_);
/// Perform wildcard test on candidate known to match prefix.
bool test_prefix_known(const std::string& candidate) const;
/// Perform full wildcard test on candidate.
bool test(const std::string& candidate) const {
return startswith(candidate, prefix) && test_prefix_known(candidate);
}
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
std::string get_pattern() const { return pattern; }
Xapian::termcount get_max_expansion() const { return max_expansion; }
int get_just_flags() const {
return flags &~ Xapian::Query::WILDCARD_LIMIT_MASK_;
}
int get_max_type() const {
return flags & Xapian::Query::WILDCARD_LIMIT_MASK_;
}
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
termcount get_length() const noexcept XAPIAN_PURE_FUNCTION;
void serialise(std::string & result) const;
/** Change the combining operator.
*
* If there's only one reference to this object we change in-place
* and return a pointer to the existing object; otherwise we create and
* return a new QueryWildcard object.
*/
QueryWildcard* change_combiner(Xapian::Query::op new_op) {
if (_refs == 1) {
combiner = new_op;
return this;
}
return new QueryWildcard(pattern,
max_expansion,
flags,
new_op);
}
/// Return the fixed prefix from the wildcard pattern.
std::string get_fixed_prefix() const { return prefix; }
std::string get_description() const;
};
class QueryEditDistance : public Query::Internal {
std::string pattern;
Xapian::termcount max_expansion;
int flags;
Query::op combiner;
EditDistanceCalculator edcalc;
unsigned edit_distance;
size_t fixed_prefix_len;
public:
QueryEditDistance(std::string_view pattern_,
Xapian::termcount max_expansion_,
int flags_,
Query::op combiner_,
unsigned edit_distance_ = 2,
size_t fixed_prefix_len_ = 0)
: pattern(pattern_),
max_expansion(max_expansion_),
flags(flags_),
combiner(combiner_),
edcalc(pattern),
edit_distance(edit_distance_),
fixed_prefix_len(fixed_prefix_len_) { }
/** Perform edit distance test.
*
* @return edit_distance + 1, or 0 for a non-match.
*/
int test(const std::string& candidate) const;
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
std::string get_pattern() const { return pattern; }
size_t get_fixed_prefix_len() const { return fixed_prefix_len; }
Xapian::termcount get_max_expansion() const { return max_expansion; }
int get_just_flags() const {
return flags &~ Xapian::Query::WILDCARD_LIMIT_MASK_;
}
int get_max_type() const {
return flags & Xapian::Query::WILDCARD_LIMIT_MASK_;
}
unsigned get_threshold() const {
return edit_distance;
}
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
termcount get_length() const noexcept XAPIAN_PURE_FUNCTION;
void serialise(std::string & result) const;
/** Change the combining operator.
*
* If there's only one reference to this object we change in-place
* and return a pointer to the existing object; otherwise we create and
* return a new QueryEditDistance object.
*/
QueryEditDistance* change_combiner(Xapian::Query::op new_op) {
if (_refs == 1) {
combiner = new_op;
return this;
}
return new QueryEditDistance(pattern,
max_expansion,
flags,
new_op,
edit_distance,
fixed_prefix_len);
}
std::string get_description() const;
};
class QueryInvalid : public Query::Internal {
public:
QueryInvalid() { }
Xapian::Query::op get_type() const noexcept XAPIAN_PURE_FUNCTION;
PostListAndEstimate postlist(QueryOptimiser* qopt,
double factor,
TermFreqs* termfreqs) const;
void serialise(std::string & result) const;
std::string get_description() const;
};
}
}
#endif // XAPIAN_INCLUDED_QUERYINTERNAL_H
|