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

Revert "Highlight field value that is a prefix of the query."

Kishore Nallan13cb7b936493
diff --git a/include/index.h b/include/index.h
index b10bea9d..37bb45e0 100644
--- a/include/index.h
+++ b/include/index.h
@@ -396,8 +396,7 @@ public:
static void transform_for_180th_meridian(GeoCoord& point, double offset);
- void get_token_leaves(const std::string & field_name, const unsigned char* token, uint32_t token_len,
- std::vector<art_leaf*>& leaves);
+ art_leaf* get_token_leaf(const std::string & field_name, const unsigned char* token, uint32_t token_len);
// the following methods are not synchronized because their parent calls are synchronized
diff --git a/src/collection.cpp b/src/collection.cpp
index aaf0d11f..3373ab30 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1502,12 +1502,11 @@ void Collection::highlight_result(const field &search_field,
// Must search for the token string fresh on that field for the given document since `token_leaf`
// 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_memory_shards];
+ art_leaf *actual_leaf = index->get_token_leaf(search_field.name, &token_leaf->key[0], token_leaf->key_len);
- std::vector<art_leaf*> leaves;
- index->get_token_leaves(search_field.name, &token_leaf->key[0], token_leaf->key_len, leaves);
+ //LOG(INFO) << "field: " << search_field.name << ", key: " << token_leaf->key;
- for(const auto actual_leaf: leaves) {
- //LOG(INFO) << "field: " << search_field.name << ", key: " << token_leaf->key;
+ if(actual_leaf != nullptr) {
query_suggestion.push_back(actual_leaf);
std::string token(reinterpret_cast<char*>(actual_leaf->key), actual_leaf->key_len-1);
query_suggestion_tokens.insert(token);
@@ -1527,11 +1526,10 @@ void Collection::highlight_result(const field &search_field,
}
Index* index = indices[field_order_kv->key % num_memory_shards];
- std::vector<art_leaf*> leaves;
- index->get_token_leaves(search_field.name, reinterpret_cast<const unsigned char*>(q_token.c_str()),
- q_token.size() + 1, leaves);
-
- for(const auto actual_leaf: leaves) {
+ art_leaf *actual_leaf = index->get_token_leaf(search_field.name,
+ reinterpret_cast<const unsigned char *>(q_token.c_str()),
+ q_token.size() + 1);
+ if(actual_leaf != nullptr) {
std::vector<uint16_t> positions;
uint32_t doc_index = actual_leaf->values->ids.indexOf(field_order_kv->key);
if(doc_index != actual_leaf->values->ids.getLength()) {
diff --git a/src/index.cpp b/src/index.cpp
index 1b8227d2..6f6f4b7d 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -2639,13 +2639,10 @@ void Index::tokenize_string_field(const nlohmann::json& document, const field& s
}
}
-void Index::get_token_leaves(const std::string & field_name, const unsigned char* token, uint32_t token_len,
- std::vector<art_leaf*>& leaves) {
+art_leaf* Index::get_token_leaf(const std::string & field_name, const unsigned char* token, uint32_t token_len) {
std::shared_lock lock(mutex);
- art_tree *t = search_index.at(field_name);
- token_len = (token_len == 0) ? 0 : token_len-1;
- art_fuzzy_search(t, token, token_len, 0, 0, 2, token_ordering::MAX_SCORE,
- true, nullptr, 0, leaves);
+ const art_tree *t = search_index.at(field_name);
+ return (art_leaf*) art_search(t, token, (int) token_len);
}
const spp::sparse_hash_map<std::string, art_tree *> &Index::_get_search_index() const {
diff --git a/test/collection_specific_test.cpp b/test/collection_specific_test.cpp
index 4fc657e7..c1855479 100644
--- a/test/collection_specific_test.cpp
+++ b/test/collection_specific_test.cpp
@@ -612,47 +612,6 @@ TEST_F(CollectionSpecificTest, TokensSpreadAcrossFields) {
collectionManager.drop_collection("coll1");
}
-TEST_F(CollectionSpecificTest, HighlightSecondaryFieldWithPrefixMatch) {
- std::vector<field> fields = {field("title", field_types::STRING, false),
- field("description", field_types::STRING, false),
- field("points", field_types::INT32, false),};
-
- Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
-
- nlohmann::json doc1;
- doc1["id"] = "0";
- doc1["title"] = "Functions and Equations";
- doc1["description"] = "Use a function to solve an equation.";
- doc1["points"] = 100;
-
- nlohmann::json doc2;
- doc2["id"] = "1";
- doc2["title"] = "Function of effort";
- doc2["description"] = "Learn all about it.";
- doc2["points"] = 100;
-
- ASSERT_TRUE(coll1->add(doc1.dump()).ok());
- ASSERT_TRUE(coll1->add(doc2.dump()).ok());
-
- auto results = coll1->search("function", {"title", "description"}, "", {}, {}, {0}, 10,
- 1, FREQUENCY, {true, true},
- 10, spp::sparse_hash_set<std::string>(),
- spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "", 40, {}, {}, {}, 0,
- "<mark>", "</mark>", {1, 1}).get();
-
- ASSERT_EQ("0", results["hits"][0]["document"]["id"].get<std::string>());
-
- ASSERT_EQ(2, results["hits"][0]["highlights"].size());
-
- ASSERT_EQ("<mark>Functions</mark> and Equations",
- results["hits"][0]["highlights"][0]["snippet"].get<std::string>());
-
- ASSERT_EQ("Use a <mark>function</mark> to solve an equation.",
- results["hits"][0]["highlights"][1]["snippet"].get<std::string>());
-
- collectionManager.drop_collection("coll1");
-}
-
TEST_F(CollectionSpecificTest, GuardAgainstIdFieldInSchema) {
// The "id" field, if defined in the schema should be ignored
diff --git a/test/collection_test.cpp b/test/collection_test.cpp
index d82982d0..0e9221a2 100644
--- a/test/collection_test.cpp
+++ b/test/collection_test.cpp
@@ -3317,7 +3317,7 @@ TEST_F(CollectionTest, MultiFieldHighlighting) {
ASSERT_STREQ("0", results["hits"][0]["document"]["id"].get<std::string>().c_str());
- ASSERT_EQ(3, results["hits"][0]["highlights"].size());
+ ASSERT_EQ(2, results["hits"][0]["highlights"].size());
ASSERT_EQ("name", results["hits"][0]["highlights"][0]["field"].get<std::string>());
ASSERT_EQ("Best Wireless Vehicle <mark>Charger</mark>",
results["hits"][0]["highlights"][0]["snippet"].get<std::string>());
@@ -3326,10 +3326,6 @@ TEST_F(CollectionTest, MultiFieldHighlighting) {
ASSERT_EQ("Easily replenish your cell phone with this wireless <mark>charger.</mark>",
results["hits"][0]["highlights"][1]["snippet"].get<std::string>());
- ASSERT_EQ("categories", results["hits"][0]["highlights"][2]["field"].get<std::string>());
- ASSERT_EQ("Car <mark>Chargers</mark>",
- results["hits"][0]["highlights"][2]["snippets"][0].get<std::string>());
-
results = coll1->search("John With Denver",
{"description"}, "", {}, {}, {0}, 10, 1, FREQUENCY,
{true}, 1, spp::sparse_hash_set<std::string>(),