From d58a6e613be02bd200db0566d040cc89d3df8f97 Mon Sep 17 00:00:00 2001 From: kishorenc Date: Thu, 14 Nov 2019 11:06:57 +0530 Subject: [PATCH] Handle edge case of finding no query tokens in the array. --- src/collection.cpp | 23 ++++++++++++++--------- src/index.cpp | 6 +++--- 2 files changed, 17 insertions(+), 12 deletions(-) diff --git a/src/collection.cpp b/src/collection.cpp index 1041d2f3..5f1fca1b 100644 --- a/src/collection.cpp +++ b/src/collection.cpp @@ -738,25 +738,25 @@ void Collection::highlight_result(const field &search_field, // is from the best matched field and need not be present in other fields of a document. Index* index = indices[field_order_kv.key % num_indices]; art_leaf *actual_leaf = index->get_token_leaf(search_field.name, &token_leaf->key[0], token_leaf->key_len); - if(actual_leaf != NULL) { + if(actual_leaf != nullptr) { query_suggestion.push_back(actual_leaf); std::vector positions; uint32_t doc_index = actual_leaf->values->ids.indexOf(field_order_kv.key); - uint32_t *indices = new uint32_t[1]; - indices[0] = doc_index; - leaf_to_indices.emplace(actual_leaf, indices); + auto doc_indices = new uint32_t[1]; + doc_indices[0] = doc_index; + leaf_to_indices.emplace(actual_leaf, doc_indices); } } + if(query_suggestion.empty()) { + // none of the tokens from the query were found on this field + return ; + } + // positions in the field of each token in the query std::vector>> array_token_positions; Index::populate_token_positions(query_suggestion, leaf_to_indices, 0, array_token_positions); - if(array_token_positions.size() == 0) { - // none of the tokens from the query were found on this field - return ; - } - std::vector match_indices; for(size_t array_index = 0; array_index < array_token_positions.size(); array_index++) { @@ -771,6 +771,11 @@ void Collection::highlight_result(const field &search_field, match_indices.push_back(match_index_t(this_match, this_match_score, array_index)); } + if(match_indices.empty()) { + // none of the tokens from the query were found on this field + return ; + } + const size_t max_array_matches = std::min((size_t)MAX_ARRAY_MATCHES, match_indices.size()); std::partial_sort(match_indices.begin(), match_indices.begin()+max_array_matches, match_indices.end()); diff --git a/src/index.cpp b/src/index.cpp index 6a5b5c42..7492e36c 100644 --- a/src/index.cpp +++ b/src/index.cpp @@ -859,7 +859,7 @@ void Index::collate_curated_ids(const std::string & query, const std::string & f uint64_t match_score = 0; for(const std::vector> & token_positions: array_token_positions) { - if(token_positions.size() == 0) { + if(token_positions.empty()) { continue; } const Match & match = Match::match(seq_id, token_positions); @@ -1228,7 +1228,7 @@ void Index::score_results(const std::vector & sort_fields, const uint16 populate_token_positions(query_suggestion, leaf_to_indices, i, array_token_positions); for(const std::vector> & token_positions: array_token_positions) { - if(token_positions.size() == 0) { + if(token_positions.empty()) { continue; } const Match & match = Match::match(seq_id, token_positions); @@ -1288,7 +1288,7 @@ void Index::populate_token_positions(const std::vector &query_sugges // for every element in a potential array, for every token in query suggestion, get the positions // first ascertain the size of the array - size_t array_size = 1; + size_t array_size = 0; for (const art_leaf *token_leaf : query_suggestion) { size_t this_array_size = 1;