sort_unit.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. /*
  2. * Tencent is pleased to support the open source community by making wwsearch
  3. * available.
  4. *
  5. * Copyright (C) 2018-present Tencent. All Rights Reserved.
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  8. * use this file except in compliance with the License. You may obtain a copy of
  9. * the License at
  10. *
  11. * https://opensource.org/licenses/Apache-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OF ANY KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations under the License.
  17. */
  18. #include <gtest/gtest.h>
  19. #include "include/collector_top.h"
  20. #include "include/document.h"
  21. #include "include/index_wrapper.h"
  22. #include "include/search_util.h"
  23. #include "include/sorter.h"
  24. #include "unittest_util.h"
  25. extern bool g_debug;
  26. extern bool g_use_rocksdb;
  27. extern bool g_use_compression;
  28. namespace wwsearch {
  29. class SortTest : public ::testing::Test {
  30. public:
  31. static DefaultIndexWrapper* index;
  32. static uint64_t document_id;
  33. static uint64_t numeric_value;
  34. wwsearch::TableID table;
  35. std::vector<DocumentUpdater*> documents;
  36. std::list<DocumentID> match_documentsid;
  37. public:
  38. SortTest() {
  39. table.business_type = 1;
  40. table.partition_set = 1;
  41. }
  42. static void SetUpTestCase() {
  43. index = new DefaultIndexWrapper();
  44. index->DBParams().path = std::string("/tmp/unit_") + std::string("sort");
  45. index->Config().SetLogLevel(g_debug ? wwsearch::kSearchLogLevelDebug
  46. : wwsearch::kSearchLogLevelError);
  47. auto status = index->Open(g_use_rocksdb, g_use_compression);
  48. ASSERT_TRUE(status.GetCode() == 0);
  49. }
  50. static void TearDownTestCase() {
  51. if (index != nullptr) {
  52. index->vdb_->DropDB();
  53. delete index;
  54. index = nullptr;
  55. }
  56. }
  57. virtual void SetUp() override {
  58. table.partition_set++;
  59. match_documentsid.clear();
  60. }
  61. virtual void TearDown() override {
  62. for (auto du : documents) {
  63. delete du;
  64. }
  65. documents.clear();
  66. match_documentsid.clear();
  67. }
  68. uint64_t GetDocumentID() { return document_id++; }
  69. uint64_t GetNumeric(uint64_t alloc_len = 1000) {
  70. auto temp = numeric_value;
  71. numeric_value += alloc_len;
  72. return temp;
  73. }
  74. private:
  75. };
  76. DefaultIndexWrapper* SortTest::index = nullptr;
  77. DocumentID SortTest::document_id = 1;
  78. DocumentID SortTest::numeric_value = 1;
  79. TEST_F(SortTest, Filters) {
  80. auto base = GetNumeric(10000);
  81. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloa", base,
  82. base + 100, base + 69));
  83. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloa", base + 1,
  84. base + 101, base + 70));
  85. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloa", base + 2,
  86. base + 102, base + 71));
  87. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  88. nullptr, nullptr);
  89. EXPECT_TRUE(ret);
  90. if (g_debug) {
  91. for (const auto& du : documents) {
  92. EXPECT_EQ(0, du->Status().GetCode());
  93. wwsearch::Document& document = du->New();
  94. std::string debug_str;
  95. document.PrintToReadStr(debug_str);
  96. SearchLogDebug("%s\n", debug_str.c_str());
  97. }
  98. }
  99. wwsearch::Searcher searcher(&index->Config());
  100. {
  101. match_documentsid.clear();
  102. std::vector<wwsearch::SortCondition*> store_sort;
  103. {
  104. wwsearch::SortCondition* sc =
  105. new wwsearch::NumericSortCondition(4, ::wwsearch::kSortConditionDesc);
  106. store_sort.push_back(sc);
  107. }
  108. wwsearch::BooleanQuery query1(1, "helloa");
  109. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, &store_sort,
  110. match_documentsid);
  111. EXPECT_EQ(0, status.GetCode());
  112. ASSERT_EQ(3, match_documentsid.size());
  113. bool first = true;
  114. DocumentID prev;
  115. for (auto id : match_documentsid) {
  116. if (first) {
  117. first = false;
  118. prev = id;
  119. } else {
  120. EXPECT_TRUE(prev >= id);
  121. prev = id;
  122. }
  123. }
  124. if (g_debug) {
  125. for (auto documentid : match_documentsid) {
  126. SearchLogDebug("match[%llu]\n", documentid);
  127. }
  128. }
  129. }
  130. {
  131. match_documentsid.clear();
  132. std::vector<wwsearch::SortCondition*> store_sort;
  133. {
  134. wwsearch::SortCondition* sc =
  135. new wwsearch::NumericSortCondition(4, ::wwsearch::kSortConditionAsc);
  136. store_sort.push_back(sc);
  137. }
  138. wwsearch::BooleanQuery query1(1, "helloa");
  139. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, &store_sort,
  140. match_documentsid);
  141. EXPECT_EQ(0, status.GetCode());
  142. ASSERT_EQ(3, match_documentsid.size());
  143. bool first = true;
  144. DocumentID prev;
  145. for (auto id : match_documentsid) {
  146. if (first) {
  147. first = false;
  148. prev = id;
  149. } else {
  150. EXPECT_TRUE(prev <= id);
  151. prev = id;
  152. }
  153. }
  154. if (g_debug) {
  155. for (auto documentid : match_documentsid) {
  156. SearchLogDebug("match[%llu]\n", documentid);
  157. }
  158. }
  159. }
  160. // default sort
  161. {
  162. match_documentsid.clear();
  163. wwsearch::BooleanQuery query1(1, "helloa");
  164. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  165. match_documentsid);
  166. EXPECT_EQ(0, status.GetCode());
  167. ASSERT_EQ(3, match_documentsid.size());
  168. bool first = true;
  169. DocumentID prev;
  170. for (auto id : match_documentsid) {
  171. if (first) {
  172. first = false;
  173. prev = id;
  174. } else {
  175. EXPECT_TRUE(prev > id);
  176. prev = id;
  177. }
  178. }
  179. if (g_debug) {
  180. for (auto documentid : match_documentsid) {
  181. SearchLogDebug("match[%llu]\n", documentid);
  182. }
  183. }
  184. }
  185. }
  186. TEST_F(SortTest, DefaultSort) {
  187. Document doc1;
  188. doc1.SetID(101);
  189. Document doc2;
  190. doc2.SetID(102);
  191. Document doc3;
  192. doc3.SetID(103);
  193. PriorityQueue pri_queue(Sorter(nullptr));
  194. pri_queue.push(&doc2);
  195. pri_queue.push(&doc1);
  196. pri_queue.push(&doc3);
  197. EXPECT_EQ(pri_queue.top()->ID(), doc1.ID());
  198. pri_queue.pop();
  199. EXPECT_EQ(pri_queue.top()->ID(), doc2.ID());
  200. pri_queue.pop();
  201. EXPECT_EQ(pri_queue.top()->ID(), doc3.ID());
  202. pri_queue.pop();
  203. EXPECT_EQ(pri_queue.size(), 0);
  204. }
  205. TEST_F(SortTest, StringSort) {
  206. auto base = GetNumeric(10000);
  207. documents.clear();
  208. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "aacc", base,
  209. base + 100, base + 69));
  210. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "aa", base,
  211. base + 101, base + 70));
  212. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "cc", base,
  213. base + 102, base + 71));
  214. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  215. nullptr, nullptr);
  216. EXPECT_TRUE(ret);
  217. if (g_debug) {
  218. for (const auto& du : documents) {
  219. EXPECT_EQ(0, du->Status().GetCode());
  220. wwsearch::Document& document = du->New();
  221. std::string debug_str;
  222. document.PrintToReadStr(debug_str);
  223. SearchLogDebug("%s\n", debug_str.c_str());
  224. }
  225. }
  226. wwsearch::Searcher searcher(&index->Config());
  227. {
  228. match_documentsid.clear();
  229. std::vector<wwsearch::SortCondition*> store_sort;
  230. {
  231. wwsearch::SortCondition* sc =
  232. new wwsearch::StringSortCondition(1, ::wwsearch::kSortConditionAsc);
  233. store_sort.push_back(sc);
  234. }
  235. wwsearch::BooleanQuery query1(2, (uint32_t)base);
  236. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, &store_sort,
  237. match_documentsid);
  238. EXPECT_EQ(0, status.GetCode());
  239. ASSERT_EQ(3, match_documentsid.size());
  240. std::vector<uint64_t> ids;
  241. for (auto id : match_documentsid) {
  242. ids.push_back(id);
  243. printf("id %lu\n", id);
  244. }
  245. ASSERT_TRUE(ids[1] < ids[0]);
  246. ASSERT_TRUE(ids[0] < ids[2]);
  247. if (g_debug) {
  248. for (auto documentid : match_documentsid) {
  249. SearchLogDebug("match[%llu]\n", documentid);
  250. }
  251. }
  252. }
  253. }
  254. } // namespace wwsearch