fix: review comments

This commit is contained in:
Volodymyr Yavdoshenko 2025-05-09 18:57:55 +03:00
parent 4000cef7c4
commit 7f1298bf3f
No known key found for this signature in database
GPG key ID: 24BC74845F4F4064
5 changed files with 38 additions and 49 deletions

View file

@ -16,18 +16,6 @@ using namespace std;
namespace dfly::search {
AstTermNode::AstTermNode(string term) : term{std::move(term)} {
}
AstPrefixNode::AstPrefixNode(string prefix) : prefix{std::move(prefix)} {
}
AstSuffixNode::AstSuffixNode(string suffix) : suffix{std::move(suffix)} {
}
AstInfixNode::AstInfixNode(string infix) : infix{std::move(infix)} {
}
AstRangeNode::AstRangeNode(double lo, bool lo_excl, double hi, bool hi_excl)
: lo{lo_excl ? nextafter(lo, hi) : lo}, hi{hi_excl ? nextafter(hi, lo) : hi} {
}

View file

@ -12,6 +12,7 @@
#include <vector>
#include "core/search/base.h"
#include "core/search/tag_types.h"
namespace dfly {
@ -25,30 +26,17 @@ struct AstStarNode {};
// Matches all documents where this field has a non-null value
struct AstStarFieldNode {};
// Matches terms in text fields
struct AstTermNode {
explicit AstTermNode(std::string term);
template <TagType T> struct AstAffixNode {
explicit AstAffixNode(std::string affix) : affix{std::move(affix)} {
}
std::string term;
std::string affix;
};
struct AstPrefixNode {
explicit AstPrefixNode(std::string prefix);
std::string prefix;
};
struct AstSuffixNode {
explicit AstSuffixNode(std::string suffix);
std::string suffix;
};
struct AstInfixNode {
explicit AstInfixNode(std::string infix);
std::string infix;
};
using AstTermNode = AstAffixNode<TagType::REGULAR>;
using AstPrefixNode = AstAffixNode<TagType::PREFIX>;
using AstSuffixNode = AstAffixNode<TagType::SUFFIX>;
using AstInfixNode = AstAffixNode<TagType::INFIX>;
// Matches numeric range
struct AstRangeNode {

View file

@ -1,6 +1,7 @@
%top{
// Our lexer need to know about Parser::symbol_type
#include "core/search/parser.hh"
#include "core/search/tag_types.h" // Include TagType enum
}
%{
@ -25,8 +26,7 @@
// A number symbol corresponding to the value in S.
using dfly::search::Parser;
using namespace std;
enum class TagType { PREFIX, SUFFIX, INFIX, REGULAR };
using dfly::search::TagType;
Parser::symbol_type make_StringLit(string_view src, const Parser::location_type& loc);
Parser::symbol_type make_Tag(string_view src, TagType type, const Parser::location_type& loc);

View file

@ -118,16 +118,16 @@ struct ProfileBuilder {
string GetNodeInfo(const AstNode& node) {
struct NodeFormatter {
void operator()(std::string* out, const AstPrefixNode& node) const {
out->append(node.prefix);
out->append(node.affix);
}
void operator()(std::string* out, const AstSuffixNode& node) const {
out->append(node.suffix);
out->append(node.affix);
}
void operator()(std::string* out, const AstInfixNode& node) const {
out->append(node.infix);
out->append(node.affix);
}
void operator()(std::string* out, const AstTermNode& node) const {
out->append(node.term);
out->append(node.affix);
}
void operator()(std::string* out, const AstTagsNode::TagValue& value) const {
visit([this, out](const auto& n) { this->operator()(out, n); }, value);
@ -135,10 +135,10 @@ struct ProfileBuilder {
};
Overloaded node_info{
[](monostate) -> string { return ""s; },
[](const AstTermNode& n) { return absl::StrCat("Term{", n.term, "}"); },
[](const AstPrefixNode& n) { return absl::StrCat("Prefix{", n.prefix, "}"); },
[](const AstSuffixNode& n) { return absl::StrCat("Suffix{", n.suffix, "}"); },
[](const AstInfixNode& n) { return absl::StrCat("Infix{", n.infix, "}"); },
[](const AstTermNode& n) { return absl::StrCat("Term{", n.affix, "}"); },
[](const AstPrefixNode& n) { return absl::StrCat("Prefix{", n.affix, "}"); },
[](const AstSuffixNode& n) { return absl::StrCat("Suffix{", n.affix, "}"); },
[](const AstInfixNode& n) { return absl::StrCat("Infix{", n.affix, "}"); },
[](const AstRangeNode& n) { return absl::StrCat("Range{", n.lo, "<>", n.hi, "}"); },
[](const AstLogicalNode& n) {
auto op = n.op == AstLogicalNode::AND ? "and" : "or";
@ -301,7 +301,7 @@ struct BasicSearch {
// "term": access field's text index or unify results from all text indices if no field is set
IndexResult Search(const AstTermNode& node, string_view active_field) {
std::string term = node.term;
std::string term = node.affix;
bool strip_whitespace = true;
if (auto synonyms = indices_->GetSynonyms(); synonyms) {
@ -363,7 +363,7 @@ struct BasicSearch {
}
auto mapping = [&node, this](TextIndex* index) {
return CollectPrefixMatches(index, node.prefix);
return CollectPrefixMatches(index, node.affix);
};
return UnifyResults(GetSubResults(indices, mapping), LogicOp::OR);
}
@ -422,16 +422,16 @@ struct BasicSearch {
return IndexResult{};
Overloaded ov{[tag_index](const AstTermNode& term) -> IndexResult {
return tag_index->Matching(term.term);
return tag_index->Matching(term.affix);
},
[tag_index, this](const AstPrefixNode& prefix) {
return CollectPrefixMatches(tag_index, prefix.prefix);
return CollectPrefixMatches(tag_index, prefix.affix);
},
[tag_index, this](const AstSuffixNode& suffix) {
return CollectSuffixMatches(tag_index, suffix.suffix);
return CollectSuffixMatches(tag_index, suffix.affix);
},
[tag_index, this](const AstInfixNode& infix) {
return CollectInfixMatches(tag_index, infix.infix);
return CollectInfixMatches(tag_index, infix.affix);
}};
auto mapping = [ov](const auto& tag) { return visit(ov, tag); };
return UnifyResults(GetSubResults(node.tags, mapping), LogicOp::OR);

View file

@ -0,0 +1,13 @@
// Copyright 2023, DragonflyDB authors. All rights reserved.
// See LICENSE for licensing terms.
//
#pragma once
namespace dfly {
namespace search {
enum class TagType { PREFIX, SUFFIX, INFIX, REGULAR };
} // namespace search
} // namespace dfly