airborne12 commented on code in PR #29444: URL: https://github.com/apache/doris/pull/29444#discussion_r1440024337
########## be/src/olap/rowset/segment_v2/inverted_index/query/phrase_query.cpp: ########## @@ -0,0 +1,180 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "phrase_query.h" + +namespace doris::segment_v2 { + +PhraseQuery::~PhraseQuery() { + for (auto& term_doc : _term_docs) { + if (term_doc) { + _CLDELETE(term_doc); + } + } + for (auto& term : _terms) { + if (term) { + _CLDELETE(term); + } + } +} + +void PhraseQuery::add(const std::wstring& field_name, const std::vector<std::string>& terms) { + if (terms.empty()) { + _CLTHROWA(CL_ERR_IllegalArgument, "PhraseQuery::add: terms empty"); + } + + std::vector<TermIterator> iterators; + for (size_t i = 0; i < terms.size(); i++) { + std::wstring ws_term = StringUtil::string_to_wstring(terms[i]); + Term* t = _CLNEW Term(field_name.c_str(), ws_term.c_str()); + _terms.push_back(t); + TermPositions* term_pos = _searcher->getReader()->termPositions(t); + _term_docs.push_back(term_pos); + iterators.emplace_back(term_pos); + _postings.push_back(PostingsAndPosition(term_pos, i)); + } + + std::sort(iterators.begin(), iterators.end(), [](const TermIterator& a, const TermIterator& b) { + return a.docFreq() < b.docFreq(); + }); + + if (iterators.size() == 1) { + _lead1 = iterators[0]; + } else { + _lead1 = iterators[0]; + _lead2 = iterators[1]; + for (int32_t i = 2; i < _terms.size(); i++) { + _others.push_back(iterators[i]); + } + } +} + +void PhraseQuery::search(roaring::Roaring& roaring) { + if (_lead1.isEmpty()) { + return; + } + search_by_skiplist(roaring); +} + +void PhraseQuery::search_by_skiplist(roaring::Roaring& roaring) { + int32_t doc = 0; + while ((doc = do_next(_lead1.nextDoc())) != INT32_MAX) { + reset(); + if (nextMatch()) { + roaring.add(doc); + } + } +} + +int32_t PhraseQuery::do_next(int32_t doc) { + while (true) { + assert(doc == _lead1.docID()); + + // the skip list is used to find the two smallest inverted lists + int32_t next2 = _lead2.advance(doc); + if (next2 != doc) { + doc = _lead1.advance(next2); + if (next2 != doc) { + continue; + } + } + + // if both lead1 and lead2 exist, use skip list to lookup other inverted indexes + bool advance_head = false; + for (auto& other : _others) { + if (other.isEmpty()) { + continue; + } + + if (other.docID() < doc) { + int32_t next = other.advance(doc); + if (next > doc) { + doc = _lead1.advance(next); + advance_head = true; + break; + } + } + } + if (advance_head) { + continue; + } + + return doc; + } +} + +bool PhraseQuery::nextMatch() { + PostingsAndPosition& lead = _postings[0]; + if (lead._upTo < lead._freq) { + lead._pos = lead._postings.nextPosition(); + lead._upTo += 1; + } else { + return false; + } + + while (true) { + int32_t phrasePos = lead._pos - lead._offset; + + bool advance_head = false; + for (size_t j = 1; j < _postings.size(); ++j) { + PostingsAndPosition& posting = _postings[j]; + int32_t expectedPos = phrasePos + posting._offset; + // advance up to the same position as the lead + if (advancePosition(posting, expectedPos) == false) { + return false; + } + + if (posting._pos != expectedPos) { // we advanced too far + if (advancePosition(lead, posting._pos - posting._offset + lead._offset)) { + advance_head = true; + break; + } else { + return false; + } + } + } + if (advance_head) { + continue; + } + + return true; + } + + return false; +} + +bool PhraseQuery::advancePosition(PostingsAndPosition& posting, int32_t target) { Review Comment: adapt to doris code style for function name -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
