or_and_query_unit.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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 OrAndQueryTest : public ::testing::Test {
  28. public:
  29. static DefaultIndexWrapper *index;
  30. static uint64_t document_id;
  31. static uint64_t numeric_value;
  32. static wwsearch::TableID table;
  33. std::vector<DocumentUpdater *> documents;
  34. std::list<DocumentID> match_documentsid;
  35. public:
  36. OrAndQueryTest() {}
  37. virtual ~OrAndQueryTest() {}
  38. static void SetUpTestCase() {
  39. index = new DefaultIndexWrapper();
  40. index->DBParams().path =
  41. std::string("/tmp/unit_") + std::string("boolquery");
  42. index->Config().SetLogLevel(g_debug ? wwsearch::kSearchLogLevelDebug
  43. : wwsearch::kSearchLogLevelError);
  44. auto status = index->Open(g_use_rocksdb, g_use_compression);
  45. ASSERT_TRUE(status.GetCode() == 0);
  46. }
  47. static void TearDownTestCase() {
  48. if (index != nullptr) {
  49. index->vdb_->DropDB();
  50. delete index;
  51. index = nullptr;
  52. }
  53. }
  54. virtual void SetUp() override {
  55. table.business_type++;
  56. table.partition_set++;
  57. documents.clear();
  58. match_documentsid.clear();
  59. }
  60. void VariableChange() {
  61. table.business_type++;
  62. table.partition_set++;
  63. documents.clear();
  64. match_documentsid.clear();
  65. }
  66. virtual void TearDown() override {
  67. for (auto du : documents) {
  68. delete du;
  69. }
  70. documents.clear();
  71. match_documentsid.clear();
  72. }
  73. uint64_t GetDocumentID() { return document_id++; }
  74. uint64_t GetNumeric(uint64_t alloc_len = 1000) {
  75. auto temp = numeric_value;
  76. numeric_value += alloc_len;
  77. return temp;
  78. }
  79. private:
  80. };
  81. DefaultIndexWrapper *OrAndQueryTest::index = nullptr;
  82. DocumentID OrAndQueryTest::document_id = 1;
  83. DocumentID OrAndQueryTest::numeric_value = 1;
  84. wwsearch::TableID OrAndQueryTest::table{1, 100};
  85. TEST_F(OrAndQueryTest, Query_String) {
  86. VariableChange();
  87. auto base = GetNumeric(10000);
  88. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloa", base,
  89. base + 100, base + 69));
  90. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "girla", base + 1,
  91. base + 101, base + 69));
  92. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloa", base + 2,
  93. base + 102, base + 69));
  94. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  95. nullptr, nullptr);
  96. EXPECT_TRUE(ret);
  97. if (g_debug) {
  98. for (const auto &du : documents) {
  99. EXPECT_EQ(0, du->Status().GetCode());
  100. wwsearch::Document &document = du->New();
  101. std::string debug_str;
  102. document.PrintToReadStr(debug_str);
  103. SearchLogDebug("%s\n", debug_str.c_str());
  104. }
  105. }
  106. wwsearch::Searcher searcher(&index->Config());
  107. {
  108. match_documentsid.clear();
  109. wwsearch::BooleanQuery query1(1, "helloa");
  110. auto status = searcher.DoQuery(table, query1, 0, 100, nullptr, nullptr,
  111. match_documentsid);
  112. EXPECT_EQ(0, status.GetCode());
  113. EXPECT_EQ(2, match_documentsid.size());
  114. SearchLogDebug(
  115. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  116. table.business_type, table.partition_set,
  117. JoinContainerToString(match_documentsid, ", ").c_str());
  118. }
  119. {
  120. match_documentsid.clear();
  121. wwsearch::BooleanQuery query2(1, "girla");
  122. auto status = searcher.DoQuery(table, query2, 0, 100, nullptr, nullptr,
  123. match_documentsid);
  124. EXPECT_EQ(0, status.GetCode());
  125. EXPECT_EQ(1, match_documentsid.size());
  126. SearchLogDebug(
  127. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  128. table.business_type, table.partition_set,
  129. JoinContainerToString(match_documentsid, ", ").c_str());
  130. }
  131. {
  132. match_documentsid.clear();
  133. wwsearch::BooleanQuery query3(1, "nothing1");
  134. auto status = searcher.DoQuery(table, query3, 0, 100, nullptr, nullptr,
  135. match_documentsid);
  136. EXPECT_EQ(0, status.GetCode());
  137. EXPECT_EQ(0, match_documentsid.size());
  138. SearchLogDebug(
  139. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  140. table.business_type, table.partition_set,
  141. JoinContainerToString(match_documentsid, ", ").c_str());
  142. }
  143. }
  144. TEST_F(OrAndQueryTest, And_Query_String) {
  145. VariableChange();
  146. auto base = GetNumeric(10000);
  147. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloc worldc",
  148. base, base + 100, base + 69));
  149. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "girlc worldc",
  150. base + 1, base + 101, base + 69));
  151. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "helloc worldc",
  152. base + 2, base + 102, base + 69));
  153. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  154. nullptr, nullptr);
  155. EXPECT_TRUE(ret);
  156. for (auto du : documents) {
  157. EXPECT_EQ(0, du->Status().GetCode());
  158. }
  159. wwsearch::Searcher searcher(&index->Config());
  160. {
  161. match_documentsid.clear();
  162. wwsearch::BooleanQuery query1(1, "helloc");
  163. wwsearch::BooleanQuery query2(1, "worldc");
  164. wwsearch::AndQuery query;
  165. query.AddQuery(&query1);
  166. query.AddQuery(&query2);
  167. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  168. match_documentsid);
  169. EXPECT_EQ(0, status.GetCode());
  170. EXPECT_EQ(2, match_documentsid.size());
  171. SearchLogDebug(
  172. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  173. table.business_type, table.partition_set,
  174. JoinContainerToString(match_documentsid, ", ").c_str());
  175. }
  176. {
  177. match_documentsid.clear();
  178. wwsearch::BooleanQuery query1(1, "girlc");
  179. wwsearch::BooleanQuery query2(1, "worldc");
  180. wwsearch::AndQuery query;
  181. query.AddQuery(&query1);
  182. query.AddQuery(&query2);
  183. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  184. match_documentsid);
  185. EXPECT_EQ(0, status.GetCode());
  186. EXPECT_EQ(1, match_documentsid.size());
  187. SearchLogDebug(
  188. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  189. table.business_type, table.partition_set,
  190. JoinContainerToString(match_documentsid, ", ").c_str());
  191. }
  192. {
  193. match_documentsid.clear();
  194. wwsearch::BooleanQuery query1(1, "girlc");
  195. wwsearch::BooleanQuery query2(1, "noexist3");
  196. wwsearch::AndQuery query;
  197. query.AddQuery(&query1);
  198. query.AddQuery(&query2);
  199. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  200. match_documentsid);
  201. EXPECT_EQ(0, status.GetCode());
  202. EXPECT_EQ(0, match_documentsid.size());
  203. SearchLogDebug(
  204. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  205. table.business_type, table.partition_set,
  206. JoinContainerToString(match_documentsid, ", ").c_str());
  207. }
  208. }
  209. TEST_F(OrAndQueryTest, Or_Query_String) {
  210. VariableChange();
  211. auto base = GetNumeric(10000);
  212. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "hellod worldd",
  213. base, base + 100, base + 69));
  214. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "hellod girld",
  215. base + 1, base + 101, base + 69));
  216. documents.push_back(TestUtil::NewDocument(GetDocumentID(), "hellod worldd",
  217. base + 2, base + 102, base + 69));
  218. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  219. nullptr, nullptr);
  220. EXPECT_TRUE(ret);
  221. for (auto du : documents) {
  222. EXPECT_EQ(0, du->Status().GetCode());
  223. }
  224. wwsearch::Searcher searcher(&index->Config());
  225. {
  226. match_documentsid.clear();
  227. wwsearch::BooleanQuery query1(1, "hellod");
  228. wwsearch::BooleanQuery query2(1, "worldd");
  229. wwsearch::OrQuery query;
  230. query.AddQuery(&query1);
  231. query.AddQuery(&query2);
  232. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  233. match_documentsid);
  234. EXPECT_EQ(0, status.GetCode());
  235. EXPECT_EQ(3, match_documentsid.size());
  236. SearchLogDebug(
  237. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  238. table.business_type, table.partition_set,
  239. JoinContainerToString(match_documentsid, ", ").c_str());
  240. }
  241. {
  242. match_documentsid.clear();
  243. wwsearch::BooleanQuery query1(1, "girld");
  244. wwsearch::BooleanQuery query2(1, "worldd");
  245. wwsearch::OrQuery query;
  246. query.AddQuery(&query1);
  247. query.AddQuery(&query2);
  248. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  249. match_documentsid);
  250. EXPECT_EQ(0, status.GetCode());
  251. EXPECT_EQ(3, match_documentsid.size());
  252. SearchLogDebug(
  253. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  254. table.business_type, table.partition_set,
  255. JoinContainerToString(match_documentsid, ", ").c_str());
  256. }
  257. {
  258. match_documentsid.clear();
  259. wwsearch::BooleanQuery query1(1, "girld");
  260. wwsearch::BooleanQuery query2(1, "noexist4");
  261. wwsearch::OrQuery query;
  262. query.AddQuery(&query1);
  263. query.AddQuery(&query2);
  264. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  265. match_documentsid);
  266. EXPECT_EQ(0, status.GetCode());
  267. EXPECT_EQ(1, match_documentsid.size());
  268. SearchLogDebug(
  269. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  270. table.business_type, table.partition_set,
  271. JoinContainerToString(match_documentsid, ", ").c_str());
  272. }
  273. }
  274. TEST_F(OrAndQueryTest, And_Query_Multi_String) {
  275. VariableChange();
  276. auto base = GetNumeric(10000);
  277. std::vector<TestUtil::FieldIdStrPair> field_id_str_list1{
  278. {1, "a"}, {2, "b"}, {3, "c"}};
  279. std::vector<TestUtil::FieldIdStrPair> field_id_str_list2{
  280. {4, "d"}, {5, "e"}, {6, "f"}};
  281. std::vector<TestUtil::FieldIdStrPair> field_id_str_list3{
  282. {7, "g"}, {8, "h"}, {9, "i"}};
  283. documents.push_back(
  284. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list1));
  285. documents.push_back(
  286. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list2));
  287. documents.push_back(
  288. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list3));
  289. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  290. nullptr, nullptr);
  291. EXPECT_TRUE(ret);
  292. for (auto du : documents) {
  293. EXPECT_EQ(0, du->Status().GetCode());
  294. }
  295. wwsearch::Searcher searcher(&index->Config());
  296. {
  297. match_documentsid.clear();
  298. wwsearch::BooleanQuery query1(1, "a");
  299. wwsearch::BooleanQuery query2(2, "c");
  300. wwsearch::AndQuery query;
  301. query.AddQuery(&query1);
  302. query.AddQuery(&query2);
  303. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  304. match_documentsid);
  305. EXPECT_EQ(0, status.GetCode());
  306. EXPECT_EQ(0, match_documentsid.size());
  307. SearchLogDebug(
  308. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  309. table.business_type, table.partition_set,
  310. JoinContainerToString(match_documentsid, ", ").c_str());
  311. }
  312. {
  313. match_documentsid.clear();
  314. wwsearch::BooleanQuery query1(1, "a");
  315. wwsearch::BooleanQuery query2(3, "c");
  316. wwsearch::AndQuery query;
  317. query.AddQuery(&query1);
  318. query.AddQuery(&query2);
  319. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  320. match_documentsid);
  321. EXPECT_EQ(0, status.GetCode());
  322. EXPECT_EQ(1, match_documentsid.size());
  323. SearchLogDebug(
  324. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  325. table.business_type, table.partition_set,
  326. JoinContainerToString(match_documentsid, ", ").c_str());
  327. }
  328. }
  329. TEST_F(OrAndQueryTest, Or_Query_Multi_String) {
  330. VariableChange();
  331. auto base = GetNumeric(10000);
  332. std::vector<TestUtil::FieldIdStrPair> field_id_str_list1{
  333. {1, "a"}, {2, "b"}, {3, "c"}};
  334. std::vector<TestUtil::FieldIdStrPair> field_id_str_list2{
  335. {4, "d"}, {5, "e"}, {6, "f"}};
  336. std::vector<TestUtil::FieldIdStrPair> field_id_str_list3{
  337. {7, "g"}, {8, "h"}, {9, "i"}};
  338. documents.push_back(
  339. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list1));
  340. documents.push_back(
  341. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list2));
  342. documents.push_back(
  343. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list3));
  344. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  345. nullptr, nullptr);
  346. EXPECT_TRUE(ret);
  347. for (auto du : documents) {
  348. EXPECT_EQ(0, du->Status().GetCode());
  349. }
  350. wwsearch::Searcher searcher(&index->Config());
  351. {
  352. match_documentsid.clear();
  353. wwsearch::BooleanQuery query1(1, "a");
  354. wwsearch::BooleanQuery query2(2, "b");
  355. wwsearch::OrQuery query;
  356. query.AddQuery(&query1);
  357. query.AddQuery(&query2);
  358. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  359. match_documentsid);
  360. EXPECT_EQ(0, status.GetCode());
  361. EXPECT_EQ(1, match_documentsid.size());
  362. SearchLogDebug(
  363. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  364. table.business_type, table.partition_set,
  365. JoinContainerToString(match_documentsid, ", ").c_str());
  366. }
  367. {
  368. match_documentsid.clear();
  369. wwsearch::BooleanQuery query1(1, "a");
  370. wwsearch::BooleanQuery query2(4, "d");
  371. wwsearch::OrQuery query;
  372. query.AddQuery(&query1);
  373. query.AddQuery(&query2);
  374. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  375. match_documentsid);
  376. EXPECT_EQ(0, status.GetCode());
  377. EXPECT_EQ(2, match_documentsid.size());
  378. SearchLogDebug(
  379. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  380. table.business_type, table.partition_set,
  381. JoinContainerToString(match_documentsid, ", ").c_str());
  382. }
  383. {
  384. match_documentsid.clear();
  385. wwsearch::BooleanQuery query1(1, "a");
  386. wwsearch::BooleanQuery query2(2, "c");
  387. wwsearch::OrQuery query;
  388. query.AddQuery(&query1);
  389. query.AddQuery(&query2);
  390. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  391. match_documentsid);
  392. EXPECT_EQ(0, status.GetCode());
  393. EXPECT_EQ(1, match_documentsid.size());
  394. SearchLogDebug(
  395. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  396. table.business_type, table.partition_set,
  397. JoinContainerToString(match_documentsid, ", ").c_str());
  398. }
  399. {
  400. match_documentsid.clear();
  401. wwsearch::BooleanQuery query1(3, "a");
  402. wwsearch::BooleanQuery query2(4, "c");
  403. wwsearch::OrQuery query;
  404. query.AddQuery(&query1);
  405. query.AddQuery(&query2);
  406. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  407. match_documentsid);
  408. EXPECT_EQ(0, status.GetCode());
  409. EXPECT_EQ(0, match_documentsid.size());
  410. SearchLogDebug(
  411. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  412. table.business_type, table.partition_set,
  413. JoinContainerToString(match_documentsid, ", ").c_str());
  414. }
  415. {
  416. match_documentsid.clear();
  417. wwsearch::BooleanQuery query1(1, "a");
  418. wwsearch::BooleanQuery query2(4, "d");
  419. wwsearch::BooleanQuery query3(8, "h");
  420. wwsearch::OrQuery query;
  421. query.AddQuery(&query1);
  422. query.AddQuery(&query2);
  423. query.AddQuery(&query3);
  424. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  425. match_documentsid);
  426. EXPECT_EQ(0, status.GetCode());
  427. EXPECT_EQ(3, match_documentsid.size());
  428. SearchLogDebug(
  429. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  430. table.business_type, table.partition_set,
  431. JoinContainerToString(match_documentsid, ", ").c_str());
  432. }
  433. }
  434. TEST_F(OrAndQueryTest, UsePostScorerForPrefixTextQuery) {
  435. std::string keyword{"hello world"};
  436. std::string keyword2{"fucking work"};
  437. VariableChange();
  438. auto base = GetNumeric(10000);
  439. std::vector<TestUtil::FieldIdStrPair> field_id_str_list1{
  440. {1, "a"}, {2, "b"}, {3, "c"}};
  441. std::vector<TestUtil::FieldIdStrPair> field_id_str_list2{
  442. {4, "d"}, {5, "e"}, {6, keyword2}};
  443. std::vector<TestUtil::FieldIdStrPair> field_id_str_list3{
  444. {7, "g"}, {8, "h"}, {9, "i"}, {10, keyword}};
  445. documents.push_back(
  446. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list1));
  447. documents.push_back(
  448. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list2));
  449. documents.push_back(
  450. TestUtil::NewStringFieldDocument(GetDocumentID(), field_id_str_list3));
  451. bool ret = index->index_writer_->AddOrUpdateDocuments(table, documents,
  452. nullptr, nullptr);
  453. EXPECT_TRUE(ret);
  454. for (auto du : documents) {
  455. EXPECT_EQ(0, du->Status().GetCode());
  456. }
  457. wwsearch::Searcher searcher(&index->Config());
  458. {
  459. match_documentsid.clear();
  460. std::vector<std::shared_ptr<wwsearch::ScoreStrategy>> score_strategy_list{
  461. wwsearch::ScoreStrategyFactory::NewScoreStrategy(keyword, 2, 10)};
  462. wwsearch::PrefixQuery query1(10, "hel");
  463. wwsearch::PrefixQuery query2(10, "wor");
  464. wwsearch::AndQuery query;
  465. query.AddQuery(&query1);
  466. query.AddQuery(&query2);
  467. auto status = searcher.DoQuery(table, query, 0, 100, nullptr, nullptr,
  468. match_documentsid, &score_strategy_list, 20);
  469. EXPECT_EQ(0, status.GetCode());
  470. EXPECT_EQ(1, match_documentsid.size());
  471. SearchLogDebug(
  472. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  473. table.business_type, table.partition_set,
  474. JoinContainerToString(match_documentsid, ", ").c_str());
  475. }
  476. {
  477. match_documentsid.clear();
  478. std::vector<std::shared_ptr<wwsearch::ScoreStrategy>> score_strategy_list{
  479. wwsearch::ScoreStrategyFactory::NewScoreStrategy(keyword, 2, 10)};
  480. wwsearch::PrefixQuery query1(10, "hel");
  481. wwsearch::PrefixQuery query2(10, "wor");
  482. wwsearch::AndQuery sub_query1;
  483. sub_query1.AddQuery(&query1);
  484. sub_query1.AddQuery(&query2);
  485. wwsearch::PrefixQuery query3(6, "fuck");
  486. wwsearch::PrefixQuery query4(6, "wo");
  487. wwsearch::AndQuery sub_query2;
  488. sub_query2.AddQuery(&query3);
  489. sub_query2.AddQuery(&query4);
  490. wwsearch::OrQuery query;
  491. query.AddQuery(&sub_query1);
  492. query.AddQuery(&sub_query2);
  493. auto status = searcher.DoQuery(table, query, 0, 2, nullptr, nullptr,
  494. match_documentsid, &score_strategy_list, 20);
  495. EXPECT_EQ(0, status.GetCode());
  496. EXPECT_EQ(2, match_documentsid.size());
  497. SearchLogDebug(
  498. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  499. table.business_type, table.partition_set,
  500. JoinContainerToString(match_documentsid, ", ").c_str());
  501. }
  502. {
  503. match_documentsid.clear();
  504. std::vector<std::shared_ptr<wwsearch::ScoreStrategy>> score_strategy_list{
  505. wwsearch::ScoreStrategyFactory::NewScoreStrategy(keyword, 2, 10)};
  506. wwsearch::PrefixQuery query1(10, "hel");
  507. wwsearch::PrefixQuery query2(10, "wor");
  508. wwsearch::AndQuery sub_query1;
  509. sub_query1.AddQuery(&query1);
  510. sub_query1.AddQuery(&query2);
  511. wwsearch::PrefixQuery query3(6, "fuck");
  512. wwsearch::PrefixQuery query4(6, "wo");
  513. wwsearch::AndQuery sub_query2;
  514. sub_query2.AddQuery(&query3);
  515. sub_query2.AddQuery(&query4);
  516. wwsearch::OrQuery query;
  517. query.AddQuery(&sub_query1);
  518. query.AddQuery(&sub_query2);
  519. auto status = searcher.DoQuery(table, query, 0, 2, nullptr, nullptr,
  520. match_documentsid, &score_strategy_list, 2);
  521. EXPECT_EQ(0, status.GetCode());
  522. EXPECT_EQ(2, match_documentsid.size());
  523. SearchLogDebug(
  524. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  525. table.business_type, table.partition_set,
  526. JoinContainerToString(match_documentsid, ", ").c_str());
  527. }
  528. {
  529. match_documentsid.clear();
  530. std::vector<std::shared_ptr<wwsearch::ScoreStrategy>> score_strategy_list{
  531. wwsearch::ScoreStrategyFactory::NewScoreStrategy(keyword, 2, 10)};
  532. wwsearch::PrefixQuery query1(10, "hel");
  533. wwsearch::PrefixQuery query2(10, "wor");
  534. wwsearch::AndQuery sub_query1;
  535. sub_query1.AddQuery(&query1);
  536. sub_query1.AddQuery(&query2);
  537. wwsearch::PrefixQuery query3(6, "fuck");
  538. wwsearch::PrefixQuery query4(6, "wo");
  539. wwsearch::AndQuery sub_query2;
  540. sub_query2.AddQuery(&query3);
  541. sub_query2.AddQuery(&query4);
  542. wwsearch::OrQuery query;
  543. query.AddQuery(&sub_query1);
  544. query.AddQuery(&sub_query2);
  545. auto status = searcher.DoQuery(table, query, 0, 1, nullptr, nullptr,
  546. match_documentsid, &score_strategy_list, 2);
  547. EXPECT_EQ(0, status.GetCode());
  548. EXPECT_EQ(1, match_documentsid.size());
  549. SearchLogDebug(
  550. "table business_id{%u}, partition_set{%llu}, match_documentsid : {%s}",
  551. table.business_type, table.partition_set,
  552. JoinContainerToString(match_documentsid, ", ").c_str());
  553. }
  554. }
  555. } // namespace wwsearch