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

Fix query index searched queries mismatch.

Kishore Nallane90dc8cbd7fd
diff --git a/include/index.h b/include/index.h
index c1e2fbbd..302567d8 100644
--- a/include/index.h
+++ b/include/index.h
@@ -802,7 +802,7 @@ public:
size_t& exclude_token_ids_size) const;
void do_infix_search(const std::vector<sort_by>& sort_fields_std,
- const std::vector<std::vector<art_leaf*>>& searched_queries, const size_t group_limit,
+ std::vector<std::vector<art_leaf*>>& searched_queries, const size_t group_limit,
const std::vector<std::string>& group_by_fields, const size_t max_extra_prefix,
const size_t max_extra_suffix, const infix_t& field_infix, const uint8_t field_id,
const string& field_name, const std::vector<token_t>& query_tokens, Topster* actual_topster,
diff --git a/src/collection.cpp b/src/collection.cpp
index 9357d48c..330ce4fe 100644
--- a/src/collection.cpp
+++ b/src/collection.cpp
@@ -1758,6 +1758,15 @@ void Collection::highlight_result(const std::string& raw_query, const field &sea
break;
}
+ if(field_order_kv->query_indices != nullptr &&
+ searched_queries.size() <= field_order_kv->query_indices[qindex + 1]) {
+ LOG(ERROR) << "Query indices exceed searched queries length, searched_queries.size(): "
+ << searched_queries.size() << ", field_order_kv->query_indices[qindex + 1]: "
+ << field_order_kv->query_indices[qindex + 1] << ", qindex: " << qindex
+ << "field_order_kv->query_indices[0]: " << field_order_kv->query_indices[0];
+ break;
+ }
+
auto searched_query =
(field_order_kv->query_indices == nullptr) ? searched_queries[field_order_kv->query_index] :
searched_queries[field_order_kv->query_indices[qindex + 1]];
diff --git a/src/index.cpp b/src/index.cpp
index efba9eec..5d9686ba 100644
--- a/src/index.cpp
+++ b/src/index.cpp
@@ -1255,7 +1255,6 @@ void Index::search_candidates(const uint8_t & field_id, bool field_is_array,
id_buff.size(), &new_all_result_ids);
delete[] *all_result_ids;
*all_result_ids = new_all_result_ids;
- num_result_ids += id_buff.size();
id_buff.clear();
}
@@ -2724,7 +2723,7 @@ void Index::do_synonym_search(const std::vector<filter>& filters,
}
void Index::do_infix_search(const std::vector<sort_by>& sort_fields_std,
- const std::vector<std::vector<art_leaf*>>& searched_queries, const size_t group_limit,
+ std::vector<std::vector<art_leaf*>>& searched_queries, const size_t group_limit,
const std::vector<std::string>& group_by_fields, const size_t max_extra_prefix,
const size_t max_extra_suffix, const infix_t& field_infix, const uint8_t field_id,
const string& field_name, const std::vector<token_t>& query_tokens, Topster* actual_topster,
@@ -2787,6 +2786,8 @@ void Index::do_infix_search(const std::vector<sort_by>& sort_fields_std,
if(raw_infix_ids != &infix_ids[0]) {
delete [] raw_infix_ids;
}
+
+ searched_queries.push_back({});
}
}
}
@@ -3059,6 +3060,8 @@ void Index::search_wildcard(const std::vector<filter>& filters,
const uint32_t* batch_result_ids = filter_ids + filter_index;
num_queued++;
+ searched_queries.push_back({});
+
topsters[thread_id] = new Topster(topster->MAX_SIZE, topster->distinct);
thread_pool->enqueue([this, &parent_search_begin, &parent_search_stop_ms, &parent_search_cutoff,
@@ -3341,8 +3344,8 @@ void Index::search_field(const uint8_t & field_id,
n++;
}
- // When atleast one token from the query is available
- if(!query_tokens.empty() && num_tokens_dropped < query_tokens.size()) {
+ // When atleast two tokens from the query are available so we can drop one
+ if(query_tokens.size() > 1 && num_tokens_dropped < query_tokens.size()) {
// Drop tokens from right until (len/2 + 1), and then from left until (len/2 + 1)
if(!exhaustive_search && field_num_results >= drop_tokens_threshold) {
diff --git a/test/collection_infix_search_test.cpp b/test/collection_infix_search_test.cpp
index 528f6460..b6df2d01 100644
--- a/test/collection_infix_search_test.cpp
+++ b/test/collection_infix_search_test.cpp
@@ -388,3 +388,36 @@ TEST_F(CollectionInfixSearchTest, InfixDeleteAndUpdate) {
collectionManager.drop_collection("coll1");
}
+
+TEST_F(CollectionInfixSearchTest, MultiFielInfixSearch) {
+ std::vector<field> fields = {field("title", field_types::STRING, false, false, true, "", -1, 1),
+ field("mpn", field_types::STRING, false, false, true, "", -1, 1),
+ field("points", field_types::INT32, false),};
+
+ Collection* coll1 = collectionManager.create_collection("coll1", 1, fields, "points").get();
+
+ nlohmann::json doc;
+ doc["id"] = "0";
+ doc["title"] = "100037 Shoe";
+ doc["mpn"] = "HYDGHSGAH";
+ doc["points"] = 100;
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ doc["id"] = "1";
+ doc["title"] = "Running Shoe";
+ doc["mpn"] = "GHX100037IN";
+ doc["points"] = 100;
+ ASSERT_TRUE(coll1->add(doc.dump()).ok());
+
+ auto results = coll1->search("100037",
+ {"title", "mpn"}, "", {}, {}, {0}, 3, 1, FREQUENCY, {true}, 5,
+ spp::sparse_hash_set<std::string>(),
+ spp::sparse_hash_set<std::string>(), 10, "", 30, 4, "title", 20, {}, {}, {}, 0,
+ "<mark>", "</mark>", {}, 1000, true, false, true, "", false, 6000 * 1000, 4, 7, true,
+ 4, {always}).get();
+
+ ASSERT_EQ(2, results["found"].get<size_t>());
+ ASSERT_EQ(2, results["hits"].size());
+
+ collectionManager.drop_collection("coll1");
+}