Allow max_hits to be specified as "all".

This commit is contained in:
kishorenc 2020-05-21 08:43:27 +05:30
parent a3024a7342
commit 6af35f5de8
4 changed files with 49 additions and 6 deletions

View File

@ -226,7 +226,7 @@ public:
size_t drop_tokens_threshold = Index::DROP_TOKENS_THRESHOLD,
const spp::sparse_hash_set<std::string> & include_fields = spp::sparse_hash_set<std::string>(),
const spp::sparse_hash_set<std::string> & exclude_fields = spp::sparse_hash_set<std::string>(),
size_t max_facet_values=10, size_t max_hits=500,
size_t max_facet_values=10, int max_hits=500,
const std::string & simple_facet_query = "",
const size_t snippet_threshold = 30,
const std::string & highlight_full_fields = "",

View File

@ -316,7 +316,7 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
const size_t drop_tokens_threshold,
const spp::sparse_hash_set<std::string> & include_fields,
const spp::sparse_hash_set<std::string> & exclude_fields,
const size_t max_facet_values, const size_t max_hits,
const size_t max_facet_values, const int _max_hits,
const std::string & simple_facet_query,
const size_t snippet_threshold,
const std::string & highlight_full_fields,
@ -570,11 +570,13 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
return Option<nlohmann::json>(422, message);
}
const size_t results_per_page = std::min(per_page, max_hits);
const size_t maximum_hits = (_max_hits < 0) ? std::max((size_t)100, get_num_documents()) : size_t(_max_hits);
const size_t results_per_page = std::min(per_page, maximum_hits);
const size_t num_results = (page * results_per_page);
if(num_results > max_hits) {
std::string message = "Only the first " + std::to_string(max_hits) + " results are available.";
if(num_results > maximum_hits) {
std::string message = "Only the first " + std::to_string(maximum_hits) + " results are available.";
return Option<nlohmann::json>(422, message);
}
@ -589,7 +591,7 @@ Option<nlohmann::json> Collection::search(const std::string & query, const std::
for(Index* index: indices) {
index->search_params = search_args(query, search_fields, filters, facets,
index_to_included_ids[index_id], index_to_excluded_ids[index_id],
sort_fields_std, facet_query, num_typos, max_facet_values, max_hits,
sort_fields_std, facet_query, num_typos, max_facet_values, maximum_hits,
results_per_page, page, token_order, prefix,
drop_tokens_threshold, typo_tokens_threshold);
{

View File

@ -290,6 +290,9 @@ bool get_search(http_req & req, http_res & res) {
} else {
req.params[MAX_HITS] = "0";
}
} else if(req.params[MAX_HITS] == "all") {
// engine will default to size of collection
req.params[MAX_HITS] = "-1";
}
if(req.params.count(SNIPPET_THRESHOLD) == 0) {

View File

@ -2265,3 +2265,41 @@ TEST_F(CollectionTest, OptionalFields) {
collectionManager.drop_collection("coll1");
}
TEST_F(CollectionTest, ReturnsResultsBasedOnMaxHitsParam) {
std::vector<std::string> facets;
spp::sparse_hash_set<std::string> empty;
nlohmann::json results = collection->search("*", query_fields, "", facets, sort_fields, 0, 100, 1,
FREQUENCY, false, 1000, empty, empty, 10, 12).get();
ASSERT_EQ(12, results["hits"].size());
ASSERT_EQ(25, results["found"].get<int>());
// should match collection size
results = collection->search("*", query_fields, "", facets, sort_fields, 0, 100, 1,
FREQUENCY, false, 1000, empty, empty, 10, -1).get();
ASSERT_EQ(25, results["hits"].size());
ASSERT_EQ(25, results["found"].get<int>());
// should still respect pagination
results = collection->search("*", query_fields, "", facets, sort_fields, 0, 10, 1,
FREQUENCY, false, 1000, empty, empty, 10, -1).get();
ASSERT_EQ(10, results["hits"].size());
ASSERT_EQ(25, results["found"].get<int>());
results = collection->search("*", query_fields, "", facets, sort_fields, 0, 10, 2,
FREQUENCY, false, 1000, empty, empty, 10, -1).get();
ASSERT_EQ(10, results["hits"].size());
ASSERT_EQ(25, results["found"].get<int>());
results = collection->search("*", query_fields, "", facets, sort_fields, 0, 10, 3,
FREQUENCY, false, 1000, empty, empty, 10, -1).get();
ASSERT_EQ(5, results["hits"].size());
ASSERT_EQ(25, results["found"].get<int>());
}