example.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 <iostream>
  19. #include "include/codec_impl.h"
  20. #include "include/document.h"
  21. #include "include/index_writer.h"
  22. #include "include/logger.h"
  23. #include "include/virtual_db_rocks.h"
  24. #include "include/and_query.h"
  25. #include "include/bool_query.h"
  26. #include "include/index_wrapper.h"
  27. #include "include/query.h"
  28. #include "include/searcher.h"
  29. #include "include/storage_type.h"
  30. #include "include/tokenizer_impl.h"
  31. #include "include/weight.h"
  32. // A simple example for index documents and search match terms.
  33. int main(int argc, char **argv) {
  34. wwsearch::DefaultIndexWrapper indexer;
  35. bool use_rocksdb = false;
  36. if (argc == 1) {
  37. // use mock(simple memory) db
  38. } else if (argc == 2) {
  39. // use rocksdb
  40. indexer.DBParams().path.assign(argv[1]);
  41. use_rocksdb = true;
  42. } else {
  43. printf("Please check input args\n");
  44. return -1;
  45. }
  46. indexer.Config().SetLogLevel(wwsearch::kSearchLogLevelDebug);
  47. wwsearch::SearchStatus status = indexer.Open(use_rocksdb);
  48. SearchLogDebug("status:%s", status.GetState().c_str());
  49. assert(status.OK());
  50. // construct
  51. wwsearch::IndexFieldFlag flag;
  52. flag.SetStoredField();
  53. flag.SetTokenize();
  54. flag.SetSuffixBuild();
  55. flag.SetDocValue();
  56. flag.SetInvertIndex();
  57. std::vector<wwsearch::DocumentUpdater *> documents;
  58. for (int i = 0; i < 3; i++) {
  59. wwsearch::DocumentUpdater *document_updater =
  60. new wwsearch::DocumentUpdater();
  61. wwsearch::Document &document = document_updater->New();
  62. document.SetID(i + 1); // document id must start from 1.
  63. // 0 -> word
  64. // 1 -> filter_value
  65. // 2 -> sort_value
  66. {
  67. auto field = document.AddField();
  68. field->SetMeta(0, flag);
  69. field->SetString("one two three");
  70. }
  71. {
  72. auto field = document.AddField();
  73. field->SetMeta(1, flag);
  74. field->SetUint32(100 + i);
  75. }
  76. {
  77. auto field = document.AddField();
  78. field->SetMeta(2, flag);
  79. field->SetUint32(1000 + i);
  80. }
  81. documents.push_back(document_updater);
  82. std::string debug_str;
  83. document.PrintToReadStr(debug_str);
  84. std::cout << "Insert Documents" << std::endl << debug_str << std::endl;
  85. }
  86. // add
  87. wwsearch::TableID table;
  88. table.business_type = 1;
  89. table.partition_set = 10000;
  90. bool success = indexer.index_writer_->AddDocuments(table, documents);
  91. if (!success) {
  92. std::cout << "Add Document return error" << std::endl;
  93. }
  94. // check
  95. std::cout << "After Add Status:" << std::endl;
  96. for (auto du : documents) {
  97. std::cout << " document_id:" << du->New().ID();
  98. std::cout << " Code:" << (du->Status().OK() ? "Success" : "Failure")
  99. << " State:" << du->Status().GetState() << std::endl;
  100. delete du;
  101. }
  102. // do search
  103. const char *terms1[] = {"one", "two", "tw", "ne"};
  104. const char *terms2[] = {"two", "three", "tw", "ne"};
  105. for (size_t i = 0; i < sizeof(terms1) / sizeof(const char *); i++) {
  106. wwsearch::Searcher searcher(&indexer.Config());
  107. std::string term1(terms1[i]);
  108. std::string term2(terms2[i]);
  109. wwsearch::AndQuery query;
  110. std::list<wwsearch::DocumentID> match_docids;
  111. std::vector<wwsearch::Document *> match_documents;
  112. std::vector<wwsearch::SearchStatus> ss;
  113. wwsearch::BooleanQuery query1(0, term1);
  114. wwsearch::BooleanQuery query2(0, term2);
  115. query.AddQuery(&query1);
  116. query.AddQuery(&query2);
  117. std::vector<wwsearch::Filter *> filter;
  118. {
  119. wwsearch::RangeFilter *rule = new wwsearch::RangeFilter(102, 103);
  120. rule->GetField()->SetMeta(1, flag);
  121. rule->GetField()->SetUint32(0);
  122. filter.push_back(rule);
  123. }
  124. std::vector<wwsearch::SortCondition *> sorter;
  125. {
  126. wwsearch::NumericSortCondition *sort =
  127. new wwsearch::NumericSortCondition(2, wwsearch::kSortConditionDesc);
  128. sorter.push_back(sort);
  129. }
  130. auto status =
  131. searcher.DoQuery(table, query, 0, 10, &filter, &sorter, match_docids);
  132. std::cout << "query: match " << match_docids.size() << std::endl;
  133. std::cout << " term=" << term1 << " and " << term2 << std::endl;
  134. for (auto rule : filter)
  135. std::cout << " filter=" << rule->PrintReadableStr() << std::endl;
  136. std::cout << std::endl;
  137. if (status.OK()) {
  138. for (auto doc : match_docids) {
  139. wwsearch::Document *document = new wwsearch::Document;
  140. document->SetID(doc);
  141. match_documents.push_back(document);
  142. }
  143. // now we fetch document again.
  144. auto ret = searcher.GetStoredFields(table, match_documents, ss, nullptr);
  145. if (ret.OK()) {
  146. for (size_t i = 0; i < ss.size(); i++) {
  147. if (ss[i].OK()) {
  148. std::string debug_str;
  149. match_documents[i]->PrintToReadStr(debug_str);
  150. std::cout << debug_str << std::endl;
  151. } else {
  152. std::cout << "Error:Get Document " << match_documents[i]->ID()
  153. << "," << ss[i].GetCode() << "," << ss[i].GetState()
  154. << std::endl;
  155. }
  156. }
  157. } else {
  158. std::cout << "Error:GetStoredFields fail," << ret.GetCode() << ","
  159. << ret.GetState() << std::endl;
  160. }
  161. } else {
  162. std::cout << "Error:fail,code:" << status.GetCode()
  163. << ",msg:" << status.GetState() << std::endl;
  164. }
  165. for (auto rule : filter) delete rule;
  166. for (auto sort : sorter) delete sort;
  167. for (auto d : match_documents) delete d;
  168. }
  169. return 0;
  170. }