be/src/exprs/function/function_search.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "exprs/function/function_search.h" |
19 | | |
20 | | #include <CLucene/config/repl_wchar.h> |
21 | | #include <CLucene/search/Scorer.h> |
22 | | #include <gen_cpp/Exprs_types.h> |
23 | | #include <glog/logging.h> |
24 | | |
25 | | #include <limits> |
26 | | #include <memory> |
27 | | #include <roaring/roaring.hh> |
28 | | #include <set> |
29 | | #include <string> |
30 | | #include <unordered_map> |
31 | | #include <unordered_set> |
32 | | #include <vector> |
33 | | |
34 | | #include "common/status.h" |
35 | | #include "core/block/columns_with_type_and_name.h" |
36 | | #include "core/column/column_const.h" |
37 | | #include "core/data_type/data_type_string.h" |
38 | | #include "exprs/function/simple_function_factory.h" |
39 | | #include "exprs/vexpr_context.h" |
40 | | #include "storage/index/index_file_reader.h" |
41 | | #include "storage/index/index_query_context.h" |
42 | | #include "storage/index/inverted/analyzer/analyzer.h" |
43 | | #include "storage/index/inverted/inverted_index_iterator.h" |
44 | | #include "storage/index/inverted/inverted_index_parser.h" |
45 | | #include "storage/index/inverted/inverted_index_reader.h" |
46 | | #include "storage/index/inverted/inverted_index_searcher.h" |
47 | | #include "storage/index/inverted/query/query_helper.h" |
48 | | #include "storage/index/inverted/query_v2/all_query/all_query.h" |
49 | | #include "storage/index/inverted/query_v2/bit_set_query/bit_set_query.h" |
50 | | #include "storage/index/inverted/query_v2/boolean_query/boolean_query_builder.h" |
51 | | #include "storage/index/inverted/query_v2/boolean_query/operator.h" |
52 | | #include "storage/index/inverted/query_v2/collect/doc_set_collector.h" |
53 | | #include "storage/index/inverted/query_v2/collect/top_k_collector.h" |
54 | | #include "storage/index/inverted/query_v2/phrase_query/multi_phrase_query.h" |
55 | | #include "storage/index/inverted/query_v2/phrase_query/phrase_query.h" |
56 | | #include "storage/index/inverted/query_v2/regexp_query/regexp_query.h" |
57 | | #include "storage/index/inverted/query_v2/term_query/term_query.h" |
58 | | #include "storage/index/inverted/query_v2/wildcard_query/wildcard_query.h" |
59 | | #include "storage/index/inverted/util/string_helper.h" |
60 | | #include "storage/segment/segment.h" |
61 | | #include "storage/segment/variant/nested_group_path.h" |
62 | | #include "storage/segment/variant/nested_group_provider.h" |
63 | | #include "storage/segment/variant/variant_column_reader.h" |
64 | | #include "storage/types.h" |
65 | | #include "util/string_util.h" |
66 | | #include "util/thrift_util.h" |
67 | | |
68 | | namespace doris { |
69 | | |
70 | | // Build canonical DSL signature for cache key. |
71 | | // Serializes the entire TSearchParam via Thrift binary protocol so that |
72 | | // every field (DSL, AST root, field bindings, default_operator, |
73 | | // minimum_should_match, etc.) is included automatically. |
74 | 1.23k | static std::string build_dsl_signature(const TSearchParam& param) { |
75 | 1.23k | ThriftSerializer ser(false, 1024); |
76 | 1.23k | TSearchParam copy = param; |
77 | 1.23k | std::string sig; |
78 | 1.23k | auto st = ser.serialize(©, &sig); |
79 | 1.23k | if (UNLIKELY(!st.ok())) { |
80 | 0 | LOG(WARNING) << "build_dsl_signature: Thrift serialization failed: " << st.to_string() |
81 | 0 | << ", caching disabled for this query"; |
82 | 0 | return ""; |
83 | 0 | } |
84 | 1.23k | return sig; |
85 | 1.23k | } |
86 | | |
87 | | // Extract segment path prefix from the first available inverted index iterator. |
88 | | // All fields in the same segment share the same path prefix. |
89 | | static std::string extract_segment_prefix( |
90 | 1.22k | const std::unordered_map<std::string, IndexIterator*>& iterators) { |
91 | 1.22k | for (const auto& [field_name, iter] : iterators) { |
92 | 1.22k | auto* inv_iter = dynamic_cast<InvertedIndexIterator*>(iter); |
93 | 1.22k | if (!inv_iter) continue; |
94 | | // Try fulltext reader first, then string type |
95 | 1.20k | for (auto type : |
96 | 1.50k | {InvertedIndexReaderType::FULLTEXT, InvertedIndexReaderType::STRING_TYPE}) { |
97 | 1.50k | IndexReaderType reader_type = type; |
98 | 1.50k | auto reader = inv_iter->get_reader(reader_type); |
99 | 1.50k | if (!reader) continue; |
100 | 1.19k | auto inv_reader = std::dynamic_pointer_cast<InvertedIndexReader>(reader); |
101 | 1.19k | if (!inv_reader) continue; |
102 | 1.19k | auto file_reader = inv_reader->get_index_file_reader(); |
103 | 1.19k | if (!file_reader) continue; |
104 | 1.19k | return file_reader->get_index_path_prefix(); |
105 | 1.19k | } |
106 | 1.20k | } |
107 | 26 | VLOG_DEBUG << "extract_segment_prefix: no suitable inverted index reader found across " |
108 | 1 | << iterators.size() << " iterators, caching disabled for this query"; |
109 | 26 | return ""; |
110 | 1.22k | } |
111 | | |
112 | | namespace { |
113 | | |
114 | 3 | bool is_nested_group_search_supported() { |
115 | 3 | auto provider = segment_v2::create_nested_group_read_provider(); |
116 | 3 | return provider != nullptr && provider->should_enable_nested_group_read_path(); |
117 | 3 | } |
118 | | |
119 | | class ResolverNullBitmapAdapter final : public query_v2::NullBitmapResolver { |
120 | | public: |
121 | 1.03k | explicit ResolverNullBitmapAdapter(const FieldReaderResolver& resolver) : _resolver(resolver) {} |
122 | | |
123 | | segment_v2::IndexIterator* iterator_for(const query_v2::Scorer& /*scorer*/, |
124 | 2.70k | const std::string& logical_field) const override { |
125 | 2.70k | if (logical_field.empty()) { |
126 | 0 | return nullptr; |
127 | 0 | } |
128 | 2.70k | return _resolver.get_iterator(logical_field); |
129 | 2.70k | } |
130 | | |
131 | | private: |
132 | | const FieldReaderResolver& _resolver; |
133 | | }; |
134 | | |
135 | | void populate_binding_context(const FieldReaderResolver& resolver, |
136 | 1.04k | query_v2::QueryExecutionContext* exec_ctx) { |
137 | 1.04k | DCHECK(exec_ctx != nullptr); |
138 | 1.04k | exec_ctx->readers = resolver.readers(); |
139 | 1.04k | exec_ctx->reader_bindings = resolver.reader_bindings(); |
140 | 1.04k | exec_ctx->field_reader_bindings = resolver.field_readers(); |
141 | 1.55k | for (const auto& [binding_key, binding] : resolver.binding_cache()) { |
142 | 1.55k | if (binding_key.empty()) { |
143 | 0 | continue; |
144 | 0 | } |
145 | 1.55k | query_v2::FieldBindingContext binding_ctx; |
146 | 1.55k | binding_ctx.logical_field_name = binding.logical_field_name; |
147 | 1.55k | binding_ctx.stored_field_name = binding.stored_field_name; |
148 | 1.55k | binding_ctx.stored_field_wstr = binding.stored_field_wstr; |
149 | 1.55k | exec_ctx->binding_fields.emplace(binding_key, std::move(binding_ctx)); |
150 | 1.55k | } |
151 | 1.04k | } |
152 | | |
153 | | query_v2::QueryExecutionContext build_query_execution_context( |
154 | | uint32_t segment_num_rows, const FieldReaderResolver& resolver, |
155 | 1.02k | query_v2::NullBitmapResolver* null_resolver) { |
156 | 1.02k | query_v2::QueryExecutionContext exec_ctx; |
157 | 1.02k | exec_ctx.segment_num_rows = segment_num_rows; |
158 | 1.02k | populate_binding_context(resolver, &exec_ctx); |
159 | 1.02k | exec_ctx.null_resolver = null_resolver; |
160 | 1.02k | return exec_ctx; |
161 | 1.02k | } |
162 | | |
163 | | } // namespace |
164 | | |
165 | | Status FieldReaderResolver::resolve(const std::string& field_name, |
166 | | InvertedIndexQueryType query_type, |
167 | 1.89k | FieldReaderBinding* binding) { |
168 | 1.89k | DCHECK(binding != nullptr); |
169 | | |
170 | | // Check if this is a variant subcolumn |
171 | 1.89k | bool is_variant_sub = is_variant_subcolumn(field_name); |
172 | | |
173 | 1.89k | auto data_it = _data_type_with_names.find(field_name); |
174 | 1.89k | if (data_it == _data_type_with_names.end()) { |
175 | | // For variant subcolumns, not finding the index is normal (the subcolumn may not exist in this segment) |
176 | | // Return OK but with null binding to signal "no match" |
177 | 8 | if (is_variant_sub) { |
178 | 3 | VLOG_DEBUG << "Variant subcolumn '" << field_name |
179 | 0 | << "' not found in this segment, treating as no match"; |
180 | 3 | *binding = FieldReaderBinding(); |
181 | 3 | return Status::OK(); |
182 | 3 | } |
183 | | // For normal fields, this is an error |
184 | 5 | return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( |
185 | 5 | "field '{}' not found in inverted index metadata", field_name); |
186 | 8 | } |
187 | | |
188 | 1.88k | const auto& stored_field_name = data_it->second.first; |
189 | 1.88k | const auto binding_key = binding_key_for(stored_field_name, query_type); |
190 | | |
191 | 1.88k | auto cache_it = _cache.find(binding_key); |
192 | 1.88k | if (cache_it != _cache.end()) { |
193 | 298 | *binding = cache_it->second; |
194 | 298 | return Status::OK(); |
195 | 298 | } |
196 | | |
197 | 1.58k | auto iterator_it = _iterators.find(field_name); |
198 | 1.58k | if (iterator_it == _iterators.end() || iterator_it->second == nullptr) { |
199 | | // For variant subcolumns, not finding the iterator is normal |
200 | 17 | if (is_variant_sub) { |
201 | 0 | VLOG_DEBUG << "Variant subcolumn '" << field_name |
202 | 0 | << "' iterator not found in this segment, treating as no match"; |
203 | 0 | *binding = FieldReaderBinding(); |
204 | 0 | return Status::OK(); |
205 | 0 | } |
206 | 17 | return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( |
207 | 17 | "iterator not found for field '{}'", field_name); |
208 | 17 | } |
209 | | |
210 | 1.56k | auto* inverted_iterator = dynamic_cast<InvertedIndexIterator*>(iterator_it->second); |
211 | 1.56k | if (inverted_iterator == nullptr) { |
212 | 2 | return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( |
213 | 2 | "iterator for field '{}' is not InvertedIndexIterator", field_name); |
214 | 2 | } |
215 | | |
216 | | // For variant subcolumns, FE resolves the field pattern to a specific index and sends |
217 | | // its index_properties via TSearchFieldBinding. When FE picks an analyzer-based index, |
218 | | // upgrade EQUAL_QUERY/WILDCARD_QUERY to MATCH_ANY_QUERY so select_best_reader picks the |
219 | | // FULLTEXT reader instead of STRING_TYPE. Without this upgrade: |
220 | | // - TERM (EQUAL_QUERY) clauses would open the wrong (untokenized) index directory |
221 | | // - WILDCARD clauses would enumerate terms from the wrong index, returning empty results |
222 | | // |
223 | | // For regular (non-variant) columns with multiple indexes, the caller (build_leaf_query) |
224 | | // is responsible for passing the appropriate query_type: MATCH_ANY_QUERY for tokenized |
225 | | // queries (TERM) and EQUAL_QUERY for exact-match queries (EXACT). This ensures |
226 | | // select_best_reader picks FULLTEXT vs STRING_TYPE correctly without needing an explicit |
227 | | // analyzer key, since the query_type alone drives the reader type preference. |
228 | 1.56k | InvertedIndexQueryType effective_query_type = query_type; |
229 | 1.56k | auto fb_it = _field_binding_map.find(field_name); |
230 | 1.56k | std::string analyzer_key; |
231 | 1.56k | if (is_variant_sub && fb_it != _field_binding_map.end() && |
232 | 1.56k | fb_it->second->__isset.index_properties && !fb_it->second->index_properties.empty()) { |
233 | 60 | analyzer_key = normalize_analyzer_key( |
234 | 60 | build_analyzer_key_from_properties(fb_it->second->index_properties)); |
235 | 60 | if (inverted_index::InvertedIndexAnalyzer::should_analyzer( |
236 | 60 | fb_it->second->index_properties) && |
237 | 60 | (effective_query_type == InvertedIndexQueryType::EQUAL_QUERY || |
238 | 60 | effective_query_type == InvertedIndexQueryType::WILDCARD_QUERY)) { |
239 | 0 | effective_query_type = InvertedIndexQueryType::MATCH_ANY_QUERY; |
240 | 0 | } |
241 | 60 | } |
242 | | |
243 | 1.56k | Result<InvertedIndexReaderPtr> reader_result; |
244 | 1.56k | const auto& column_type = data_it->second.second; |
245 | 1.56k | if (column_type) { |
246 | 1.54k | reader_result = inverted_iterator->select_best_reader(column_type, effective_query_type, |
247 | 1.54k | analyzer_key); |
248 | 1.54k | } else { |
249 | 18 | reader_result = inverted_iterator->select_best_reader(analyzer_key); |
250 | 18 | } |
251 | | |
252 | 1.56k | if (!reader_result.has_value()) { |
253 | 0 | return reader_result.error(); |
254 | 0 | } |
255 | | |
256 | 1.56k | auto inverted_reader = reader_result.value(); |
257 | 1.56k | if (inverted_reader == nullptr) { |
258 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( |
259 | 0 | "selected reader is null for field '{}'", field_name); |
260 | 0 | } |
261 | | |
262 | 1.56k | auto index_file_reader = inverted_reader->get_index_file_reader(); |
263 | 1.56k | if (index_file_reader == nullptr) { |
264 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_FILE_NOT_FOUND>( |
265 | 0 | "index file reader is null for field '{}'", field_name); |
266 | 0 | } |
267 | | |
268 | | // Use InvertedIndexSearcherCache to avoid re-opening index files repeatedly |
269 | 1.56k | auto index_file_key = |
270 | 1.56k | index_file_reader->get_index_file_cache_key(&inverted_reader->get_index_meta()); |
271 | 1.56k | InvertedIndexSearcherCache::CacheKey searcher_cache_key(index_file_key); |
272 | 1.56k | InvertedIndexCacheHandle searcher_cache_handle; |
273 | 1.56k | bool cache_hit = InvertedIndexSearcherCache::instance()->lookup(searcher_cache_key, |
274 | 1.56k | &searcher_cache_handle); |
275 | | |
276 | 1.56k | std::shared_ptr<lucene::index::IndexReader> reader_holder; |
277 | 1.56k | if (cache_hit) { |
278 | 1.30k | auto searcher_variant = searcher_cache_handle.get_index_searcher(); |
279 | 1.30k | auto* searcher_ptr = std::get_if<FulltextIndexSearcherPtr>(&searcher_variant); |
280 | 1.31k | if (searcher_ptr != nullptr && *searcher_ptr != nullptr) { |
281 | 1.30k | reader_holder = std::shared_ptr<lucene::index::IndexReader>( |
282 | 1.30k | (*searcher_ptr)->getReader(), |
283 | 1.31k | [](lucene::index::IndexReader*) { /* lifetime managed by searcher cache */ }); |
284 | 1.30k | } |
285 | 1.30k | } |
286 | | |
287 | 1.56k | if (!reader_holder) { |
288 | | // Cache miss: open directory, build IndexSearcher, insert into cache |
289 | 256 | RETURN_IF_ERROR( |
290 | 256 | index_file_reader->init(config::inverted_index_read_buffer_size, _context->io_ctx)); |
291 | 256 | auto directory = DORIS_TRY( |
292 | 256 | index_file_reader->open(&inverted_reader->get_index_meta(), _context->io_ctx)); |
293 | | |
294 | 256 | auto index_searcher_builder = DORIS_TRY( |
295 | 256 | IndexSearcherBuilder::create_index_searcher_builder(inverted_reader->type())); |
296 | 256 | auto searcher_result = |
297 | 256 | DORIS_TRY(index_searcher_builder->get_index_searcher(directory.get())); |
298 | 256 | auto reader_size = index_searcher_builder->get_reader_size(); |
299 | | |
300 | 256 | auto* cache_value = new InvertedIndexSearcherCache::CacheValue(std::move(searcher_result), |
301 | 256 | reader_size, UnixMillis()); |
302 | 256 | InvertedIndexSearcherCache::instance()->insert(searcher_cache_key, cache_value, |
303 | 256 | &searcher_cache_handle); |
304 | | |
305 | 256 | auto new_variant = searcher_cache_handle.get_index_searcher(); |
306 | 256 | auto* new_ptr = std::get_if<FulltextIndexSearcherPtr>(&new_variant); |
307 | 256 | if (new_ptr != nullptr && *new_ptr != nullptr) { |
308 | 255 | reader_holder = std::shared_ptr<lucene::index::IndexReader>( |
309 | 255 | (*new_ptr)->getReader(), |
310 | 256 | [](lucene::index::IndexReader*) { /* lifetime managed by searcher cache */ }); |
311 | 255 | } |
312 | | |
313 | 256 | if (!reader_holder) { |
314 | 0 | return Status::Error<ErrorCode::INVERTED_INDEX_CLUCENE_ERROR>( |
315 | 0 | "failed to build IndexSearcher for field '{}'", field_name); |
316 | 0 | } |
317 | 256 | } |
318 | | |
319 | 1.56k | _searcher_cache_handles.push_back(std::move(searcher_cache_handle)); |
320 | | |
321 | 1.56k | FieldReaderBinding resolved; |
322 | 1.56k | resolved.logical_field_name = field_name; |
323 | 1.56k | resolved.stored_field_name = stored_field_name; |
324 | 1.56k | resolved.stored_field_wstr = StringHelper::to_wstring(resolved.stored_field_name); |
325 | 1.56k | resolved.column_type = column_type; |
326 | 1.56k | resolved.query_type = effective_query_type; |
327 | 1.56k | resolved.inverted_reader = inverted_reader; |
328 | 1.56k | resolved.lucene_reader = reader_holder; |
329 | | // Prefer FE-provided index_properties (needed for variant subcolumn field_pattern matching) |
330 | | // Reuse fb_it from earlier lookup above. |
331 | 1.56k | if (fb_it != _field_binding_map.end() && fb_it->second->__isset.index_properties && |
332 | 1.56k | !fb_it->second->index_properties.empty()) { |
333 | 1.26k | resolved.index_properties = fb_it->second->index_properties; |
334 | 1.26k | } else { |
335 | 303 | resolved.index_properties = inverted_reader->get_index_properties(); |
336 | 303 | } |
337 | 1.56k | resolved.binding_key = binding_key; |
338 | 1.56k | resolved.analyzer_key = |
339 | 1.56k | normalize_analyzer_key(build_analyzer_key_from_properties(resolved.index_properties)); |
340 | | |
341 | 1.56k | _binding_readers[binding_key] = reader_holder; |
342 | 1.56k | _field_readers[resolved.stored_field_wstr] = reader_holder; |
343 | 1.56k | _readers.emplace_back(reader_holder); |
344 | 1.56k | _cache.emplace(binding_key, resolved); |
345 | 1.56k | *binding = resolved; |
346 | 1.56k | return Status::OK(); |
347 | 1.56k | } |
348 | | |
349 | | Status FunctionSearch::execute_impl(FunctionContext* /*context*/, Block& /*block*/, |
350 | | const ColumnNumbers& /*arguments*/, uint32_t /*result*/, |
351 | 4 | size_t /*input_rows_count*/) const { |
352 | 4 | return Status::RuntimeError("only inverted index queries are supported"); |
353 | 4 | } |
354 | | |
355 | | // Enhanced implementation: Handle new parameter structure (DSL + SlotReferences) |
356 | | Status FunctionSearch::evaluate_inverted_index( |
357 | | const ColumnsWithTypeAndName& arguments, |
358 | | const std::vector<IndexFieldNameAndTypePair>& data_type_with_names, |
359 | | std::vector<IndexIterator*> iterators, uint32_t num_rows, |
360 | | const InvertedIndexAnalyzerCtx* /*analyzer_ctx*/, |
361 | 1 | InvertedIndexResultBitmap& bitmap_result) const { |
362 | 1 | return Status::OK(); |
363 | 1 | } |
364 | | |
365 | | Status FunctionSearch::evaluate_inverted_index_with_search_param( |
366 | | const TSearchParam& search_param, |
367 | | const std::unordered_map<std::string, IndexFieldNameAndTypePair>& data_type_with_names, |
368 | | std::unordered_map<std::string, IndexIterator*> iterators, uint32_t num_rows, |
369 | 31 | InvertedIndexResultBitmap& bitmap_result, bool enable_cache) const { |
370 | 31 | static const std::unordered_map<std::string, int> empty_field_to_column_id; |
371 | 31 | return evaluate_inverted_index_with_search_param( |
372 | 31 | search_param, data_type_with_names, std::move(iterators), num_rows, bitmap_result, |
373 | 31 | enable_cache, nullptr, empty_field_to_column_id); |
374 | 31 | } |
375 | | |
376 | | Status FunctionSearch::evaluate_inverted_index_with_search_param( |
377 | | const TSearchParam& search_param, |
378 | | const std::unordered_map<std::string, IndexFieldNameAndTypePair>& data_type_with_names, |
379 | | std::unordered_map<std::string, IndexIterator*> iterators, uint32_t num_rows, |
380 | | InvertedIndexResultBitmap& bitmap_result, bool enable_cache, |
381 | | const IndexExecContext* index_exec_ctx, |
382 | | const std::unordered_map<std::string, int>& field_name_to_column_id, |
383 | 1.25k | const std::shared_ptr<IndexQueryContext>& index_query_context) const { |
384 | 1.25k | const bool is_nested_query = search_param.root.clause_type == "NESTED"; |
385 | 1.25k | if (is_nested_query && !is_nested_group_search_supported()) { |
386 | 3 | return Status::NotSupported( |
387 | 3 | "NESTED query requires NestedGroup support, which is unavailable in this build"); |
388 | 3 | } |
389 | | |
390 | 1.24k | if (!is_nested_query && (iterators.empty() || data_type_with_names.empty())) { |
391 | 5 | LOG(INFO) << "No indexed columns or iterators available, returning empty result, dsl:" |
392 | 5 | << search_param.original_dsl; |
393 | 5 | bitmap_result = InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(), |
394 | 5 | std::make_shared<roaring::Roaring>()); |
395 | 5 | return Status::OK(); |
396 | 5 | } |
397 | | |
398 | | // DSL result cache: reuse InvertedIndexQueryCache with SEARCH_DSL_QUERY type |
399 | 1.24k | auto* dsl_cache = enable_cache ? InvertedIndexQueryCache::instance() : nullptr; |
400 | 1.24k | std::string seg_prefix; |
401 | 1.24k | std::string dsl_sig; |
402 | 1.24k | InvertedIndexQueryCache::CacheKey dsl_cache_key; |
403 | 1.24k | bool cache_usable = false; |
404 | 1.24k | if (dsl_cache) { |
405 | 1.22k | seg_prefix = extract_segment_prefix(iterators); |
406 | 1.22k | dsl_sig = build_dsl_signature(search_param); |
407 | 1.22k | if (!seg_prefix.empty() && !dsl_sig.empty()) { |
408 | 1.20k | dsl_cache_key = InvertedIndexQueryCache::CacheKey { |
409 | 1.20k | seg_prefix, "__search_dsl__", InvertedIndexQueryType::SEARCH_DSL_QUERY, |
410 | 1.20k | dsl_sig}; |
411 | 1.20k | cache_usable = true; |
412 | 1.20k | InvertedIndexQueryCacheHandle dsl_cache_handle; |
413 | 1.20k | if (dsl_cache->lookup(dsl_cache_key, &dsl_cache_handle)) { |
414 | 165 | auto cached_bitmap = dsl_cache_handle.get_bitmap(); |
415 | 168 | if (cached_bitmap) { |
416 | | // Also retrieve cached null bitmap for three-valued SQL logic |
417 | | // (needed by compound operators NOT, OR, AND in VCompoundPred) |
418 | 168 | auto null_cache_key = InvertedIndexQueryCache::CacheKey { |
419 | 168 | seg_prefix, "__search_dsl__", InvertedIndexQueryType::SEARCH_DSL_QUERY, |
420 | 168 | dsl_sig + "__null"}; |
421 | 168 | InvertedIndexQueryCacheHandle null_cache_handle; |
422 | 168 | std::shared_ptr<roaring::Roaring> null_bitmap; |
423 | 168 | if (dsl_cache->lookup(null_cache_key, &null_cache_handle)) { |
424 | 165 | null_bitmap = null_cache_handle.get_bitmap(); |
425 | 165 | } |
426 | 168 | if (!null_bitmap) { |
427 | 0 | null_bitmap = std::make_shared<roaring::Roaring>(); |
428 | 0 | } |
429 | 168 | bitmap_result = |
430 | 168 | InvertedIndexResultBitmap(cached_bitmap, std::move(null_bitmap)); |
431 | 168 | return Status::OK(); |
432 | 168 | } |
433 | 165 | } |
434 | 1.20k | } |
435 | 1.22k | } |
436 | | |
437 | 1.07k | std::shared_ptr<IndexQueryContext> context; |
438 | 1.07k | if (index_query_context) { |
439 | 1.03k | context = index_query_context; |
440 | 1.03k | } else { |
441 | 37 | context = std::make_shared<IndexQueryContext>(); |
442 | 37 | context->collection_statistics = std::make_shared<CollectionStatistics>(); |
443 | 37 | context->collection_similarity = std::make_shared<CollectionSimilarity>(); |
444 | 37 | } |
445 | | |
446 | | // NESTED() queries evaluate predicates on the flattened "element space" of a nested group. |
447 | | // For VARIANT nested groups, the indexed lucene field (stored_field_name) uses: |
448 | | // parent_unique_id + "." + <variant-relative nested path> |
449 | | // where the nested path is rooted at either: |
450 | | // - "__D0_root__" for top-level array<object> (NESTED(data, ...)) |
451 | | // - "<nested_path_after_variant_root>" for object fields (NESTED(data.items, ...)) |
452 | | // |
453 | | // FE field bindings are expressed using logical column paths (e.g. "data.items.msg"), so for |
454 | | // NESTED() we normalize stored_field_name suffix to be consistent with the nested group root. |
455 | 1.07k | std::unordered_map<std::string, IndexFieldNameAndTypePair> patched_data_type_with_names; |
456 | 1.07k | const auto* effective_data_type_with_names = &data_type_with_names; |
457 | 1.07k | if (is_nested_query && search_param.root.__isset.nested_path) { |
458 | 0 | const std::string& nested_path = search_param.root.nested_path; |
459 | 0 | const auto dot_pos = nested_path.find('.'); |
460 | 0 | const std::string root_field = |
461 | 0 | (dot_pos == std::string::npos) ? nested_path : nested_path.substr(0, dot_pos); |
462 | 0 | const std::string root_prefix = root_field + "."; |
463 | 0 | const std::string array_path = (dot_pos == std::string::npos) |
464 | 0 | ? std::string(segment_v2::kRootNestedGroupPath) |
465 | 0 | : nested_path.substr(dot_pos + 1); |
466 | |
|
467 | 0 | bool copied = false; |
468 | 0 | for (const auto& fb : search_param.field_bindings) { |
469 | 0 | if (!fb.__isset.is_variant_subcolumn || !fb.is_variant_subcolumn) { |
470 | 0 | continue; |
471 | 0 | } |
472 | 0 | if (fb.field_name.empty()) { |
473 | 0 | continue; |
474 | 0 | } |
475 | 0 | const auto it_orig = data_type_with_names.find(fb.field_name); |
476 | 0 | if (it_orig == data_type_with_names.end()) { |
477 | 0 | continue; |
478 | 0 | } |
479 | 0 | const std::string& old_stored = it_orig->second.first; |
480 | 0 | const auto first_dot = old_stored.find('.'); |
481 | 0 | if (first_dot == std::string::npos) { |
482 | 0 | continue; |
483 | 0 | } |
484 | 0 | std::string sub_path; |
485 | 0 | if (fb.__isset.subcolumn_path && !fb.subcolumn_path.empty()) { |
486 | 0 | sub_path = fb.subcolumn_path; |
487 | 0 | } else if (fb.field_name.starts_with(nested_path + ".")) { |
488 | 0 | sub_path = fb.field_name.substr(nested_path.size() + 1); |
489 | 0 | } else if (fb.field_name.starts_with(root_prefix)) { |
490 | 0 | sub_path = fb.field_name.substr(root_prefix.size()); |
491 | 0 | } else { |
492 | 0 | sub_path = fb.field_name; |
493 | 0 | } |
494 | 0 | if (sub_path.empty()) { |
495 | 0 | continue; |
496 | 0 | } |
497 | 0 | const std::string array_prefix = array_path + "."; |
498 | 0 | const std::string suffix_path = |
499 | 0 | sub_path.starts_with(array_prefix) ? sub_path : (array_prefix + sub_path); |
500 | 0 | const std::string parent_uid = old_stored.substr(0, first_dot); |
501 | 0 | const std::string expected_stored = parent_uid + "." + suffix_path; |
502 | 0 | if (old_stored == expected_stored) { |
503 | 0 | continue; |
504 | 0 | } |
505 | | |
506 | 0 | if (!copied) { |
507 | 0 | patched_data_type_with_names = data_type_with_names; |
508 | 0 | effective_data_type_with_names = &patched_data_type_with_names; |
509 | 0 | copied = true; |
510 | 0 | } |
511 | 0 | auto it = patched_data_type_with_names.find(fb.field_name); |
512 | 0 | if (it == patched_data_type_with_names.end()) { |
513 | 0 | continue; |
514 | 0 | } |
515 | 0 | it->second.first = expected_stored; |
516 | 0 | } |
517 | 0 | } |
518 | | |
519 | | // Pass field_bindings to resolver for variant subcolumn detection |
520 | 1.07k | FieldReaderResolver resolver(*effective_data_type_with_names, iterators, context, |
521 | 1.07k | search_param.field_bindings); |
522 | | |
523 | 1.07k | if (is_nested_query) { |
524 | 0 | std::shared_ptr<roaring::Roaring> row_bitmap; |
525 | 0 | RETURN_IF_ERROR(evaluate_nested_query(search_param, search_param.root, context, resolver, |
526 | 0 | num_rows, index_exec_ctx, field_name_to_column_id, |
527 | 0 | row_bitmap)); |
528 | 0 | bitmap_result = InvertedIndexResultBitmap(std::move(row_bitmap), |
529 | 0 | std::make_shared<roaring::Roaring>()); |
530 | 0 | bitmap_result.mask_out_null(); |
531 | 0 | return Status::OK(); |
532 | 0 | } |
533 | | |
534 | | // Extract default_operator from TSearchParam (default: "or") |
535 | 1.07k | std::string default_operator = "or"; |
536 | 1.07k | if (search_param.__isset.default_operator && !search_param.default_operator.empty()) { |
537 | 1.04k | default_operator = search_param.default_operator; |
538 | 1.04k | } |
539 | | // Extract minimum_should_match from TSearchParam (-1 means not set) |
540 | 1.07k | int32_t minimum_should_match = -1; |
541 | 1.07k | if (search_param.__isset.minimum_should_match) { |
542 | 46 | minimum_should_match = search_param.minimum_should_match; |
543 | 46 | } |
544 | | |
545 | 1.07k | query_v2::QueryPtr root_query; |
546 | 1.07k | std::string root_binding_key; |
547 | 1.07k | RETURN_IF_ERROR(build_query_recursive(search_param.root, context, resolver, &root_query, |
548 | 1.07k | &root_binding_key, default_operator, |
549 | 1.07k | minimum_should_match)); |
550 | 1.05k | if (root_query == nullptr) { |
551 | 0 | LOG(INFO) << "search: Query tree resolved to empty query, dsl:" |
552 | 0 | << search_param.original_dsl; |
553 | 0 | bitmap_result = InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(), |
554 | 0 | std::make_shared<roaring::Roaring>()); |
555 | 0 | return Status::OK(); |
556 | 0 | } |
557 | | |
558 | 1.05k | ResolverNullBitmapAdapter null_resolver(resolver); |
559 | 1.05k | query_v2::QueryExecutionContext exec_ctx = |
560 | 1.05k | build_query_execution_context(num_rows, resolver, &null_resolver); |
561 | | |
562 | 1.05k | bool enable_scoring = false; |
563 | 1.05k | bool is_asc = false; |
564 | 1.05k | size_t top_k = 0; |
565 | 1.05k | if (index_query_context) { |
566 | 1.04k | enable_scoring = index_query_context->collection_similarity != nullptr; |
567 | 1.04k | is_asc = index_query_context->is_asc; |
568 | 1.04k | top_k = index_query_context->query_limit; |
569 | 1.04k | } |
570 | | |
571 | 1.05k | auto weight = root_query->weight(enable_scoring); |
572 | 1.05k | if (!weight) { |
573 | 0 | LOG(WARNING) << "search: Failed to build query weight"; |
574 | 0 | bitmap_result = InvertedIndexResultBitmap(std::make_shared<roaring::Roaring>(), |
575 | 0 | std::make_shared<roaring::Roaring>()); |
576 | 0 | return Status::OK(); |
577 | 0 | } |
578 | | |
579 | 1.05k | std::shared_ptr<roaring::Roaring> roaring = std::make_shared<roaring::Roaring>(); |
580 | 1.05k | if (enable_scoring && !is_asc && top_k > 0) { |
581 | 0 | bool use_wand = index_query_context->runtime_state != nullptr && |
582 | 0 | index_query_context->runtime_state->query_options() |
583 | 0 | .enable_inverted_index_wand_query; |
584 | 0 | query_v2::collect_multi_segment_top_k(weight, exec_ctx, root_binding_key, top_k, roaring, |
585 | 0 | index_query_context->collection_similarity, use_wand); |
586 | 1.05k | } else { |
587 | 1.05k | query_v2::collect_multi_segment_doc_set( |
588 | 1.05k | weight, exec_ctx, root_binding_key, roaring, |
589 | 1.05k | index_query_context ? index_query_context->collection_similarity : nullptr, |
590 | 1.05k | enable_scoring); |
591 | 1.05k | } |
592 | | |
593 | 18.4E | VLOG_DEBUG << "search: Query completed, matched " << roaring->cardinality() << " documents"; |
594 | | |
595 | | // Extract NULL bitmap from three-valued logic scorer |
596 | | // The scorer correctly computes which documents evaluate to NULL based on query logic |
597 | | // For example: TRUE OR NULL = TRUE (not NULL), FALSE OR NULL = NULL |
598 | 1.05k | std::shared_ptr<roaring::Roaring> null_bitmap = std::make_shared<roaring::Roaring>(); |
599 | 1.05k | if (exec_ctx.null_resolver) { |
600 | 1.05k | auto scorer = weight->scorer(exec_ctx, root_binding_key); |
601 | 1.05k | if (scorer && scorer->has_null_bitmap(exec_ctx.null_resolver)) { |
602 | 223 | const auto* bitmap = scorer->get_null_bitmap(exec_ctx.null_resolver); |
603 | 223 | if (bitmap != nullptr) { |
604 | 223 | *null_bitmap = *bitmap; |
605 | 18.4E | VLOG_TRACE << "search: Extracted NULL bitmap with " << null_bitmap->cardinality() |
606 | 18.4E | << " NULL documents"; |
607 | 223 | } |
608 | 223 | } |
609 | 1.05k | } |
610 | | |
611 | 18.4E | VLOG_TRACE << "search: Before mask - true_bitmap=" << roaring->cardinality() |
612 | 18.4E | << ", null_bitmap=" << null_bitmap->cardinality(); |
613 | | |
614 | | // Create result and mask out NULLs (SQL WHERE clause semantics: only TRUE rows) |
615 | 1.05k | bitmap_result = InvertedIndexResultBitmap(std::move(roaring), std::move(null_bitmap)); |
616 | 1.05k | bitmap_result.mask_out_null(); |
617 | | |
618 | 18.4E | VLOG_TRACE << "search: After mask - result_bitmap=" |
619 | 18.4E | << bitmap_result.get_data_bitmap()->cardinality(); |
620 | | |
621 | | // Insert post-mask_out_null result into DSL cache for future reuse |
622 | | // Cache both data bitmap and null bitmap so compound operators (NOT, OR, AND) |
623 | | // can apply correct three-valued SQL logic on cache hit |
624 | 1.05k | if (dsl_cache && cache_usable) { |
625 | 1.05k | InvertedIndexQueryCacheHandle insert_handle; |
626 | 1.05k | dsl_cache->insert(dsl_cache_key, bitmap_result.get_data_bitmap(), &insert_handle); |
627 | 1.05k | if (bitmap_result.get_null_bitmap()) { |
628 | 1.05k | auto null_cache_key = InvertedIndexQueryCache::CacheKey { |
629 | 1.05k | seg_prefix, "__search_dsl__", InvertedIndexQueryType::SEARCH_DSL_QUERY, |
630 | 1.05k | dsl_sig + "__null"}; |
631 | 1.05k | InvertedIndexQueryCacheHandle null_insert_handle; |
632 | 1.05k | dsl_cache->insert(null_cache_key, bitmap_result.get_null_bitmap(), &null_insert_handle); |
633 | 1.05k | } |
634 | 1.05k | } |
635 | | |
636 | 1.05k | return Status::OK(); |
637 | 1.05k | } |
638 | | |
639 | | Status FunctionSearch::evaluate_nested_query( |
640 | | const TSearchParam& search_param, const TSearchClause& nested_clause, |
641 | | const std::shared_ptr<IndexQueryContext>& context, FieldReaderResolver& resolver, |
642 | | uint32_t num_rows, const IndexExecContext* index_exec_ctx, |
643 | | const std::unordered_map<std::string, int>& field_name_to_column_id, |
644 | 7 | std::shared_ptr<roaring::Roaring>& result_bitmap) const { |
645 | 7 | (void)field_name_to_column_id; |
646 | 7 | if (!(nested_clause.__isset.nested_path)) { |
647 | 2 | return Status::InvalidArgument("NESTED clause missing nested_path"); |
648 | 2 | } |
649 | 5 | if (!(nested_clause.__isset.children) || nested_clause.children.empty()) { |
650 | 2 | return Status::InvalidArgument("NESTED clause missing inner query"); |
651 | 2 | } |
652 | 3 | if (result_bitmap == nullptr) { |
653 | 2 | result_bitmap = std::make_shared<roaring::Roaring>(); |
654 | 2 | } else { |
655 | 1 | *result_bitmap = roaring::Roaring(); |
656 | 1 | } |
657 | | |
658 | | // 1. Get the nested group chain directly |
659 | 3 | std::string root_field = nested_clause.nested_path; |
660 | 3 | auto dot_pos = nested_clause.nested_path.find('.'); |
661 | 3 | if (dot_pos != std::string::npos) { |
662 | 1 | root_field = nested_clause.nested_path.substr(0, dot_pos); |
663 | 1 | } |
664 | 3 | if (index_exec_ctx == nullptr || index_exec_ctx->segment() == nullptr) { |
665 | 3 | return Status::InvalidArgument("NESTED query requires IndexExecContext with valid segment"); |
666 | 3 | } |
667 | 0 | auto* segment = index_exec_ctx->segment(); |
668 | 0 | const int32_t ordinal = segment->tablet_schema()->field_index(root_field); |
669 | 0 | if (ordinal < 0) { |
670 | 0 | return Status::InvalidArgument("Column '{}' not found in tablet schema for nested query", |
671 | 0 | root_field); |
672 | 0 | } |
673 | 0 | const ColumnId column_id = static_cast<ColumnId>(ordinal); |
674 | |
|
675 | 0 | std::shared_ptr<segment_v2::ColumnReader> column_reader; |
676 | 0 | RETURN_IF_ERROR(segment->get_column_reader(segment->tablet_schema()->column(column_id), |
677 | 0 | &column_reader, |
678 | 0 | index_exec_ctx->column_iter_opts().stats)); |
679 | 0 | auto* variant_reader = dynamic_cast<segment_v2::VariantColumnReader*>(column_reader.get()); |
680 | 0 | if (variant_reader == nullptr) { |
681 | 0 | return Status::InvalidArgument("Column '{}' is not VARIANT for nested query", root_field); |
682 | 0 | } |
683 | | |
684 | 0 | std::string array_path; |
685 | 0 | if (dot_pos == std::string::npos) { |
686 | 0 | array_path = std::string(segment_v2::kRootNestedGroupPath); |
687 | 0 | } else { |
688 | 0 | array_path = nested_clause.nested_path.substr(dot_pos + 1); |
689 | 0 | } |
690 | |
|
691 | 0 | auto [found, group_chain, _] = variant_reader->collect_nested_group_chain(array_path); |
692 | 0 | if (!found || group_chain.empty()) { |
693 | 0 | return Status::OK(); |
694 | 0 | } |
695 | | |
696 | | // Use the read provider for element counting and bitmap mapping. |
697 | 0 | auto read_provider = segment_v2::create_nested_group_read_provider(); |
698 | 0 | if (!read_provider || !read_provider->should_enable_nested_group_read_path()) { |
699 | 0 | return Status::NotSupported( |
700 | 0 | "NestedGroup search is an enterprise capability, not available in this build"); |
701 | 0 | } |
702 | | |
703 | 0 | auto& leaf_group = group_chain.back(); |
704 | 0 | uint64_t total_elements = 0; |
705 | 0 | RETURN_IF_ERROR(read_provider->get_total_elements(index_exec_ctx->column_iter_opts(), |
706 | 0 | leaf_group, &total_elements)); |
707 | 0 | if (total_elements == 0) { |
708 | 0 | return Status::OK(); |
709 | 0 | } |
710 | | |
711 | | // 3. Evaluate inner query |
712 | 0 | std::string default_operator = "or"; |
713 | 0 | if (search_param.__isset.default_operator && !search_param.default_operator.empty()) { |
714 | 0 | default_operator = search_param.default_operator; |
715 | 0 | } |
716 | 0 | int32_t minimum_should_match = -1; |
717 | 0 | if (search_param.__isset.minimum_should_match) { |
718 | 0 | minimum_should_match = search_param.minimum_should_match; |
719 | 0 | } |
720 | |
|
721 | 0 | query_v2::QueryPtr inner_query; |
722 | 0 | std::string inner_binding_key; |
723 | 0 | RETURN_IF_ERROR(build_query_recursive(nested_clause.children[0], context, resolver, |
724 | 0 | &inner_query, &inner_binding_key, default_operator, |
725 | 0 | minimum_should_match)); |
726 | 0 | if (inner_query == nullptr) { |
727 | 0 | return Status::OK(); |
728 | 0 | } |
729 | | |
730 | 0 | if (total_elements > std::numeric_limits<uint32_t>::max()) { |
731 | 0 | return Status::InvalidArgument("nested element_count exceeds uint32_t max"); |
732 | 0 | } |
733 | | |
734 | 0 | ResolverNullBitmapAdapter null_resolver(resolver); |
735 | 0 | query_v2::QueryExecutionContext exec_ctx = build_query_execution_context( |
736 | 0 | static_cast<uint32_t>(total_elements), resolver, &null_resolver); |
737 | |
|
738 | 0 | auto weight = inner_query->weight(false); |
739 | 0 | if (!weight) { |
740 | 0 | return Status::OK(); |
741 | 0 | } |
742 | 0 | auto scorer = weight->scorer(exec_ctx, inner_binding_key); |
743 | 0 | if (!scorer) { |
744 | 0 | return Status::OK(); |
745 | 0 | } |
746 | | |
747 | 0 | roaring::Roaring element_bitmap; |
748 | 0 | uint32_t doc = scorer->doc(); |
749 | 0 | while (doc != query_v2::TERMINATED) { |
750 | 0 | element_bitmap.add(doc); |
751 | 0 | doc = scorer->advance(); |
752 | 0 | } |
753 | |
|
754 | 0 | if (scorer->has_null_bitmap(exec_ctx.null_resolver)) { |
755 | 0 | const auto* bitmap = scorer->get_null_bitmap(exec_ctx.null_resolver); |
756 | 0 | if (bitmap != nullptr && !bitmap->isEmpty()) { |
757 | 0 | element_bitmap -= *bitmap; |
758 | 0 | } |
759 | 0 | } |
760 | | |
761 | | // 4. Map element-level hits back to row-level hits through NestedGroup chain. |
762 | 0 | if (result_bitmap == nullptr) { |
763 | 0 | result_bitmap = std::make_shared<roaring::Roaring>(); |
764 | 0 | } |
765 | 0 | roaring::Roaring parent_bitmap; |
766 | 0 | RETURN_IF_ERROR(read_provider->map_elements_to_parent_ords( |
767 | 0 | group_chain, index_exec_ctx->column_iter_opts(), element_bitmap, &parent_bitmap)); |
768 | 0 | *result_bitmap = std::move(parent_bitmap); |
769 | 0 | return Status::OK(); |
770 | 0 | } |
771 | | |
772 | | // Aligned with FE QsClauseType enum - uses enum.name() as clause_type |
773 | | FunctionSearch::ClauseTypeCategory FunctionSearch::get_clause_type_category( |
774 | 7.16k | const std::string& clause_type) const { |
775 | 7.16k | if (clause_type == "AND" || clause_type == "OR" || clause_type == "NOT" || |
776 | 7.16k | clause_type == "OCCUR_BOOLEAN" || clause_type == "NESTED") { |
777 | 146 | return ClauseTypeCategory::COMPOUND; |
778 | 7.01k | } else if (clause_type == "TERM" || clause_type == "PREFIX" || clause_type == "WILDCARD" || |
779 | 7.01k | clause_type == "REGEXP" || clause_type == "RANGE" || clause_type == "LIST" || |
780 | 7.01k | clause_type == "EXACT") { |
781 | | // Non-tokenized queries: exact matching, pattern matching, range, list operations |
782 | 6.61k | return ClauseTypeCategory::NON_TOKENIZED; |
783 | 6.61k | } else if (clause_type == "PHRASE" || clause_type == "MATCH" || clause_type == "ANY" || |
784 | 424 | clause_type == "ALL") { |
785 | | // Tokenized queries: phrase search, full-text search, multi-value matching |
786 | | // Note: ANY and ALL require tokenization of their input values |
787 | 424 | return ClauseTypeCategory::TOKENIZED; |
788 | 18.4E | } else { |
789 | | // Default to NON_TOKENIZED for unknown types |
790 | 18.4E | LOG(WARNING) << "Unknown clause type '" << clause_type |
791 | 18.4E | << "', defaulting to NON_TOKENIZED category"; |
792 | 18.4E | return ClauseTypeCategory::NON_TOKENIZED; |
793 | 18.4E | } |
794 | 7.16k | } |
795 | | |
796 | | // Analyze query type for a specific field in the search clause |
797 | | InvertedIndexQueryType FunctionSearch::analyze_field_query_type(const std::string& field_name, |
798 | 5.28k | const TSearchClause& clause) const { |
799 | 5.28k | const std::string& clause_type = clause.clause_type; |
800 | 5.28k | ClauseTypeCategory category = get_clause_type_category(clause_type); |
801 | | |
802 | | // Handle leaf queries - use direct mapping |
803 | 5.28k | if (category != ClauseTypeCategory::COMPOUND) { |
804 | | // Check if this clause targets the specific field |
805 | 5.14k | if (clause.field_name == field_name) { |
806 | | // Use direct mapping from clause_type to InvertedIndexQueryType |
807 | 163 | return clause_type_to_query_type(clause_type); |
808 | 163 | } |
809 | 5.14k | } |
810 | | |
811 | | // Handle boolean queries - recursively analyze children |
812 | 5.11k | if (!clause.children.empty()) { |
813 | 5.09k | for (const auto& child_clause : clause.children) { |
814 | | // Recursively analyze each child |
815 | 5.09k | InvertedIndexQueryType child_type = analyze_field_query_type(field_name, child_clause); |
816 | | // If this child targets the field (not default EQUAL_QUERY), return its query type |
817 | 5.09k | if (child_type != InvertedIndexQueryType::UNKNOWN_QUERY) { |
818 | 124 | return child_type; |
819 | 124 | } |
820 | 5.09k | } |
821 | 132 | } |
822 | | |
823 | | // If no children target this field, return UNKNOWN_QUERY as default |
824 | 4.99k | return InvertedIndexQueryType::UNKNOWN_QUERY; |
825 | 5.11k | } |
826 | | |
827 | | // Map clause_type string to InvertedIndexQueryType |
828 | | InvertedIndexQueryType FunctionSearch::clause_type_to_query_type( |
829 | 2.08k | const std::string& clause_type) const { |
830 | | // Use static map for better performance and maintainability |
831 | 2.08k | static const std::unordered_map<std::string, InvertedIndexQueryType> clause_type_map = { |
832 | | // Boolean operations |
833 | 2.08k | {"AND", InvertedIndexQueryType::BOOLEAN_QUERY}, |
834 | 2.08k | {"OR", InvertedIndexQueryType::BOOLEAN_QUERY}, |
835 | 2.08k | {"NOT", InvertedIndexQueryType::BOOLEAN_QUERY}, |
836 | 2.08k | {"OCCUR_BOOLEAN", InvertedIndexQueryType::BOOLEAN_QUERY}, |
837 | 2.08k | {"NESTED", InvertedIndexQueryType::BOOLEAN_QUERY}, |
838 | | |
839 | | // Non-tokenized queries (exact matching, pattern matching) |
840 | 2.08k | {"TERM", InvertedIndexQueryType::EQUAL_QUERY}, |
841 | 2.08k | {"PREFIX", InvertedIndexQueryType::MATCH_PHRASE_PREFIX_QUERY}, |
842 | 2.08k | {"WILDCARD", InvertedIndexQueryType::WILDCARD_QUERY}, |
843 | 2.08k | {"REGEXP", InvertedIndexQueryType::MATCH_REGEXP_QUERY}, |
844 | 2.08k | {"RANGE", InvertedIndexQueryType::RANGE_QUERY}, |
845 | 2.08k | {"LIST", InvertedIndexQueryType::LIST_QUERY}, |
846 | | |
847 | | // Tokenized queries (full-text search, phrase search) |
848 | 2.08k | {"PHRASE", InvertedIndexQueryType::MATCH_PHRASE_QUERY}, |
849 | 2.08k | {"MATCH", InvertedIndexQueryType::MATCH_ANY_QUERY}, |
850 | 2.08k | {"ANY", InvertedIndexQueryType::MATCH_ANY_QUERY}, |
851 | 2.08k | {"ALL", InvertedIndexQueryType::MATCH_ALL_QUERY}, |
852 | | |
853 | | // Exact match without tokenization |
854 | 2.08k | {"EXACT", InvertedIndexQueryType::EQUAL_QUERY}, |
855 | 2.08k | }; |
856 | | |
857 | 2.08k | auto it = clause_type_map.find(clause_type); |
858 | 2.08k | if (it != clause_type_map.end()) { |
859 | 2.07k | return it->second; |
860 | 2.07k | } |
861 | | |
862 | | // Unknown clause type |
863 | 2.08k | LOG(WARNING) << "Unknown clause type '" << clause_type << "', defaulting to EQUAL_QUERY"; |
864 | 8 | return InvertedIndexQueryType::EQUAL_QUERY; |
865 | 2.08k | } |
866 | | |
867 | | // Map Thrift TSearchOccur to query_v2::Occur |
868 | 834 | static query_v2::Occur map_thrift_occur(TSearchOccur::type thrift_occur) { |
869 | 834 | switch (thrift_occur) { |
870 | 289 | case TSearchOccur::MUST: |
871 | 289 | return query_v2::Occur::MUST; |
872 | 481 | case TSearchOccur::SHOULD: |
873 | 481 | return query_v2::Occur::SHOULD; |
874 | 65 | case TSearchOccur::MUST_NOT: |
875 | 65 | return query_v2::Occur::MUST_NOT; |
876 | 0 | default: |
877 | 0 | return query_v2::Occur::MUST; |
878 | 834 | } |
879 | 834 | } |
880 | | |
881 | | Status FunctionSearch::build_query_recursive(const TSearchClause& clause, |
882 | | const std::shared_ptr<IndexQueryContext>& context, |
883 | | FieldReaderResolver& resolver, |
884 | | inverted_index::query_v2::QueryPtr* out, |
885 | | std::string* binding_key, |
886 | | const std::string& default_operator, |
887 | 2.68k | int32_t minimum_should_match) const { |
888 | 2.68k | DCHECK(out != nullptr); |
889 | 2.68k | *out = nullptr; |
890 | 2.70k | if (binding_key) { |
891 | 2.70k | binding_key->clear(); |
892 | 2.70k | } |
893 | | |
894 | 2.68k | const std::string& clause_type = clause.clause_type; |
895 | | |
896 | | // Handle MATCH_ALL_DOCS - matches all documents in the segment |
897 | 2.68k | if (clause_type == "MATCH_ALL_DOCS") { |
898 | 41 | *out = std::make_shared<query_v2::AllQuery>(); |
899 | 41 | return Status::OK(); |
900 | 41 | } |
901 | | |
902 | | // Handle OCCUR_BOOLEAN - Lucene-style boolean query with MUST/SHOULD/MUST_NOT |
903 | 2.63k | if (clause_type == "OCCUR_BOOLEAN") { |
904 | 407 | auto builder = segment_v2::inverted_index::query_v2::create_occur_boolean_query_builder(); |
905 | | |
906 | | // Set minimum_should_match if specified |
907 | 407 | if (clause.__isset.minimum_should_match) { |
908 | 396 | builder->set_minimum_number_should_match(clause.minimum_should_match); |
909 | 396 | } |
910 | | |
911 | 410 | if (clause.__isset.children) { |
912 | 844 | for (const auto& child_clause : clause.children) { |
913 | 844 | query_v2::QueryPtr child_query; |
914 | 844 | std::string child_binding_key; |
915 | 844 | RETURN_IF_ERROR(build_query_recursive(child_clause, context, resolver, &child_query, |
916 | 844 | &child_binding_key, default_operator, |
917 | 844 | minimum_should_match)); |
918 | | |
919 | | // Determine occur type from child clause |
920 | 843 | query_v2::Occur occur = query_v2::Occur::MUST; // default |
921 | 843 | if (child_clause.__isset.occur) { |
922 | 838 | occur = map_thrift_occur(child_clause.occur); |
923 | 838 | } |
924 | | |
925 | 843 | builder->add(child_query, occur, std::move(child_binding_key)); |
926 | 843 | } |
927 | 410 | } |
928 | | |
929 | 406 | *out = builder->build(); |
930 | 406 | return Status::OK(); |
931 | 407 | } |
932 | | |
933 | 2.23k | if (clause_type == "NESTED") { |
934 | 1 | return Status::InvalidArgument("NESTED clause must be evaluated at top level"); |
935 | 1 | } |
936 | | |
937 | | // Handle standard boolean operators (AND/OR/NOT) |
938 | 2.23k | if (clause_type == "AND" || clause_type == "OR" || clause_type == "NOT") { |
939 | 381 | query_v2::OperatorType op = query_v2::OperatorType::OP_AND; |
940 | 381 | if (clause_type == "OR") { |
941 | 197 | op = query_v2::OperatorType::OP_OR; |
942 | 197 | } else if (clause_type == "NOT") { |
943 | 89 | op = query_v2::OperatorType::OP_NOT; |
944 | 89 | } |
945 | | |
946 | 381 | auto builder = create_operator_boolean_query_builder(op); |
947 | 381 | if (clause.__isset.children) { |
948 | 792 | for (const auto& child_clause : clause.children) { |
949 | 792 | query_v2::QueryPtr child_query; |
950 | 792 | std::string child_binding_key; |
951 | 792 | RETURN_IF_ERROR(build_query_recursive(child_clause, context, resolver, &child_query, |
952 | 792 | &child_binding_key, default_operator, |
953 | 792 | minimum_should_match)); |
954 | | // Add all children including empty BitSetQuery |
955 | | // BooleanQuery will handle the logic: |
956 | | // - AND with empty bitmap → result is empty |
957 | | // - OR with empty bitmap → empty bitmap is ignored by OR logic |
958 | | // - NOT with empty bitmap → NOT(empty) = all rows (handled by BooleanQuery) |
959 | 791 | builder->add(child_query, std::move(child_binding_key)); |
960 | 791 | } |
961 | 373 | } |
962 | | |
963 | 380 | *out = builder->build(); |
964 | 380 | return Status::OK(); |
965 | 381 | } |
966 | | |
967 | 1.85k | return build_leaf_query(clause, context, resolver, out, binding_key, default_operator, |
968 | 1.85k | minimum_should_match); |
969 | 2.23k | } |
970 | | |
971 | | Status FunctionSearch::build_leaf_query(const TSearchClause& clause, |
972 | | const std::shared_ptr<IndexQueryContext>& context, |
973 | | FieldReaderResolver& resolver, |
974 | | inverted_index::query_v2::QueryPtr* out, |
975 | | std::string* binding_key, |
976 | | const std::string& default_operator, |
977 | 1.89k | int32_t minimum_should_match) const { |
978 | 1.89k | DCHECK(out != nullptr); |
979 | 1.89k | *out = nullptr; |
980 | 1.89k | if (binding_key) { |
981 | 1.88k | binding_key->clear(); |
982 | 1.88k | } |
983 | | |
984 | 1.89k | if (!clause.__isset.field_name || !clause.__isset.value) { |
985 | 0 | return Status::InvalidArgument("search clause missing field_name or value"); |
986 | 0 | } |
987 | | |
988 | 1.89k | const std::string& field_name = clause.field_name; |
989 | 1.89k | const std::string& value = clause.value; |
990 | 1.89k | const std::string& clause_type = clause.clause_type; |
991 | | |
992 | 1.89k | auto query_type = clause_type_to_query_type(clause_type); |
993 | | // TERM, WILDCARD, PREFIX, and REGEXP in search DSL operate on individual index terms |
994 | | // (like Lucene TermQuery, WildcardQuery, PrefixQuery, RegexpQuery). |
995 | | // Override to MATCH_ANY_QUERY so select_best_reader() prefers the FULLTEXT reader |
996 | | // when multiple indexes exist on the same column (one tokenized, one untokenized). |
997 | | // Without this, these queries would select the untokenized index and try to match |
998 | | // patterns like "h*llo" against full strings ("hello world") instead of individual |
999 | | // tokens ("hello"), returning empty results. |
1000 | | // EXACT must remain EQUAL_QUERY to prefer the untokenized STRING_TYPE reader. |
1001 | | // |
1002 | | // Safe for single-index columns: select_best_reader() has a single-reader fast path |
1003 | | // that returns the only reader directly, bypassing the query_type preference logic. |
1004 | 1.89k | if (clause_type == "TERM" || clause_type == "WILDCARD" || clause_type == "PREFIX" || |
1005 | 1.89k | clause_type == "REGEXP") { |
1006 | 1.39k | query_type = InvertedIndexQueryType::MATCH_ANY_QUERY; |
1007 | 1.39k | } |
1008 | | |
1009 | 1.89k | FieldReaderBinding binding; |
1010 | 1.89k | RETURN_IF_ERROR(resolver.resolve(field_name, query_type, &binding)); |
1011 | | |
1012 | | // Check if binding is empty (variant subcolumn not found in this segment) |
1013 | 1.87k | if (binding.lucene_reader == nullptr) { |
1014 | 3 | LOG(INFO) << "search: No inverted index for field '" << field_name |
1015 | 3 | << "' in this segment, clause_type='" << clause_type |
1016 | 3 | << "', query_type=" << static_cast<int>(query_type) << ", returning no matches"; |
1017 | | // Variant subcolumn doesn't exist - create empty BitSetQuery (no matches) |
1018 | 3 | *out = std::make_shared<query_v2::BitSetQuery>(roaring::Roaring()); |
1019 | 3 | if (binding_key) { |
1020 | 3 | binding_key->clear(); |
1021 | 3 | } |
1022 | 3 | return Status::OK(); |
1023 | 3 | } |
1024 | | |
1025 | 1.86k | if (binding_key) { |
1026 | 1.85k | *binding_key = binding.binding_key; |
1027 | 1.85k | } |
1028 | | |
1029 | 1.86k | FunctionSearch::ClauseTypeCategory category = get_clause_type_category(clause_type); |
1030 | 1.86k | std::wstring field_wstr = binding.stored_field_wstr; |
1031 | 1.86k | std::wstring value_wstr = StringHelper::to_wstring(value); |
1032 | | |
1033 | 1.87k | auto make_term_query = [&](const std::wstring& term) -> query_v2::QueryPtr { |
1034 | 1.87k | return std::make_shared<query_v2::TermQuery>(context, field_wstr, term); |
1035 | 1.87k | }; |
1036 | | |
1037 | 1.86k | if (clause_type == "TERM") { |
1038 | 1.28k | bool should_analyze = |
1039 | 1.28k | inverted_index::InvertedIndexAnalyzer::should_analyzer(binding.index_properties); |
1040 | 1.28k | if (should_analyze) { |
1041 | 1.04k | if (binding.index_properties.empty()) { |
1042 | 0 | LOG(WARNING) << "search: analyzer required but index properties empty for field '" |
1043 | 0 | << field_name << "'"; |
1044 | 0 | *out = make_term_query(value_wstr); |
1045 | 0 | return Status::OK(); |
1046 | 0 | } |
1047 | | |
1048 | 1.04k | std::vector<TermInfo> term_infos = |
1049 | 1.04k | inverted_index::InvertedIndexAnalyzer::get_analyse_result( |
1050 | 1.04k | value, binding.index_properties); |
1051 | 1.04k | if (term_infos.empty()) { |
1052 | 0 | LOG(WARNING) << "search: No terms found after tokenization for TERM query, field=" |
1053 | 0 | << field_name << ", value='" << value |
1054 | 0 | << "', returning empty BitSetQuery"; |
1055 | 0 | *out = std::make_shared<query_v2::BitSetQuery>(roaring::Roaring()); |
1056 | 0 | return Status::OK(); |
1057 | 0 | } |
1058 | | |
1059 | 1.04k | if (term_infos.size() == 1) { |
1060 | 1.03k | std::wstring term_wstr = StringHelper::to_wstring(term_infos[0].get_single_term()); |
1061 | 1.03k | *out = make_term_query(term_wstr); |
1062 | 1.03k | return Status::OK(); |
1063 | 1.03k | } |
1064 | | |
1065 | | // When minimum_should_match is specified, use OccurBooleanQuery |
1066 | | // ES behavior: msm only applies to SHOULD clauses |
1067 | 8 | if (minimum_should_match > 0) { |
1068 | 0 | auto builder = |
1069 | 0 | segment_v2::inverted_index::query_v2::create_occur_boolean_query_builder(); |
1070 | 0 | builder->set_minimum_number_should_match(minimum_should_match); |
1071 | 0 | query_v2::Occur occur = (default_operator == "and") ? query_v2::Occur::MUST |
1072 | 0 | : query_v2::Occur::SHOULD; |
1073 | 0 | for (const auto& term_info : term_infos) { |
1074 | 0 | std::wstring term_wstr = StringHelper::to_wstring(term_info.get_single_term()); |
1075 | 0 | builder->add(make_term_query(term_wstr), occur); |
1076 | 0 | } |
1077 | 0 | *out = builder->build(); |
1078 | 0 | return Status::OK(); |
1079 | 0 | } |
1080 | | |
1081 | | // Use default_operator to determine how to combine tokenized terms |
1082 | 8 | query_v2::OperatorType op_type = (default_operator == "and") |
1083 | 8 | ? query_v2::OperatorType::OP_AND |
1084 | 8 | : query_v2::OperatorType::OP_OR; |
1085 | 8 | auto builder = create_operator_boolean_query_builder(op_type); |
1086 | 8 | for (const auto& term_info : term_infos) { |
1087 | 8 | std::wstring term_wstr = StringHelper::to_wstring(term_info.get_single_term()); |
1088 | 8 | builder->add(make_term_query(term_wstr), binding.binding_key); |
1089 | 8 | } |
1090 | | |
1091 | 8 | *out = builder->build(); |
1092 | 8 | return Status::OK(); |
1093 | 8 | } |
1094 | | |
1095 | 245 | *out = make_term_query(value_wstr); |
1096 | 245 | return Status::OK(); |
1097 | 1.28k | } |
1098 | | |
1099 | 582 | if (category == FunctionSearch::ClauseTypeCategory::TOKENIZED) { |
1100 | 395 | if (clause_type == "PHRASE") { |
1101 | 123 | bool should_analyze = inverted_index::InvertedIndexAnalyzer::should_analyzer( |
1102 | 123 | binding.index_properties); |
1103 | 123 | if (!should_analyze) { |
1104 | 11 | VLOG_DEBUG << "search: PHRASE on non-tokenized field '" << field_name |
1105 | 0 | << "', falling back to TERM"; |
1106 | 11 | *out = make_term_query(value_wstr); |
1107 | 11 | return Status::OK(); |
1108 | 11 | } |
1109 | | |
1110 | 112 | if (binding.index_properties.empty()) { |
1111 | 0 | LOG(WARNING) << "search: analyzer required but index properties empty for PHRASE " |
1112 | 0 | "query on field '" |
1113 | 0 | << field_name << "'"; |
1114 | 0 | *out = make_term_query(value_wstr); |
1115 | 0 | return Status::OK(); |
1116 | 0 | } |
1117 | | |
1118 | 112 | std::vector<TermInfo> term_infos = |
1119 | 112 | inverted_index::InvertedIndexAnalyzer::get_analyse_result( |
1120 | 112 | value, binding.index_properties); |
1121 | 112 | if (term_infos.empty()) { |
1122 | 8 | LOG(WARNING) << "search: No terms found after tokenization for PHRASE query, field=" |
1123 | 8 | << field_name << ", value='" << value |
1124 | 8 | << "', returning empty BitSetQuery"; |
1125 | 8 | *out = std::make_shared<query_v2::BitSetQuery>(roaring::Roaring()); |
1126 | 8 | return Status::OK(); |
1127 | 8 | } |
1128 | | |
1129 | 104 | std::vector<TermInfo> phrase_term_infos = |
1130 | 104 | QueryHelper::build_phrase_term_infos(term_infos); |
1131 | 104 | if (phrase_term_infos.size() == 1) { |
1132 | 63 | const auto& term_info = phrase_term_infos[0]; |
1133 | 63 | if (term_info.is_single_term()) { |
1134 | 63 | std::wstring term_wstr = StringHelper::to_wstring(term_info.get_single_term()); |
1135 | 63 | *out = std::make_shared<query_v2::TermQuery>(context, field_wstr, term_wstr); |
1136 | 63 | } else { |
1137 | 0 | auto builder = |
1138 | 0 | create_operator_boolean_query_builder(query_v2::OperatorType::OP_OR); |
1139 | 0 | for (const auto& term : term_info.get_multi_terms()) { |
1140 | 0 | std::wstring term_wstr = StringHelper::to_wstring(term); |
1141 | 0 | builder->add(make_term_query(term_wstr), binding.binding_key); |
1142 | 0 | } |
1143 | 0 | *out = builder->build(); |
1144 | 0 | } |
1145 | 63 | } else { |
1146 | 41 | if (QueryHelper::is_simple_phrase(phrase_term_infos)) { |
1147 | 21 | *out = std::make_shared<query_v2::PhraseQuery>(context, field_wstr, |
1148 | 21 | phrase_term_infos); |
1149 | 21 | } else { |
1150 | 20 | *out = std::make_shared<query_v2::MultiPhraseQuery>(context, field_wstr, |
1151 | 20 | phrase_term_infos); |
1152 | 20 | } |
1153 | 41 | } |
1154 | | |
1155 | 104 | return Status::OK(); |
1156 | 112 | } |
1157 | 272 | if (clause_type == "MATCH") { |
1158 | 0 | VLOG_DEBUG << "search: MATCH clause not implemented, fallback to TERM"; |
1159 | 0 | *out = make_term_query(value_wstr); |
1160 | 0 | return Status::OK(); |
1161 | 0 | } |
1162 | | |
1163 | 275 | if (clause_type == "ANY" || clause_type == "ALL") { |
1164 | 275 | bool should_analyze = inverted_index::InvertedIndexAnalyzer::should_analyzer( |
1165 | 275 | binding.index_properties); |
1166 | 275 | if (!should_analyze) { |
1167 | 1 | *out = make_term_query(value_wstr); |
1168 | 1 | return Status::OK(); |
1169 | 1 | } |
1170 | | |
1171 | 274 | if (binding.index_properties.empty()) { |
1172 | 0 | LOG(WARNING) << "search: index properties empty for tokenized clause '" |
1173 | 0 | << clause_type << "' field=" << field_name; |
1174 | 0 | *out = make_term_query(value_wstr); |
1175 | 0 | return Status::OK(); |
1176 | 0 | } |
1177 | | |
1178 | 274 | std::vector<TermInfo> term_infos = |
1179 | 274 | inverted_index::InvertedIndexAnalyzer::get_analyse_result( |
1180 | 274 | value, binding.index_properties); |
1181 | 274 | if (term_infos.empty()) { |
1182 | 0 | LOG(WARNING) << "search: tokenization yielded no terms for clause '" << clause_type |
1183 | 0 | << "', field=" << field_name << ", returning empty BitSetQuery"; |
1184 | 0 | *out = std::make_shared<query_v2::BitSetQuery>(roaring::Roaring()); |
1185 | 0 | return Status::OK(); |
1186 | 0 | } |
1187 | | |
1188 | 274 | query_v2::OperatorType bool_type = query_v2::OperatorType::OP_OR; |
1189 | 274 | if (clause_type == "ALL") { |
1190 | 184 | bool_type = query_v2::OperatorType::OP_AND; |
1191 | 184 | } |
1192 | | |
1193 | 274 | if (term_infos.size() == 1) { |
1194 | 105 | std::wstring term_wstr = StringHelper::to_wstring(term_infos[0].get_single_term()); |
1195 | 105 | *out = make_term_query(term_wstr); |
1196 | 105 | return Status::OK(); |
1197 | 105 | } |
1198 | | |
1199 | 169 | auto builder = create_operator_boolean_query_builder(bool_type); |
1200 | 383 | for (const auto& term_info : term_infos) { |
1201 | 383 | std::wstring term_wstr = StringHelper::to_wstring(term_info.get_single_term()); |
1202 | 383 | builder->add(make_term_query(term_wstr), binding.binding_key); |
1203 | 383 | } |
1204 | 169 | *out = builder->build(); |
1205 | 169 | return Status::OK(); |
1206 | 274 | } |
1207 | | |
1208 | | // Default tokenized clause fallback |
1209 | 18.4E | *out = make_term_query(value_wstr); |
1210 | 18.4E | return Status::OK(); |
1211 | 272 | } |
1212 | | |
1213 | 187 | if (category == FunctionSearch::ClauseTypeCategory::NON_TOKENIZED) { |
1214 | 172 | if (clause_type == "EXACT") { |
1215 | | // EXACT match: exact string matching without tokenization |
1216 | | // Note: EXACT prefers untokenized index (STRING_TYPE) which doesn't support lowercase |
1217 | | // If only tokenized index exists, EXACT may return empty results because |
1218 | | // tokenized indexes store individual tokens, not complete strings |
1219 | 81 | *out = make_term_query(value_wstr); |
1220 | 18.4E | VLOG_DEBUG << "search: EXACT clause processed, field=" << field_name << ", value='" |
1221 | 18.4E | << value << "'"; |
1222 | 81 | return Status::OK(); |
1223 | 81 | } |
1224 | 91 | if (clause_type == "PREFIX") { |
1225 | | // Apply lowercase only if: |
1226 | | // 1. There's a parser/analyzer (otherwise lower_case has no effect on indexing) |
1227 | | // 2. lower_case is explicitly set to "true" |
1228 | 36 | bool has_parser = inverted_index::InvertedIndexAnalyzer::should_analyzer( |
1229 | 36 | binding.index_properties); |
1230 | 36 | std::string lowercase_setting = |
1231 | 36 | get_parser_lowercase_from_properties(binding.index_properties); |
1232 | 36 | bool should_lowercase = has_parser && (lowercase_setting == INVERTED_INDEX_PARSER_TRUE); |
1233 | 36 | std::string pattern = should_lowercase ? to_lower(value) : value; |
1234 | 36 | *out = std::make_shared<query_v2::WildcardQuery>(context, field_wstr, pattern); |
1235 | 36 | VLOG_DEBUG << "search: PREFIX clause processed, field=" << field_name << ", pattern='" |
1236 | 0 | << pattern << "' (original='" << value << "', has_parser=" << has_parser |
1237 | 0 | << ", lower_case=" << lowercase_setting << ")"; |
1238 | 36 | return Status::OK(); |
1239 | 36 | } |
1240 | | |
1241 | 55 | if (clause_type == "WILDCARD") { |
1242 | | // Standalone wildcard "*" matches all non-null values for this field |
1243 | | // Consistent with ES query_string behavior where field:* becomes FieldExistsQuery |
1244 | 23 | if (value == "*") { |
1245 | 0 | *out = std::make_shared<query_v2::AllQuery>(field_wstr, true); |
1246 | 0 | VLOG_DEBUG << "search: WILDCARD '*' converted to AllQuery(nullable=true), field=" |
1247 | 0 | << field_name; |
1248 | 0 | return Status::OK(); |
1249 | 0 | } |
1250 | | // Apply lowercase only if: |
1251 | | // 1. There's a parser/analyzer (otherwise lower_case has no effect on indexing) |
1252 | | // 2. lower_case is explicitly set to "true" |
1253 | 23 | bool has_parser = inverted_index::InvertedIndexAnalyzer::should_analyzer( |
1254 | 23 | binding.index_properties); |
1255 | 23 | std::string lowercase_setting = |
1256 | 23 | get_parser_lowercase_from_properties(binding.index_properties); |
1257 | 23 | bool should_lowercase = has_parser && (lowercase_setting == INVERTED_INDEX_PARSER_TRUE); |
1258 | 23 | std::string pattern = should_lowercase ? to_lower(value) : value; |
1259 | 23 | *out = std::make_shared<query_v2::WildcardQuery>(context, field_wstr, pattern); |
1260 | 23 | VLOG_DEBUG << "search: WILDCARD clause processed, field=" << field_name << ", pattern='" |
1261 | 0 | << pattern << "' (original='" << value << "', has_parser=" << has_parser |
1262 | 0 | << ", lower_case=" << lowercase_setting << ")"; |
1263 | 23 | return Status::OK(); |
1264 | 23 | } |
1265 | | |
1266 | 32 | if (clause_type == "REGEXP") { |
1267 | | // ES-compatible: regex patterns are NOT lowercased (case-sensitive matching) |
1268 | | // This matches ES query_string behavior where regex patterns bypass analysis |
1269 | 28 | *out = std::make_shared<query_v2::RegexpQuery>(context, field_wstr, value); |
1270 | 28 | VLOG_DEBUG << "search: REGEXP clause processed, field=" << field_name << ", pattern='" |
1271 | 0 | << value << "'"; |
1272 | 28 | return Status::OK(); |
1273 | 28 | } |
1274 | | |
1275 | 4 | if (clause_type == "RANGE" || clause_type == "LIST") { |
1276 | 3 | VLOG_DEBUG << "search: clause type '" << clause_type |
1277 | 0 | << "' not implemented, fallback to TERM"; |
1278 | 3 | } |
1279 | 4 | *out = make_term_query(value_wstr); |
1280 | 4 | return Status::OK(); |
1281 | 32 | } |
1282 | | |
1283 | 187 | LOG(WARNING) << "search: Unexpected clause type '" << clause_type << "', using TERM fallback"; |
1284 | 15 | *out = make_term_query(value_wstr); |
1285 | 15 | return Status::OK(); |
1286 | 187 | } |
1287 | | |
1288 | 6 | void register_function_search(SimpleFunctionFactory& factory) { |
1289 | 6 | factory.register_function<FunctionSearch>(); |
1290 | 6 | } |
1291 | | |
1292 | | } // namespace doris |