Skip to content

Commit

Permalink
cherry pick from nebula : full text (guotai junan) (vesoft-inc#2825)
Browse files Browse the repository at this point in the history
* Ft search (vesoft-inc#5567)

* Add optimizer rule for pushing limit into the fulltext index scan (vesoft-inc#5575)

* Merge limit and full text index scan rule (vesoft-inc#5577)

* Support to return score from es query (vesoft-inc#5580)

* modify ft search (vesoft-inc#5584)

* modify ft search

* fix bug

* address some comment

* fix bug

* fix conflict

---------

Co-authored-by: hs.zhang <22708345+cangfengzhs@users.noreply.github.com>
Co-authored-by: Yee <2520865+yixinglu@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 12, 2023
1 parent a832d57 commit 4dff80e
Show file tree
Hide file tree
Showing 58 changed files with 1,145 additions and 1,608 deletions.
2 changes: 1 addition & 1 deletion src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1370,7 +1370,7 @@ Status MetaClient::handleResponse(const RESP& resp) {
case nebula::cpp2::ErrorCode::E_HOST_CAN_NOT_BE_ADDED:
return Status::Error("Could not add a host, which is not a storage and not expired either");
default:
return Status::Error("Unknown error!");
return Status::Error("Unknown error %d!", static_cast<int>(resp.get_code()));
}
}

Expand Down
18 changes: 3 additions & 15 deletions src/common/expression/Expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -512,10 +512,7 @@ Expression* Expression::decode(ObjectPool* pool, Expression::Decoder& decoder) {
exp->resetFrom(decoder);
return exp;
}
case Expression::Kind::kTSPrefix:
case Expression::Kind::kTSWildcard:
case Expression::Kind::kTSRegexp:
case Expression::Kind::kTSFuzzy: {
case Expression::Kind::kESQUERY: {
LOG(FATAL) << "Should not decode text search expression";
return exp;
}
Expand Down Expand Up @@ -721,17 +718,8 @@ std::ostream& operator<<(std::ostream& os, Expression::Kind kind) {
case Expression::Kind::kPathBuild:
os << "PathBuild";
break;
case Expression::Kind::kTSPrefix:
os << "Prefix";
break;
case Expression::Kind::kTSWildcard:
os << "Wildcard";
break;
case Expression::Kind::kTSRegexp:
os << "Regexp";
break;
case Expression::Kind::kTSFuzzy:
os << "Fuzzy";
case Expression::Kind::kESQUERY:
os << "ESQuery";
break;
case Expression::Kind::kListComprehension:
os << "ListComprehension";
Expand Down
5 changes: 1 addition & 4 deletions src/common/expression/Expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,7 @@ class Expression {

kPathBuild,
// text or key word search expression
kTSPrefix,
kTSWildcard,
kTSRegexp,
kTSFuzzy,
kESQUERY,

kAggregate,
kIsNull,
Expand Down
37 changes: 5 additions & 32 deletions src/common/expression/TextSearchExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,14 @@
namespace nebula {

bool TextSearchArgument::operator==(const TextSearchArgument& rhs) const {
return val_ == rhs.val_ && op_ == rhs.op_ && fuzziness_ == rhs.fuzziness_ &&
limit_ == rhs.limit_ && timeout_ == rhs.timeout_;
return index_ == rhs.index_ && query_ == rhs.query_;
}

std::string TextSearchArgument::toString() const {
std::string buf;
buf.reserve(64);
buf = from_ + "." + prop_ + ", ";
buf += "\"" + val_ + "\"";
if (fuzziness_ == -1) {
buf += ", AUTO, ";
buf += ((op_ == "or") ? "OR" : "AND");
} else if (fuzziness_ > -1) {
buf += ", ";
buf += folly::stringPrintf("%d, ", fuzziness_);
buf += ((op_ == "or") ? "OR" : "AND");
}
if (limit_ != -1) {
buf += folly::stringPrintf(", %d", limit_);
}
if (timeout_ != -1) {
buf += folly::stringPrintf(", %d", timeout_);
}
buf += index_ + ", ";
buf += "\"" + query_ + "\"";
return buf;
}

Expand All @@ -49,20 +34,8 @@ std::string TextSearchExpression::toString() const {
std::string buf;
buf.reserve(64);
switch (kind_) {
case Kind::kTSWildcard: {
buf = "WILDCARD(";
break;
}
case Kind::kTSPrefix: {
buf = "PREFIX(";
break;
}
case Kind::kTSFuzzy: {
buf = "FUZZY(";
break;
}
case Kind::kTSRegexp: {
buf = "REGEXP(";
case Kind::kESQUERY: {
buf = "ES_QUERY(";
break;
}
default: {
Expand Down
86 changes: 14 additions & 72 deletions src/common/expression/TextSearchExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,60 +14,19 @@ namespace nebula {
class TextSearchArgument final {
public:
static TextSearchArgument* make(ObjectPool* pool,
const std::string& from,
const std::string& prop,
const std::string& val) {
return pool->makeAndAdd<TextSearchArgument>(from, prop, val);
const std::string& index,
const std::string& query) {
return pool->makeAndAdd<TextSearchArgument>(index, query);
}

~TextSearchArgument() = default;

void setVal(const std::string& val) {
val_ = val;
std::string& index() {
return index_;
}

const std::string& from() {
return from_;
}

const std::string& prop() {
return prop_;
}

const std::string& val() const {
return val_;
}

void setOP(const std::string& op) {
op_ = op;
}

const std::string& op() const {
return op_;
}

void setFuzziness(int32_t fuzz) {
fuzziness_ = fuzz;
}

int32_t fuzziness() {
return fuzziness_;
}

void setLimit(int32_t limit) {
limit_ = limit;
}

int32_t limit() {
return limit_;
}

void setTimeout(int32_t timeout) {
timeout_ = timeout;
}

int32_t timeout() {
return timeout_;
std::string& query() {
return query_;
}

bool operator==(const TextSearchArgument& rhs) const;
Expand All @@ -76,35 +35,18 @@ class TextSearchArgument final {

private:
friend ObjectPool;
TextSearchArgument(const std::string& from, const std::string& prop, const std::string& val)
: from_(from), prop_(prop), val_(val) {}
TextSearchArgument(const std::string& index, const std::string& query)
: index_(index), query_(query) {}

private:
std::string from_;
std::string prop_;
std::string val_;
std::string op_;
int32_t fuzziness_{-2};
int32_t limit_{10000};
int32_t timeout_{-1};
std::string index_;
std::string query_;
};

class TextSearchExpression : public Expression {
public:
static TextSearchExpression* makePrefix(ObjectPool* pool, TextSearchArgument* arg) {
return pool->makeAndAdd<TextSearchExpression>(pool, Kind::kTSPrefix, arg);
}

static TextSearchExpression* makeWildcard(ObjectPool* pool, TextSearchArgument* arg) {
return pool->makeAndAdd<TextSearchExpression>(pool, Kind::kTSWildcard, arg);
}

static TextSearchExpression* makeRegexp(ObjectPool* pool, TextSearchArgument* arg) {
return pool->makeAndAdd<TextSearchExpression>(pool, Kind::kTSRegexp, arg);
}

static TextSearchExpression* makeFuzzy(ObjectPool* pool, TextSearchArgument* arg) {
return pool->makeAndAdd<TextSearchExpression>(pool, Kind::kTSFuzzy, arg);
static TextSearchExpression* makeQuery(ObjectPool* pool, TextSearchArgument* arg) {
return pool->makeAndAdd<TextSearchExpression>(pool, Kind::kESQUERY, arg);
}

static TextSearchExpression* make(ObjectPool* pool, Kind kind, TextSearchArgument* arg) {
Expand All @@ -125,7 +67,7 @@ class TextSearchExpression : public Expression {
std::string toString() const override;

Expression* clone() const override {
auto arg = TextSearchArgument::make(pool_, arg_->from(), arg_->prop(), arg_->val());
auto arg = TextSearchArgument::make(pool_, arg_->index(), arg_->query());
return TextSearchExpression::make(pool_, kind_, arg);
}

Expand Down
12 changes: 12 additions & 0 deletions src/common/function/FunctionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,7 @@ std::unordered_map<std::string, std::vector<TypeSignature>> FunctionManager::typ
{"json_extract",
{TypeSignature({Value::Type::STRING}, Value::Type::MAP),
TypeSignature({Value::Type::STRING}, Value::Type::NULLVALUE)}},
{"score", {TypeSignature({}, Value::Type::__EMPTY__)}},
};

// static
Expand Down Expand Up @@ -2937,6 +2938,17 @@ FunctionManager::FunctionManager() {
return Value::kNullValue;
};
}
// Score function is used to identify the score of a full-text search
{
auto &attr = functions_["score"];
attr.minArity_ = 0;
attr.maxArity_ = 0;
attr.isAlwaysPure_ = true;
attr.body_ = [](const auto &) -> Value {
// Only placeholder, will be replaced by actual expression and need not to be evaluated
return Value::kNullValue;
};
}
} // NOLINT

// static
Expand Down
Loading

0 comments on commit 4dff80e

Please sign in to comment.