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

Don't expose the match distance computation outside.

Kishore Nallane00f7a5cece8
diff --git a/include/match_score.h b/include/match_score.h
index 49edf5c6..cf901000 100644
--- a/include/match_score.h
+++ b/include/match_score.h
@@ -17,7 +17,7 @@
const size_t WINDOW_SIZE = 10;
const uint16_t MAX_DISPLACEMENT = std::numeric_limits<uint16_t>::max();
-const size_t MAX_TOKENS_DISTANCE = 100;
+const uint16_t MAX_TOKENS_DISTANCE = 100;
struct TokenOffset {
uint8_t token_id; // token identifier
@@ -179,7 +179,8 @@ struct Match {
token_index++;
}
+ const uint16_t distance = MAX_TOKENS_DISTANCE - min_displacement;
pack_token_offsets(min_token_offset, token_offsets.size(), token_start_offset, packed_offset_diffs);
- return Match(max_match, std::min(MAX_TOKENS_DISTANCE, (size_t)min_displacement), token_start_offset, packed_offset_diffs);
+ return Match(max_match, distance, token_start_offset, packed_offset_diffs);
}
};
diff --git a/src/index.cpp b/src/index.cpp
index e005de1f..429260ad 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -846,7 +846,7 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const int &
Match single_token_match = Match(1, 0, 0, empty_offset_diffs);
const uint64_t single_token_match_score = ((int64_t)(single_token_match.words_present) << 24) |
((int64_t)(255 - total_cost) << 16) |
- ((int64_t)abs(int(MAX_TOKENS_DISTANCE - (size_t)single_token_match.distance)));
+ ((int64_t)(single_token_match.distance));
for(size_t i=0; i<result_size; i++) {
const uint32_t seq_id = result_ids[i];
@@ -863,7 +863,7 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const int &
// Construct a single match score from individual components (for multi-field sort)
match_score = ((int64_t)(match.words_present) << 24) |
((int64_t)(255 - total_cost) << 16) |
- ((int64_t)(MAX_TOKENS_DISTANCE - match.distance));
+ ((int64_t)(match.distance));
}
const int64_t default_score = 0;
@@ -889,8 +889,6 @@ void Index::score_results(const std::vector<sort_by> & sort_fields, const int &
os << name << ", total_cost: " << (255 - total_cost)
<< ", words_present: " << match.words_present << ", match_score: " << match_score
<< ", match.distance: " << match.distance
- << ", distance: "
- << (MAX_TOKENS_DISTANCE - match.distance)
<< ", seq_id: " << seq_id << std::endl;
LOG(INFO) << os.str();
*/