Skip to content

Open Source alternative to Algolia + Pinecone and an Easier-to-Use alternative to ElasticSearch ⚡ 🔍 ✨ Fast, typo tolerant, in-memory fuzzy Search Engine for building delightful search experiences

Handle edge case of finding no query tokens in the array.

kishorencd58a6e613be0
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<uint16_t> 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);
}
}
- // positions in the field of each token in the query
- std::vector<std::vector<std::vector<uint16_t>>> array_token_positions;
- Index::populate_token_positions(query_suggestion, leaf_to_indices, 0, array_token_positions);
-
- if(array_token_positions.size() == 0) {
+ 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<std::vector<std::vector<uint16_t>>> array_token_positions;
+ Index::populate_token_positions(query_suggestion, leaf_to_indices, 0, array_token_positions);
+
std::vector<match_index_t> 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<std::vector<uint16_t>> & 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_by> & sort_fields, const uint16
populate_token_positions(query_suggestion, leaf_to_indices, i, array_token_positions);
for(const std::vector<std::vector<uint16_t>> & 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<art_leaf *> &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;