Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modify ft search #5584

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 2 additions & 14 deletions src/common/expression/TextSearchExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,14 @@
namespace nebula {

bool TextSearchArgument::operator==(const TextSearchArgument& rhs) const {
return index_ == rhs.index_ && query_ == rhs.query_ && props_ == rhs.props_;
return index_ == rhs.index_ && query_ == rhs.query_;
}

std::string TextSearchArgument::toString() const {
std::string buf;
buf.reserve(64);
if (!index_.empty()) {
buf += "\"" + index_ + "\", ";
}
buf += index_ + ", ";
buf += "\"" + query_ + "\"";
if (!props_.empty()) {
buf += ", [";
for (size_t i = 0; i < props_.size(); i++) {
buf += props_[i];
if (i != props_.size() - 1) {
buf += ", ";
}
}
buf += "]";
}
return buf;
}

Expand Down
30 changes: 5 additions & 25 deletions src/common/expression/TextSearchExpression.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ class TextSearchArgument final {
public:
static TextSearchArgument* make(ObjectPool* pool,
const std::string& index,
const std::string& query,
const std::vector<std::string>& props) {
return pool->makeAndAdd<TextSearchArgument>(index, query, props);
const std::string& query) {
return pool->makeAndAdd<TextSearchArgument>(index, query);
}

~TextSearchArgument() = default;
Expand All @@ -30,35 +29,18 @@ class TextSearchArgument final {
return query_;
}

std::vector<std::string>& props() {
return props_;
}

int64_t& offset() {
return offset_;
}

int64_t& count() {
return count_;
}

bool operator==(const TextSearchArgument& rhs) const;

std::string toString() const;

private:
friend ObjectPool;
TextSearchArgument(const std::string& index,
const std::string& query,
const std::vector<std::string>& props)
: index_(index), query_(query), props_(props) {}
TextSearchArgument(const std::string& index, const std::string& query)
: index_(index), query_(query) {}

private:
std::string index_;
std::string query_;
std::vector<std::string> props_;
int64_t count_ = 0;
int64_t offset_ = 0;
};

class TextSearchExpression : public Expression {
Expand All @@ -85,9 +67,7 @@ class TextSearchExpression : public Expression {
std::string toString() const override;

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

Expand Down
5 changes: 0 additions & 5 deletions src/common/plugin/fulltext/elasticsearch/ESAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,17 +189,12 @@ Status ESAdapter::bulk(const ESBulk& bulk, bool refresh) {

StatusOr<ESQueryResult> ESAdapter::queryString(const std::string& index,
const std::string& query,
const std::vector<std::string>& fields,
int64_t from,
int64_t size) {
folly::dynamic body = folly::dynamic::object();
body["query"] = folly::dynamic::object();
body["query"]["query_string"] = folly::dynamic::object();
body["query"]["query_string"]["query"] = query;
body["query"]["query_string"]["fields"] = folly::dynamic::array();
for (auto& field : fields) {
body["query"]["query_string"]["fields"].push_back(field);
}
if (size > 0) {
body["size"] = size;
body["from"] = from;
Expand Down
1 change: 0 additions & 1 deletion src/common/plugin/fulltext/elasticsearch/ESAdapter.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ class ESAdapter {

virtual StatusOr<ESQueryResult> queryString(const std::string& index,
const std::string& query,
const std::vector<std::string>& fields,
int64_t from,
int64_t size);

Expand Down
16 changes: 6 additions & 10 deletions src/graph/executor/query/FulltextIndexScanExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,18 @@ folly::Future<Status> FulltextIndexScanExecutor::execute() {
if (ftIndexScan->isEdge()) {
DataSet edges({"edge"});
for (auto& item : esResultValue.items) {
// TODO(hs.zhang): return item.score
Edge edge;
edge.src = item.src;
edge.dst = item.dst;
edge.ranking = item.rank;
edge.type = ftIndexScan->schemaId();
edges.emplace_back(Row({std::move(edge)}));
edges.emplace_back(Row({std::move(edge), item.score}));
}
finish(ResultBuilder().value(Value(std::move(edges))).iter(Iterator::Kind::kProp).build());
} else {
DataSet vertices({kVid});
for (auto& item : esResultValue.items) {
// TODO(hs.zhang): return item.score
vertices.emplace_back(Row({item.vid}));
vertices.emplace_back(Row({item.vid, item.score}));
}
finish(ResultBuilder().value(Value(std::move(vertices))).iter(Iterator::Kind::kProp).build());
}
Expand Down Expand Up @@ -78,17 +76,15 @@ StatusOr<plugin::ESQueryResult> FulltextIndexScanExecutor::accessFulltextIndex(
TextSearchExpression* tsExpr) {
std::function<StatusOr<nebula::plugin::ESQueryResult>()> execFunc;
plugin::ESAdapter& esAdapter = esAdapter_;
auto* ftIndexScan = asNode<FulltextIndexScan>(node());
switch (tsExpr->kind()) {
case Expression::Kind::kESQUERY: {
auto arg = tsExpr->arg();
auto index = arg->index();
auto query = arg->query();
auto props = arg->props();
auto count = arg->count();
auto offset = arg->offset();
execFunc = [=, &esAdapter]() {
return esAdapter.queryString(index, query, props, offset, count);
};
int64_t offset = ftIndexScan->offset();
int64_t count = ftIndexScan->limit();
execFunc = [=, &esAdapter]() { return esAdapter.queryString(index, query, offset, count); };
break;
}
default: {
Expand Down
42 changes: 7 additions & 35 deletions src/graph/validator/LookupValidator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,42 +164,14 @@ Status LookupValidator::validateWhere() {
std::string& index = arg->index();
auto metaClient = qctx_->getMetaClient();
meta::cpp2::FTIndex ftIndex;
if (index.empty()) {
auto result = metaClient->getFTIndexFromCache(spaceId(), schemaId());
NG_RETURN_IF_ERROR(result);
auto indexes = std::move(result).value();
if (indexes.size() == 0) {
return Status::Error("There is no ft index of schema");
} else if (indexes.size() > 1) {
return Status::Error("There is more than one schema, one must be specified");
}
index = indexes.begin()->first;
ftIndex = indexes.begin()->second;
} else {
// TODO(hs.zhang): Directly get `ftIndex` by `index`
auto result = metaClient->getFTIndexFromCache(spaceId(), schemaId());
NG_RETURN_IF_ERROR(result);
auto indexes = std::move(result).value();
auto iter = indexes.find(index);
if (iter == indexes.end()) {
return Status::Error("Index %s is not found", index.c_str());
}
ftIndex = iter->second;
}
auto& props = arg->props();
if (props.empty()) {
for (auto& f : *ftIndex.fields()) {
props.push_back(f);
}
} else {
std::set<std::string> fields(ftIndex.fields()->begin(), ftIndex.fields()->end());
for (auto& p : props) {
if (fields.count(p)) {
continue;
}
return Status::Error("Index %s does not include %s", index.c_str(), p.c_str());
}
auto result = metaClient->getFTIndexFromCache(spaceId(), schemaId());
NG_RETURN_IF_ERROR(result);
auto indexes = std::move(result).value();
auto iter = indexes.find(index);
if (iter == indexes.end()) {
return Status::Error("Index %s is not found", index.c_str());
}
ftIndex = iter->second;
} else {
if (filter != nullptr) {
auto vars = graph::ExpressionUtils::ExtractInnerVars(filter, qctx_);
Expand Down
34 changes: 4 additions & 30 deletions src/parser/parser.yy
Original file line number Diff line number Diff line change
Expand Up @@ -2066,38 +2066,12 @@ sign_out_service_sentence


text_search_argument
: STRING COMMA STRING COMMA L_BRACKET name_label_list R_BRACKET{
std::vector<std::string> props;
for(auto& p:$6->labels()){
props.push_back(*p);
}
delete $6;
auto args = TextSearchArgument::make(qctx->objPool(), *$1, *$3, props);
delete $1;
delete $3;
$$ = args;
}
| STRING COMMA STRING {
auto args = TextSearchArgument::make(qctx->objPool(), *$1, *$3, {});
: name_label COMMA STRING {
auto args = TextSearchArgument::make(qctx->objPool(), *$1, *$3);
delete $1;
delete $3;
$$ = args;
}
| STRING COMMA L_BRACKET name_label_list R_BRACKET {
std::vector<std::string> props;
for(auto& p:$4->labels()){
props.push_back(*p);
}
delete $4;
auto args = TextSearchArgument::make(qctx->objPool(), "", *$1, props);
delete $1;
$$ = args;
}
| STRING {
auto args = TextSearchArgument::make(qctx->objPool(), "", *$1, {});
delete $1;
$$ = args;
}
;

text_search_expression
Expand Down Expand Up @@ -2637,8 +2611,8 @@ opt_analyzer
: %empty {
$$ = nullptr;
}
| KW_USE KW_ANALYZER ASSIGN STRING {
$$ = $4;
| KW_ANALYZER ASSIGN STRING {
$$ = $3;
}
;

Expand Down
17 changes: 1 addition & 16 deletions src/parser/test/ParserTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2929,22 +2929,7 @@ TEST_F(ParserTest, Zone) {

TEST_F(ParserTest, FullText) {
{
std::string query = "LOOKUP ON t1 WHERE ES_QUERY(\"abc\", \"qwerty\", [a,b,c,d])";
auto result = parse(query);
EXPECT_TRUE(result.ok()) << result.status();
}
{
std::string query = "LOOKUP ON t1 WHERE ES_QUERY(\"qwerty\", [a,b,c,d])";
auto result = parse(query);
EXPECT_TRUE(result.ok()) << result.status();
}
{
std::string query = "LOOKUP ON t1 WHERE ES_QUERY(\"index1\", \"qwerty\")";
auto result = parse(query);
EXPECT_TRUE(result.ok()) << result.status();
}
{
std::string query = "LOOKUP ON t1 WHERE ES_QUERY(\"qwerty\")";
std::string query = "LOOKUP ON t1 WHERE ES_QUERY(abc, \"qwerty\")";
auto result = parse(query);
EXPECT_TRUE(result.ok()) << result.status();
}
Expand Down