diff --git a/internal/core/src/exec/expression/Expr.h b/internal/core/src/exec/expression/Expr.h index 3298d8d7b4e19..972e352c0890d 100644 --- a/internal/core/src/exec/expression/Expr.h +++ b/internal/core/src/exec/expression/Expr.h @@ -1189,22 +1189,17 @@ class SegmentExpr : public Expr { } using Index = index::ScalarIndex; - if (op == OpType::Match) { - for (size_t i = current_index_chunk_; i < num_index_chunk_; i++) { - auto pw = - segment_->chunk_scalar_index(field_id_, i); - auto index_ptr = const_cast(pw.get()); - // 1, index support regex query, then index handles the query; - // 2, index has raw data, then call index.Reverse_Lookup to handle the query; - if (!index_ptr->SupportRegexQuery() && - !index_ptr->HasRawData()) { - return false; - } - // all chunks have same index. - return true; - } + if (op == OpType::Match || op == OpType::InnerMatch || + op == OpType::PostfixMatch) { + auto pw = segment_->chunk_scalar_index( + field_id_, current_index_chunk_); + auto* index_ptr = const_cast(pw.get()); + // 1, index support regex query and try use it, then index handles the query; + // 2, index has raw data, then call index.Reverse_Lookup to handle the query; + return (index_ptr->TryUseRegexQuery() && + index_ptr->SupportRegexQuery()) || + index_ptr->HasRawData(); } - return true; } diff --git a/internal/core/src/exec/expression/UnaryExpr.cpp b/internal/core/src/exec/expression/UnaryExpr.cpp index 0231b13a2339a..fa9194b490eab 100644 --- a/internal/core/src/exec/expression/UnaryExpr.cpp +++ b/internal/core/src/exec/expression/UnaryExpr.cpp @@ -1828,17 +1828,26 @@ PhyUnaryRangeFilterExpr::ExecRangeVisitorImplForData(EvalCtx& context) { template bool PhyUnaryRangeFilterExpr::CanUseIndex() { - bool res = is_index_mode_ && SegmentExpr::CanUseIndex(expr_->op_type_); - use_index_ = res; - return res; + use_index_ = is_index_mode_ && SegmentExpr::CanUseIndex(expr_->op_type_); + return use_index_; } bool PhyUnaryRangeFilterExpr::CanUseIndexForJson(DataType val_type) { - use_index_ = + auto has_index = segment_->HasIndex(field_id_, milvus::Json::pointer(expr_->column_.nested_path_), val_type); + switch (val_type) { + case DataType::STRING: + use_index_ = has_index && + expr_->op_type_ != proto::plan::OpType::Match && + expr_->op_type_ != proto::plan::OpType::PostfixMatch && + expr_->op_type_ != proto::plan::OpType::InnerMatch; + break; + default: + use_index_ = has_index; + } return use_index_; } diff --git a/internal/core/src/index/HybridScalarIndex.h b/internal/core/src/index/HybridScalarIndex.h index 432c16cc7fc27..4963a43395956 100644 --- a/internal/core/src/index/HybridScalarIndex.h +++ b/internal/core/src/index/HybridScalarIndex.h @@ -115,6 +115,11 @@ class HybridScalarIndex : public ScalarIndex { return internal_index_->PatternMatch(pattern, op); } + bool + TryUseRegexQuery() const override { + return internal_index_->TryUseRegexQuery(); + } + bool SupportRegexQuery() const override { return internal_index_->SupportRegexQuery(); diff --git a/internal/core/src/index/InvertedIndexTantivy.h b/internal/core/src/index/InvertedIndexTantivy.h index 059107a49a3d3..1e0a6d24a2c2b 100644 --- a/internal/core/src/index/InvertedIndexTantivy.h +++ b/internal/core/src/index/InvertedIndexTantivy.h @@ -228,6 +228,12 @@ class InvertedIndexTantivy : public ScalarIndex { return std::is_same_v; } + bool + TryUseRegexQuery() const override { + // for inverted index, not use regex query to implement match + return false; + } + const TargetBitmap RegexQuery(const std::string& regex_pattern) override; diff --git a/internal/core/src/index/ScalarIndex.h b/internal/core/src/index/ScalarIndex.h index 4dc58b586aeb9..1314de269cd0c 100644 --- a/internal/core/src/index/ScalarIndex.h +++ b/internal/core/src/index/ScalarIndex.h @@ -151,6 +151,11 @@ class ScalarIndex : public IndexBase { return false; } + virtual bool + TryUseRegexQuery() const { + return true; + } + virtual const TargetBitmap RegexQuery(const std::string& pattern) { PanicInfo(Unsupported, "regex query is not supported");