boolquery_unit.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  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/index_wrapper.h"
  20. #include "include/prefix_query.h"
  21. #include "include/search_util.h"
  22. #include "unittest_util.h"
  23. extern bool g_debug;
  24. extern bool g_use_rocksdb;
  25. extern bool g_use_compression;
  26. namespace wwsearch {
  27. class BoolQueryTest : public ::testing::Test {
  28. public:
  29. static DefaultIndexWrapper *index;
  30. static uint64_t document_id;
  31. static uint64_t numeric_value;
  32. wwsearch::TableID table;
  33. std::vector<DocumentUpdater *> documents;
  34. std::list<DocumentID> match_documentsid;
  35. public:
  36. BoolQueryTest() {
  37. table.business_type = 1;
  38. table.partition_set = 1;
  39. }
  40. static void SetUpTestCase() {
  41. index = new DefaultIndexWrapper();
  42. index->DBParams().path =
  43. std::string("/tmp/unit_") + std::string("boolquery");
  44. index->Config().SetLogLevel(g_debug ? wwsearch::kSearchLogLevelDebug
  45. : wwsearch::kSearchLogLevelError);
  46. auto status = index->Open(g_use_rocksdb, g_use_compression);
  47. ASSERT_TRUE(status.GetCode() == 0);
  48. }
  49. static void TearDownTestCase() {
  50. if (index != nullptr) {
  51. index->vdb_->DropDB();
  52. delete index;
  53. index = nullptr;
  54. }
  55. }
  56. virtual void SetUp() override {
  57. table.partition_set++;
  58. match_documentsid.clear();
  59. }
  60. virtual void TearDown() override {
  61. for (auto du : documents) {
  62. delete du;
  63. }
  64. documents.clear();
  65. match_documentsid.clear();
  66. }
  67. uint64_t GetDocumentID() { return document_id++; }
  68. uint64_t GetNumeric(uint64_t alloc_len = 1000) {
  69. auto temp = numeric_value;
  70. numeric_value += alloc_len;
  71. return temp;
  72. }
  73. private:
  74. };
  75. DefaultIndexWrapper *BoolQueryTest::index = nullptr;
  76. DocumentID BoolQueryTest::document_id = 1;
  77. DocumentID BoolQueryTest::numeric_value = 1;
  78. TEST_F(BoolQueryTest, Query_String) {
  79. auto base = GetNumeric(10000);
  80. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloa", base,
  81. base + 100, base + 69));
  82. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "girla", base + 1,
  83. base + 101, base + 69));
  84. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloa", base + 2,
  85. base + 102, base + 69));
  86. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  87. nullptr, nullptr);
  88. EXPECT_TRUE(ret);
  89. if (g_debug) {
  90. for (const auto &du : documents) {
  91. EXPECT_EQ(0, du->Status().GetCode());
  92. wwsearch::Document &document = du->New();
  93. std::string debug_str;
  94. document.PrintToReadStr(debug_str);
  95. SearchLogDebug("%s\n", debug_str.c_str());
  96. }
  97. }
  98. wwsearch::Searcher searcher(&index->Config());
  99. {
  100. match_documentsid.clear();
  101. wwsearch::BooleanQuery query1(1, "helloa");
  102. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  103. match_documentsid);
  104. EXPECT_EQ(0, status.GetCode());
  105. EXPECT_EQ(2, match_documentsid.size());
  106. if (g_debug) {
  107. for (auto documentid : match_documentsid) {
  108. SearchLogDebug("match[%llu]\n", documentid);
  109. }
  110. }
  111. }
  112. {
  113. match_documentsid.clear();
  114. wwsearch::BooleanQuery query2(1, "girla");
  115. auto status = searcher.DoQuery(table, query2, 0, 100, nullptr, nullptr,
  116. match_documentsid);
  117. EXPECT_EQ(0, status.GetCode());
  118. EXPECT_EQ(1, match_documentsid.size());
  119. if (g_debug) {
  120. for (auto documentid : match_documentsid) {
  121. SearchLogDebug("match[%llu]\n", documentid);
  122. }
  123. }
  124. }
  125. {
  126. match_documentsid.clear();
  127. wwsearch::BooleanQuery query3(1, "nothing1");
  128. auto status = searcher.DoQuery(table, query3, 0, 100, nullptr, nullptr,
  129. match_documentsid);
  130. EXPECT_EQ(0, status.GetCode());
  131. EXPECT_EQ(0, match_documentsid.size());
  132. if (g_debug) {
  133. for (auto documentid : match_documentsid) {
  134. SearchLogDebug("match[%llu]\n", documentid);
  135. }
  136. }
  137. }
  138. }
  139. TEST_F(BoolQueryTest, Query_SuffixString) {
  140. auto base = GetNumeric(10000);
  141. auto document_updater = TestUtil::NewDocument(
  142. GetDocumentID(), "sufixxxxxxxxx", base, base + 100, base + 69);
  143. document_updater->New().FindField(1)->Flag().SetSuffixBuild();
  144. documents.push_back(document_updater);
  145. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  146. nullptr, nullptr);
  147. EXPECT_TRUE(ret);
  148. if (g_debug) {
  149. for (const auto &du : documents) {
  150. EXPECT_EQ(0, du->Status().GetCode());
  151. wwsearch::Document &document = du->New();
  152. std::string debug_str;
  153. document.PrintToReadStr(debug_str);
  154. SearchLogDebug("%s\n", debug_str.c_str());
  155. }
  156. }
  157. wwsearch::Searcher searcher(&index->Config());
  158. {
  159. match_documentsid.clear();
  160. wwsearch::BooleanQuery query1(1, "fixxxxxxxxx");
  161. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  162. match_documentsid);
  163. EXPECT_EQ(0, status.GetCode());
  164. EXPECT_EQ(1, match_documentsid.size());
  165. if (g_debug) {
  166. for (auto documentid : match_documentsid) {
  167. SearchLogDebug("match[%llu]\n", documentid);
  168. }
  169. }
  170. }
  171. }
  172. TEST_F(BoolQueryTest, PrefixQuery) {
  173. auto base = GetNumeric(10000);
  174. {
  175. auto document_updater = TestUtil::NewDocument(GetDocumentID(), "prefixxxxx",
  176. base, base + 100, base + 69);
  177. document_updater->New().FindField(1)->Flag().SetSuffixBuild();
  178. documents.push_back(document_updater);
  179. }
  180. {
  181. auto document_updater = TestUtil::NewDocument(
  182. GetDocumentID(), "wonderrefixg", base, base + 100, base + 69);
  183. document_updater->New().FindField(1)->Flag().SetSuffixBuild();
  184. documents.push_back(document_updater);
  185. }
  186. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  187. nullptr, nullptr);
  188. EXPECT_TRUE(ret);
  189. if (g_debug) {
  190. for (const auto &du : documents) {
  191. EXPECT_EQ(0, du->Status().GetCode());
  192. wwsearch::Document &document = du->New();
  193. std::string debug_str;
  194. document.PrintToReadStr(debug_str);
  195. SearchLogDebug("%s\n", debug_str.c_str());
  196. }
  197. }
  198. wwsearch::Searcher searcher(&index->Config());
  199. {
  200. match_documentsid.clear();
  201. wwsearch::PrefixQuery query1(1, "refix");
  202. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  203. match_documentsid);
  204. EXPECT_EQ(0, status.GetCode());
  205. EXPECT_EQ(2, match_documentsid.size());
  206. if (g_debug) {
  207. for (auto documentid : match_documentsid) {
  208. SearchLogDebug("match[%llu]\n", documentid);
  209. }
  210. }
  211. }
  212. }
  213. TEST_F(BoolQueryTest, Query_Chinese) {
  214. auto base = GetNumeric(10000);
  215. auto document_updater = TestUtil::NewDocument(
  216. GetDocumentID(),
  217. "我是中国人,我爱中国。为了加速倒排列表归并。归并过程是按DocumentID从大到"
  218. "小的顺序遍历的,在遍历时要用一个堆结构来加速查找当前最大的DocumentID属于"
  219. "哪个列表。这个堆结构用内存池分配。",
  220. base, base + 100, base + 69);
  221. // document_updater->New().FindField(1)->Flag().SetSuffixBuild();
  222. documents.push_back(document_updater);
  223. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  224. nullptr, nullptr);
  225. EXPECT_TRUE(ret);
  226. if (g_debug) {
  227. for (const auto &du : documents) {
  228. EXPECT_EQ(0, du->Status().GetCode());
  229. wwsearch::Document &document = du->New();
  230. std::string debug_str;
  231. document.PrintToReadStr(debug_str);
  232. SearchLogDebug("%s\n", debug_str.c_str());
  233. }
  234. }
  235. wwsearch::Searcher searcher(&index->Config());
  236. {
  237. match_documentsid.clear();
  238. wwsearch::BooleanQuery query1(1, "分配");
  239. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  240. match_documentsid);
  241. EXPECT_EQ(0, status.GetCode());
  242. EXPECT_EQ(1, match_documentsid.size());
  243. if (g_debug) {
  244. for (auto documentid : match_documentsid) {
  245. SearchLogDebug("match[%llu]\n", documentid);
  246. }
  247. }
  248. }
  249. {
  250. match_documentsid.clear();
  251. wwsearch::BooleanQuery query1(1, "不存在");
  252. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  253. match_documentsid);
  254. EXPECT_EQ(0, status.GetCode());
  255. EXPECT_EQ(0, match_documentsid.size());
  256. if (g_debug) {
  257. for (auto documentid : match_documentsid) {
  258. SearchLogDebug("match[%llu]\n", documentid);
  259. }
  260. }
  261. }
  262. }
  263. TEST_F(BoolQueryTest, Query_Numeric) {
  264. auto base = GetNumeric(10000);
  265. documents.push_back(
  266. TestUtil::NewDocument(GetDocumentID(), "hellob", base, base, base + 69));
  267. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "girlb", base + 1,
  268. base, base + 69));
  269. documents.push_back(
  270. TestUtil::NewDocument(GetDocumentID(), "hellob", base, base, base + 69));
  271. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  272. nullptr, nullptr);
  273. EXPECT_TRUE(ret);
  274. for (auto du : documents) {
  275. EXPECT_EQ(0, du->Status().GetCode());
  276. }
  277. wwsearch::Searcher searcher(&index->Config());
  278. uint32_t value;
  279. {
  280. match_documentsid.clear();
  281. value = base;
  282. wwsearch::BooleanQuery query1(2, value);
  283. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  284. match_documentsid);
  285. EXPECT_EQ(0, status.GetCode());
  286. EXPECT_EQ(2, match_documentsid.size());
  287. }
  288. {
  289. match_documentsid.clear();
  290. value = base + 1;
  291. wwsearch::BooleanQuery query2(2, value);
  292. auto status = searcher.DoQuery(table, query2, 0, 100, nullptr, nullptr,
  293. match_documentsid);
  294. EXPECT_EQ(0, status.GetCode());
  295. EXPECT_EQ(1, match_documentsid.size());
  296. }
  297. {
  298. match_documentsid.clear();
  299. value = 0;
  300. wwsearch::BooleanQuery query3(2, value);
  301. auto status = searcher.DoQuery(table, query3, 0, 100, nullptr, nullptr,
  302. match_documentsid);
  303. EXPECT_EQ(0, status.GetCode());
  304. EXPECT_EQ(0, match_documentsid.size());
  305. }
  306. }
  307. TEST_F(BoolQueryTest, TextQueryUsingTokenize) {
  308. auto base = GetNumeric(10000);
  309. const std::string doc_text1{
  310. " \303\244\302\270\302\245\303\245\302\276\302\267\303\250\305\222\342"
  311. "\200\232 "
  312. "\303\245\302\267\302\245\303\244\302\275\305\223\303\244\302\273\302\273"
  313. "\303\245\305\240\302\241\303\245\302\215\342\200\242 "
  314. "\303\244\302\270\302\245\303\245\302\276\302\267\303\250\305\222\342\200"
  315. "\232\303\247\305\241\342\200\236\303\245\302\267\302\245\303\244\302\275"
  316. "\305\223\303\244\302\273\302\273\303\245\305\240\302\241\303\245\302\215"
  317. "\342\200\242 "
  318. "\303\244\302\270\302\245\303\245\302\276\302\267\303\250\305\222\342\200"
  319. "\232\'s\303\245\302\267\302\245\303\244\302\275\305\223\303\244\302\273"
  320. "\302\273\303\245\305\240\302\241\303\245\302\215\342\200\242 "
  321. "\303\251\302\241\302\271\303\247\342\200\272\302\256\303\245\302\220\302"
  322. "\215\303\247\302\247\302\260 "
  323. "971\303\245\302\261\342\200\242\303\250\302\275\302\246 "
  324. "\303\251\342\200\241\342\200\241\303\250\302\264\302\255\303\246\313\234"
  325. "\305\275\303\247\302\273\342\200\240 "
  326. "\303\245\302\220\313\206\303\245\302\220\305\222\303\245\302\217\302\267"
  327. " HT2018-12-020 "
  328. "\303\244\302\270\342\200\271\303\245\302\215\342\200\242\303\246\342\200"
  329. "\224\302\245\303\246\305\223\305\270 "
  330. "\303\244\302\270\342\200\271\303\245\302\215\342\200\242\303\251\306\222"
  331. "\302\250\303\251\342\200\224\302\250 "
  332. "\303\245\302\220\305\275\303\245\302\244\342\200\236\303\247\302\220\342"
  333. "\200\240\303\246\302\235\305\275\303\244\302\277\305\240\303\246\302\235"
  334. "\302\260 "
  335. "\303\245\302\247\342\200\235\303\246\302\264\302\276\303\251\306\222\302"
  336. "\250\303\251\342\200\224\302\250 "
  337. "\303\244\302\273\342\200\234\303\245\302\272\342\200\234\303\244\302\270"
  338. "\342\200\271\303\246\342\200\223\342\204\242\303\245\305\222\302\272 "
  339. "\303\251\305\223\342\202\254\303\246\302\261\342\200\232\303\244\302\272"
  340. "\302\272\303\246\342\200\242\302\260 2 "
  341. "\303\244\302\272\302\272\303\245\342\200\230\313\234\303\245\302\247\342"
  342. "\200\234\303\245\302\220\302\215 "
  343. "\303\245\342\200\241\302\272\303\245\302\267\302\256\303\245\305\223\302"
  344. "\260\303\245\302\235\342\202\254 "
  345. "\303\245\302\256\302\242\303\246\313\206\302\267\303\250\302\201\342\200"
  346. "\235\303\247\302\263\302\273\303\244\302\272\302\272\303\245\302\217\305"
  347. "\240\303\247\342\200\235\302\265\303\250\302\257\302\235 "
  348. "\303\250\302\246\302\201\303\246\302\261\342\200\232\303\245\342\200\241"
  349. "\302\272\303\245\302\217\342\200\230\303\246\342\200\224\302\266\303\251"
  350. "\342\200\224\302\264 "
  351. "\303\251\302\241\302\271\303\247\342\200\272\302\256\303\247\302\273\302"
  352. "\217\303\247\302\220\342\200\240\303\245\302\247\342\200\234\303\245\302"
  353. "\220\302\215\303\250\302\201\342\200\235\303\247\302\263\302\273\303\246"
  354. "\342\200\223\302\271\303\245\302\274\302\217 "
  355. "\303\250\302\246\302\201\303\246\302\261\342\200\232\303\245\302\256\305"
  356. "\222\303\246\313\206\302\220\303\246\342\200\224\302\266\303\251\342\200"
  357. "\224\302\264 "
  358. "\303\244\302\273\302\273\303\245\305\240\302\241\303\245\342\200\240\342"
  359. "\200\246\303\245\302\256\302\271 "
  360. "12mm\303\246\305\223\302\250\303\246\302\235\302\277\303\257\302\274\305"
  361. "\241\n410*440*2\303\245\302\235\342\200\224\n360*440*"
  362. "2\303\245\302\235\342\200\224\n220*440*"
  363. "4\303\245\302\235\342\200\224\n460*160*300*"
  364. "2\n\303\245\302\244\302\215\303\246\302\250\302\241\303\247\342\200\235"
  365. "\302\250\303\245\302\220\305\275\303\245\302\244\342\200\236\303\247\302"
  366. "\220\342\200\240\303\251\302\242\342\200\2403/"
  367. "11\303\244\302\270\342\200\271\303\245\302\215\313\2067\303\247\342\200"
  368. "\232\302\271\303\245\302\215\305\240\303\245\342\200\260\302\215\303\245"
  369. "\302\256\305\222\303\246\313\206\302\220 "
  370. "\303\244\302\273\302\273\303\245\305\240\302\241\303\244\302\270\342\200"
  371. "\271\303\250\302\276\302\276\303\244\302\272\302\272 "
  372. "\303\250\302\257\302\264\303\246\313\234\305\275"};
  373. const std::string doc_text2{
  374. " \303\251\342\200\241\342\200\230\303\246\342\202\254\302\235\303\247"
  375. "\342\200\236\302\261 "
  376. "\303\245\302\247\342\200\235\303\245\302\244\342\200\223\303\245\305\240"
  377. "\302\240\303\245\302\267\302\245\303\247\342\200\235\302\263\303\250\302"
  378. "\257\302\267\303\245\302\215\342\200\242 "
  379. "\303\251\342\200\241\342\200\230\303\246\342\202\254\302\235\303\247\342"
  380. "\200\236\302\261\303\247\305\241\342\200\236\303\245\302\247\342\200\235"
  381. "\303\245\302\244\342\200\223\303\245\305\240\302\240\303\245\302\267\302"
  382. "\245\303\247\342\200\235\302\263\303\250\302\257\302\267\303\245\302\215"
  383. "\342\200\242 "
  384. "\303\251\342\200\241\342\200\230\303\246\342\202\254\302\235\303\247\342"
  385. "\200\236\302\261\'s\303\245\302\247\342\200\235\303\245\302\244\342\200"
  386. "\223\303\245\305\240\302\240\303\245\302\267\302\245\303\247\342\200\235"
  387. "\302\263\303\250\302\257\302\267\303\245\302\215\342\200\242 "
  388. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\245\342\200\246"
  389. "\302\254\303\245\302\217\302\270\303\245\302\220\302\215\303\247\302\247"
  390. "\302\260 "
  391. "\303\246\302\265\302\267\303\247\302\273\302\264\303\246\342\200\223\302"
  392. "\257\303\247\342\200\260\302\271\303\246\302\261\302\275\303\250\302\275"
  393. "\302\246\303\245\302\267\302\245\303\247\302\250\342\200\271 "
  394. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\251\306\222\302"
  395. "\250\303\251\342\200\224\302\250 "
  396. "\303\247\342\200\235\305\270\303\244\302\272\302\247\303\250\302\256\302"
  397. "\241\303\245\313\206\342\200\231 "
  398. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\246\342\200\224"
  399. "\302\245\303\246\305\223\305\270 "
  400. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\245\302\215\342"
  401. "\200\242\303\245\302\217\302\267 "
  402. "\303\251\342\200\241\342\200\241\303\250\302\264\302\255\303\246\313\234"
  403. "\305\275\303\247\302\273\342\200\240 "
  404. "\303\247\342\200\260\302\251\303\245\342\200\234\302\201\303\245\302\220"
  405. "\302\215\303\247\302\247\302\260 "
  406. "\303\245\342\200\260\302\215\303\245\302\220\305\275\303\251\342\200\224"
  407. "\302\250\303\251\342\204\242\302\220\303\244\302\275\302\215\303\245\302"
  408. "\274\342\202\254\303\245\342\200\246\302\263 "
  409. "\303\246\342\200\242\302\260\303\251\342\200\241\302\217 "
  410. "\303\245\313\206\302\260\303\250\302\264\302\247\303\246\342\200\224\302"
  411. "\266\303\251\342\200\224\302\264 "
  412. "\303\245\302\220\313\206\303\245\302\220\305\222\303\245\302\217\302\267"
  413. " HT2018-12-020 "
  414. "\303\245\305\240\302\240\303\245\302\267\302\245\303\250\302\246\302\201"
  415. "\303\246\302\261\342\200\232 "
  416. "\303\246\305\222\342\200\260\303\246\342\200\242\302\260\303\246\302\215"
  417. "\302\256\303\245\305\240\302\240\303\245\302\267\302\245\303\245\313\206"
  418. "\302\266\303\244\302\275\305\223\n\\\\192.168.1.2\\public2\\2018."
  419. "12\\HT2018-12-"
  420. "020971\303\246\302\246\342\200\232\303\245\302\277\302\265\303\250\302"
  421. "\275\302\246\\\303\245\305\240\302\240\303\245\302\267\302\245\303\246"
  422. "\342\200\242\302\260\303\246\302\215\302\256\\SC\\SH190311-11-"
  423. "971XianWeiKaiGuanDiZuoJiaQiangSeCaiDingYi\303\257\302\274\313\2062019031"
  424. "1\303\257\302\274\342\200\260 "
  425. "\303\245\302\244\342\200\241\303\246\302\263\302\250 "
  426. "\303\246\302\263\302\250\303\257\302\274\305\2412."
  427. "5\303\247\305\241\342\200\236\303\245\302\255\342\200\235\303\246\342"
  428. "\200\235\302\273M3\303\247\305\241\342\200\236\303\247\342\200\260\342"
  429. "\204\242\303\257\302\274\305\222\303\251\302\242\342\200\236\303\247\302"
  430. "\256\342\200\224\303\245\302\217\302\267\303\257\302\274\305\241YS-XM-"
  431. "201812-020 \303\251\342\204\242\342\200\236\303\244\302\273\302\266"};
  432. const std::string doc_text3{
  433. " \303\250\306\222\302\241\303\244\302\270\342\200\223\303\246\302\235"
  434. "\302\260 "
  435. "\303\246\305\240\342\202\254\303\246\305\223\302\257\303\245\302\267\302"
  436. "\245\303\247\302\250\342\200\271\303\251\306\222\302\250\303\251\342\200"
  437. "\241\342\200\241\303\250\302\264\302\255\303\247\342\200\235\302\263\303"
  438. "\250\302\257\302\267 "
  439. "\303\250\306\222\302\241\303\244\302\270\342\200\223\303\246\302\235\302"
  440. "\260\303\247\305\241\342\200\236\303\246\305\240\342\202\254\303\246\305"
  441. "\223\302\257\303\245\302\267\302\245\303\247\302\250\342\200\271\303\251"
  442. "\306\222\302\250\303\251\342\200\241\342\200\241\303\250\302\264\302\255"
  443. "\303\247\342\200\235\302\263\303\250\302\257\302\267 "
  444. "\303\250\306\222\302\241\303\244\302\270\342\200\223\303\246\302\235\302"
  445. "\260\'s\303\246\305\240\342\202\254\303\246\305\223\302\257\303\245\302"
  446. "\267\302\245\303\247\302\250\342\200\271\303\251\306\222\302\250\303\251"
  447. "\342\200\241\342\200\241\303\250\302\264\302\255\303\247\342\200\235\302"
  448. "\263\303\250\302\257\302\267 "
  449. "\303\251\305\223\342\202\254\303\246\302\261\342\200\232\303\251\306\222"
  450. "\302\250\303\251\342\200\224\302\250 "
  451. "\303\245\302\267\302\245\303\247\302\250\342\200\271\303\251\306\222\302"
  452. "\250 "
  453. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\246\342\200\224"
  454. "\302\245\303\246\305\223\305\270 "
  455. "\303\251\342\200\241\342\200\241\303\250\302\264\302\255\303\246\313\234"
  456. "\305\275\303\247\302\273\342\200\240 "
  457. "\303\251\302\242\342\200\236\303\247\302\256\342\200\224\303\245\302\215"
  458. "\342\200\242\303\245\302\217\302\267 YS-XM-201812-020 "
  459. "\303\245\305\276\342\200\271\303\245\302\217\302\267\303\246\313\206\342"
  460. "\200\223\303\250\302\247\342\200\236\303\246\302\240\302\274 "
  461. "\303\246\342\204\242\302\256\303\251\342\202\254\305\241\303\247\302\201"
  462. "\302\257\303\245\302\270\302\246\303\251\342\200\241\342\200\241\303\250"
  463. "\302\264\302\255 "
  464. "\303\245\302\220\313\206\303\245\302\220\305\222\303\245\302\217\302\267"
  465. " HT-2018-12-020 "
  466. "\303\246\342\200\242\302\260\303\251\342\200\241\302\217 "
  467. "\303\245\302\215\342\200\242\303\244\302\273\302\267 "
  468. "\303\251\342\200\241\342\200\230\303\251\302\242\302\235 "
  469. "\303\247\342\200\235\302\250\303\251\342\202\254\342\200\235 "
  470. "971\303\246\342\204\242\302\256\303\251\342\202\254\305\241\303\247\302"
  471. "\201\302\257\303\245\302\270\302\246\303\251\342\200\241\342\200\241\303"
  472. "\250\302\264\302\255\303\244\302\270\342\200\271\303\245\302\215\342\200"
  473. "\242 "
  474. "\303\245\313\206\302\260\303\250\302\264\302\247\303\246\342\200\224\302"
  475. "\245\303\246\305\223\305\270 "
  476. "\303\245\302\272\342\200\234\303\245\302\255\313\234\303\246\342\200\242"
  477. "\302\260\303\251\342\200\241\302\217 "
  478. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\244\302\272\302"
  479. "\272 "
  480. "\303\250\306\222\302\241\303\244\302\270\342\200\223\303\246\302\235\302"
  481. "\260 \303\251\342\204\242\342\200\236\303\244\302\273\302\266"};
  482. std::string doc_text4{
  483. " \303\247\305\275\342\200\271\303\250\342\200\271\342\200\224\303\250"
  484. "\342\200\271\342\200\224 "
  485. "\303\245\302\247\342\200\235\303\245\302\244\342\200\223\303\245\305\240"
  486. "\302\240\303\245\302\267\302\245\303\247\342\200\235\302\263\303\250\302"
  487. "\257\302\267\303\245\302\215\342\200\242 "
  488. "\303\247\305\275\342\200\271\303\250\342\200\271\342\200\224\303\250\342"
  489. "\200\271\342\200\224\303\247\305\241\342\200\236\303\245\302\247\342\200"
  490. "\235\303\245\302\244\342\200\223\303\245\305\240\302\240\303\245\302\267"
  491. "\302\245\303\247\342\200\235\302\263\303\250\302\257\302\267\303\245\302"
  492. "\215\342\200\242 "
  493. "\303\247\305\275\342\200\271\303\250\342\200\271\342\200\224\303\250\342"
  494. "\200\271\342\200\224\'s\303\245\302\247\342\200\235\303\245\302\244\342"
  495. "\200\223\303\245\305\240\302\240\303\245\302\267\302\245\303\247\342\200"
  496. "\235\302\263\303\250\302\257\302\267\303\245\302\215\342\200\242 "
  497. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\245\342\200\246"
  498. "\302\254\303\245\302\217\302\270\303\245\302\220\302\215\303\247\302\247"
  499. "\302\260 "
  500. "\303\246\302\265\302\267\303\247\302\273\302\264\303\246\342\200\223\302"
  501. "\257\303\247\342\200\260\302\271\303\246\302\261\302\275\303\250\302\275"
  502. "\302\246\303\245\302\267\302\245\303\247\302\250\342\200\271 "
  503. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\251\306\222\302"
  504. "\250\303\251\342\200\224\302\250 "
  505. "\303\251\342\200\241\342\200\241\303\250\302\264\302\255 "
  506. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\246\342\200\224"
  507. "\302\245\303\246\305\223\305\270 "
  508. "\303\247\342\200\235\302\263\303\250\302\257\302\267\303\245\302\215\342"
  509. "\200\242\303\245\302\217\302\267 "
  510. "\303\251\342\200\241\342\200\241\303\250\302\264\302\255\303\246\313\234"
  511. "\305\275\303\247\302\273\342\200\240 "
  512. "\303\247\342\200\260\302\251\303\245\342\200\234\302\201\303\245\302\220"
  513. "\302\215\303\247\302\247\302\260 "
  514. "\303\251\302\241\302\266\303\246\302\243\305\241\303\246\305\223\302\272"
  515. "\303\245\342\204\242\302\250\303\244\302\272\302\272VVC\303\246\302\235"
  516. "\302\220\303\246\342\200\223\342\204\242 "
  517. "\303\246\342\200\242\302\260\303\251\342\200\241\302\217 "
  518. "\303\245\313\206\302\260\303\250\302\264\302\247\303\246\342\200\224\302"
  519. "\266\303\251\342\200\224\302\264 "
  520. "\303\245\302\220\313\206\303\245\302\220\305\222\303\245\302\217\302\267"
  521. " HT2018-12-020 "
  522. "\303\245\305\240\302\240\303\245\302\267\302\245\303\250\302\246\302\201"
  523. "\303\246\302\261\342\200\232 "
  524. "\303\245\302\244\342\200\241\303\246\302\263\302\250 "
  525. "HT2018-12-"
  526. "020971\303\246\302\246\342\200\232\303\245\302\277\302\265\303\250\302"
  527. "\275\302\246-"
  528. "\303\251\302\241\302\266\303\246\302\243\305\241\303\246\305\223\302\272"
  529. "\303\245\342\204\242\302\250\303\244\302\272\302\272-"
  530. "\303\251\302\273\342\200\230\303\250\342\200\260\302\262VVC\303\246\302"
  531. "\235\302\220\303\246\342\200\223\342\204\242-"
  532. "\303\245\342\200\246\302\261\303\250\302\256\302\2415\303\244\302\273"
  533. "\302\266\303\243\342\202\254\342\200\232\303\244\302\276\342\200\272\303"
  534. "\245\302\272\342\200\235\303\245\342\200\242\342\200\240\303\245\302\263"
  535. "\302\273\303\245\302\256\302\270\303\246\305\240\302\245\303\244\302\273"
  536. "\302\267190\303\245\342\200\246\306\222\303\257\302\274\313\2063.9/"
  537. "\303\245\342\200\246\342\200\271\303\257\302\274\342\200\260\303\257\302"
  538. "\274\305\222\303\244\302\272\302\244\303\246\305\223\305\2703/"
  539. "13\303\245\302\217\302\267\303\243\342\202\254\342\200\232\303\245\302"
  540. "\217\342\200\230\303\245\302\263\302\273\303\245\302\256\302\270\303\257"
  541. "\302\274\305\222\303\250\302\257\302\267\303\251\302\242\342\200\240\303"
  542. "\245\302\257\302\274\303\245\342\200\231\305\222\303\251\342\204\242\313"
  543. "\206\303\246\342\202\254\302\273\303\246\342\200\260\302\271\303\245\342"
  544. "\200\241\342\200\240\303\257\302\274\305\222\303\250\302\260\302\242\303"
  545. "\250\302\260\302\242\303\257\302\274\302\201 "
  546. "\303\251\342\204\242\342\200\236\303\244\302\273\302\266"};
  547. std::string doc_text5{
  548. " \303\251\342\204\242\313\206\303\250\305\275\342\200\260 "
  549. "\303\246\305\240\302\245\303\245\302\272\305\270\303\245\302\215\342\200"
  550. "\242 "
  551. "\303\251\342\204\242\313\206\303\250\305\275\342\200\260\303\247\305\241"
  552. "\342\200\236\303\246\305\240\302\245\303\245\302\272\305\270\303\245\302"
  553. "\215\342\200\242 "
  554. "\303\251\342\204\242\313\206\303\250\305\275\342\200\260\'s\303\246\305"
  555. "\240\302\245\303\245\302\272\305\270\303\245\302\215\342\200\242 "
  556. "NO\303\247\302\274\342\200\223\303\245\302\217\302\267 HVBF2019-0312 "
  557. "\303\245\302\220\313\206\303\245\302\220\305\222\303\245\302\217\302\267"
  558. " HT2018-12-"
  559. "020971\303\246\302\246\342\200\232\303\245\302\277\302\265\303\250\302"
  560. "\275\302\246 "
  561. "\303\246\305\240\302\245\303\245\302\272\305\270\303\245\342\200\240\342"
  562. "\200\246\303\245\302\256\302\271\303\245\302\217\305\240\303\245\305\275"
  563. "\305\270\303\245\342\200\272\302\240 "
  564. "\303\245\302\267\302\246\303\245\342\200\260\302\215\303\251\342\200\224"
  565. "\302\250\303\251\302\245\302\260\303\246\302\235\302\241\303\257\302\274"
  566. "\313\2061106X129X91\303\257\302\274\342\200\260\303\257\302\274\342\200"
  567. "\272\303\245\302\217\302\263\303\245\342\200\260\302\215\303\251\342\200"
  568. "\224\302\250\303\251\302\245\302\260\303\246\302\235\302\241\303\257\302"
  569. "\274\313\2061106X129X91\303\257\302\274\342\200\260\303\257\302\274\342"
  570. "\200\272\n\303\245\342\200\272\302\240\303\246\305\223\302\252\303\247"
  571. "\342\200\242\342\204\242\303\245\305\222\302\271\303\251\342\200\246\302"
  572. "\215\303\244\302\275\342\204\242\303\251\342\200\241\302\217\303\257\302"
  573. "\274\305\222\303\245\302\256\305\276\303\251\342\204\242\342\200\246\303"
  574. "\250\302\243\342\200\246\303\251\342\200\246\302\215\303\246\302\257\342"
  575. "\200\235\303\247\305\275\302\273\303\247\342\200\231\306\222\303\247\305"
  576. "\270\302\255\303\244\302\272\342\200\240\303\244\302\270\342\202\254\303"
  577. "\247\342\200\232\302\271\303\257\302\274\305\222\303\251\305\223\342\202"
  578. "\254\303\246\305\240\302\245\303\245\302\272\305\270\303\251\342\200\241"
  579. "\302\215\303\246\342\200\223\302\260\303\245\313\206\302\266\303\244\302"
  580. "\275\305\223\303\243\342\202\254\342\200\232 "
  581. "\303\251\342\200\272\302\266\303\244\302\273\302\266\303\247\302\274\342"
  582. "\200\223\303\245\302\217\302\267 201812020G0061-001201812020G0061-002 "
  583. "\303\250\302\264\302\243\303\244\302\273\302\273\303\244\302\272\302\272"
  584. " \303\247\305\275\342\200\271\303\251\342\200\241\342\200\230\303\245"
  585. "\302\272\342\200\240 "
  586. "\303\251\306\222\302\250\303\251\342\200\224\302\250 "
  587. "\303\245\302\267\302\245\303\247\302\250\342\200\271 "
  588. "\303\245\302\241\302\253\303\245\302\215\342\200\242\303\244\302\272\302"
  589. "\272 "
  590. "\303\247\305\275\342\200\271\303\251\342\200\241\342\200\230\303\245\302"
  591. "\272\342\200\240 "
  592. "\303\245\302\241\302\253\303\245\302\215\342\200\242\303\246\342\200\224"
  593. "\302\266\303\251\342\200\224\302\264 "
  594. "\303\246\305\275\302\245\303\245\302\215\342\200\242\303\251\306\222\302"
  595. "\250\303\251\342\200\224\302\250\303\250\302\264\305\270\303\250\302\264"
  596. "\302\243\303\244\302\272\302\272 "
  597. "\303\251\342\200\241\302\215\303\246\342\200\223\302\260\303\245\313\206"
  598. "\302\266\303\244\302\275\305\223\303\247\302\241\302\256\303\250\302\256"
  599. "\302\244 "
  600. "\303\247\302\241\302\256\303\250\302\256\302\244\303\244\302\272\302\272"
  601. " "
  602. "\303\247\302\241\302\256\303\250\302\256\302\244\303\246\342\200\224\302"
  603. "\266\303\251\342\200\224\302\264 "
  604. "\303\246\342\200\260\342\202\254\303\251\305\223\342\202\254\303\246\302"
  605. "\235\302\220\303\246\342\200\223\342\204\242 "
  606. "\303\246\342\200\260\342\202\254\303\251\305\223\342\202\254\303\246\302"
  607. "\235\302\220\303\246\342\200\223\342\204\242\303\246\342\200\224\302\266"
  608. "\303\251\342\200\224\302\264 "
  609. "\303\246\302\235\302\220\303\246\342\200\223\342\204\242\303\250\302\264"
  610. "\302\271 "
  611. "\303\244\302\272\302\272\303\245\302\267\302\245\303\250\302\264\302\271"
  612. " "
  613. "\303\245\302\220\313\206\303\250\302\256\302\241\303\250\302\264\302\271"
  614. "\303\247\342\200\235\302\250 "
  615. "\303\245\302\256\305\222\303\246\313\206\302\220\303\246\342\200\224\302"
  616. "\266\303\251\342\200\224\302\264 "
  617. "\303\245\302\256\305\222\303\246\313\206\302\220\303\246\306\222\342\200"
  618. "\246\303\245\342\200\240\302\265 "
  619. "\303\250\302\264\302\250\303\246\302\243\342\202\254\303\247\302\255\302"
  620. "\276\303\245\302\255\342\200\224\303\247\302\241\302\256\303\250\302\256"
  621. "\302\244 "
  622. "\303\250\302\264\302\250\303\246\302\243\342\202\254\303\247\302\255\302"
  623. "\276\303\245\302\255\342\200\224\303\246\342\200\224\302\245\303\246\305"
  624. "\223\305\270 \303\250\302\257\342\200\236\303\244\302\273\302\267 "
  625. "\303\247\302\275\305\241\303\246\302\254\302\276100\303\245\342\200\246"
  626. "\306\222 \303\251\342\204\242\342\200\236\303\244\302\273\302\266 "
  627. "\303\250\302\257\302\264\303\246\313\234\305\275"};
  628. documents.push_back(TestUtil::NewDocument(GetDocumentID(), doc_text1, base,
  629. base + 100, base + 69));
  630. documents.push_back(TestUtil::NewDocument(GetDocumentID(), doc_text2,
  631. base + 1, base + 101, base + 69));
  632. documents.push_back(TestUtil::NewDocument(GetDocumentID(), doc_text3,
  633. base + 2, base + 102, base + 69));
  634. documents.push_back(TestUtil::NewDocument(GetDocumentID(), doc_text4,
  635. base + 3, base + 103, base + 69));
  636. documents.push_back(TestUtil::NewDocument(GetDocumentID(), doc_text5,
  637. base + 4, base + 104, base + 69));
  638. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  639. nullptr, nullptr);
  640. EXPECT_TRUE(ret);
  641. SearchLogDebug("TextQueryUsingTokenize AddOrUpdateDocuments table id(%s)",
  642. table.PrintToStr().c_str());
  643. for (auto du : documents) {
  644. EXPECT_EQ(0, du->Status().GetCode());
  645. }
  646. IndexConfig &index_config = index->Config();
  647. Tokenizer *tokenizer = index_config.GetTokenizer();
  648. wwsearch::Searcher searcher(&index_config);
  649. {
  650. std::string match_text1{"HT2018-12-020"};
  651. match_documentsid.clear();
  652. wwsearch::AndQuery query;
  653. std::set<std::string> terms;
  654. EXPECT_TRUE(
  655. tokenizer->BuildTerms(match_text1.c_str(), match_text1.size(), terms));
  656. SearchLogDebug("match_text1 tokenizer terms(%s)",
  657. JoinContainerToString(terms, ";").c_str());
  658. for (auto &term : terms) {
  659. wwsearch::BooleanQuery *sub_query =
  660. new wwsearch::BooleanQuery(1, std::move(term));
  661. query.AddQuery(sub_query);
  662. }
  663. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  664. match_documentsid);
  665. SearchLogDebug("DoQuery match_documentsid(%s)",
  666. JoinContainerToString(match_documentsid, ";").c_str());
  667. EXPECT_EQ(0, status.GetCode());
  668. EXPECT_EQ(3, match_documentsid.size());
  669. }
  670. {
  671. std::string match_text2{"HT-2018-12-020"};
  672. match_documentsid.clear();
  673. wwsearch::AndQuery query;
  674. std::set<std::string> terms;
  675. EXPECT_TRUE(
  676. tokenizer->BuildTerms(match_text2.c_str(), match_text2.size(), terms));
  677. SearchLogDebug("match_text2 tokenizer terms(%s)",
  678. JoinContainerToString(terms, ";").c_str());
  679. for (auto &term : terms) {
  680. wwsearch::BooleanQuery *sub_query =
  681. new wwsearch::BooleanQuery(1, std::move(term));
  682. query.AddQuery(sub_query);
  683. }
  684. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  685. match_documentsid);
  686. SearchLogDebug("DoQuery match_documentsid(%s)",
  687. JoinContainerToString(match_documentsid, ";").c_str());
  688. EXPECT_EQ(0, status.GetCode());
  689. EXPECT_EQ(1, match_documentsid.size());
  690. }
  691. }
  692. TEST_F(BoolQueryTest, Query_String_not_get_all) {
  693. auto base = GetNumeric(10000);
  694. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "worlda", base,
  695. base + 100, base + 69));
  696. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "xxxxx", base + 1,
  697. base + 101, base + 69));
  698. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "worlda", base + 2,
  699. base + 102, base + 69));
  700. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  701. nullptr, nullptr);
  702. EXPECT_TRUE(ret);
  703. wwsearch::Searcher searcher(&index->Config());
  704. SearchTracer tracer;
  705. {
  706. match_documentsid.clear();
  707. wwsearch::BooleanQuery query1(1, "worlda");
  708. auto status =
  709. searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  710. match_documentsid, nullptr, 2000, 0, &tracer, nullptr);
  711. EXPECT_EQ(0, status.GetCode());
  712. EXPECT_EQ(2, match_documentsid.size());
  713. if (g_debug) {
  714. for (auto documentid : match_documentsid) {
  715. SearchLogDebug("match[%llu]\n", documentid);
  716. }
  717. }
  718. }
  719. {
  720. match_documentsid.clear();
  721. wwsearch::BooleanQuery query1(1, "worlda");
  722. auto status =
  723. searcher.DoQuery(table, query1, 0, 1, nullptr, nullptr,
  724. match_documentsid, nullptr, 2000, 0, &tracer, nullptr);
  725. EXPECT_EQ(0, status.GetCode());
  726. EXPECT_EQ(1, match_documentsid.size());
  727. if (g_debug) {
  728. for (auto documentid : match_documentsid) {
  729. SearchLogDebug("match[%llu]\n", documentid);
  730. }
  731. }
  732. }
  733. {
  734. uint32_t get_match_total_cnt = 0;
  735. match_documentsid.clear();
  736. wwsearch::BooleanQuery query1(1, "worlda");
  737. auto status = searcher.DoQuery(table, query1, 0, 1, nullptr, nullptr,
  738. match_documentsid, nullptr, 2000, 0, &tracer,
  739. &get_match_total_cnt);
  740. EXPECT_EQ(0, status.GetCode());
  741. EXPECT_EQ(1, match_documentsid.size());
  742. EXPECT_EQ(2, get_match_total_cnt);
  743. if (g_debug) {
  744. for (auto documentid : match_documentsid) {
  745. SearchLogDebug("match[%llu]\n", documentid);
  746. }
  747. }
  748. }
  749. }
  750. } // namespace wwsearch