linux版本中间件

EsSearch.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #include "EsSearch.h"
  2. #include <json/json.h>
  3. #include "Log.h"
  4. #include "Util.h"
  5. #define ES_HOST "http://192.168.1.8:9200/"
  6. #define ES_INDEX_NAME "qs_index" // 索引
  7. EsSearch::EsSearch():m_pHttpClient(nullptr)
  8. {
  9. m_pHttpClient = std::make_unique<HttpClient>();
  10. }
  11. std::pair<std::string, bool> EsSearch::CreatIndex()
  12. {
  13. std::stringstream ss;
  14. ss << ES_HOST << ES_INDEX_NAME<<"?pretty=true";
  15. Json::Value root, mappings, properties, question, answer;
  16. question["type"] = "text";
  17. question["analyzer"] = "jieba_index"; // 指定分词器
  18. question["search_analyzer"] = "jieba_search";
  19. answer["type"] = "text";
  20. properties["question"] = question;
  21. properties["answer"] = answer;
  22. mappings["properties"] = properties;
  23. root["mappings"] = mappings;
  24. auto jsonData = root.toStyledString();
  25. std::cout << "请求参数:\n" << jsonData << std::endl;
  26. std::string rep;
  27. m_pHttpClient->PutJson(ss.str(), jsonData, rep);
  28. std::cout << "创建索引响应:\n" << rep << std::endl << std::endl;
  29. return std::pair<std::string, bool>(rep,true);
  30. }
  31. std::pair<std::string, bool> EsSearch::AddFiled()
  32. {
  33. std::stringstream ss;
  34. ss << ES_HOST << ES_INDEX_NAME << "/_mapping?pretty=true";
  35. Json::Value root, properties, answer;
  36. answer["type"] = "text";
  37. properties["answer_file"] = answer;
  38. root["properties"] = properties;
  39. auto jsonData = root.toStyledString();
  40. std::cout << "请求参数:\n" << jsonData << std::endl;
  41. std::string rep;
  42. m_pHttpClient->PostJson(ss.str(), jsonData, rep);
  43. std::cout << "添加字段响应:\n" << rep << std::endl << std::endl;
  44. return std::pair<std::string, bool>(rep, true);
  45. }
  46. std::pair<std::string, bool> EsSearch::AddData()
  47. {
  48. std::stringstream ss;
  49. ss << ES_HOST << ES_INDEX_NAME << "/_bulk?pretty=true";
  50. std::string jsonData = "";
  51. Json::FastWriter writer;
  52. std::string jsonString = "";// writer.write(jsonObject);
  53. Json::Value index1, doc1, index2, doc2;
  54. index1["index"]["_id"] = 1;
  55. jsonString = writer.write(index1);
  56. jsonData.append(jsonString);
  57. doc1["question"] = "批量1吗";
  58. doc1["answer"] = "是的,批量1";
  59. jsonString = writer.write(doc1);
  60. jsonData.append(jsonString);
  61. index2["index"]["_id"] = 2;
  62. jsonString = writer.write(index2);
  63. jsonData.append(jsonString);
  64. doc2["question"] = "批量2吗";
  65. doc2["answer"] = "是的,批量2";
  66. jsonString = writer.write(doc2);
  67. jsonData.append(jsonString).append("");
  68. std::cout << "请求参数:\n" << jsonData << std::endl;
  69. std::string rep;
  70. m_pHttpClient->PutJson(ss.str(), jsonData, rep);
  71. std::cout << "批量添加数据响应:\n" << rep << std::endl << std::endl;
  72. return std::pair<std::string, bool>();
  73. }
  74. std::pair<std::string, bool> EsSearch::EditData()
  75. {
  76. std::stringstream ss;
  77. ss << ES_HOST << ES_INDEX_NAME << "/_update/2?pretty=true";
  78. Json::Value root, doc, answer;
  79. doc["answer"] = "是的,批量2,不然呢";
  80. root["doc"] = doc;
  81. auto jsonData = root.toStyledString();
  82. std::cout << "请求参数:\n" << jsonData << std::endl;
  83. std::string rep;
  84. m_pHttpClient->PostJson(ss.str(), jsonData, rep);
  85. std::cout << "修改该字段值响应:\n" << rep << std::endl << std::endl;
  86. return std::pair<std::string, bool>(rep, true);
  87. }
  88. std::string EsSearch::Search(const std::string & strKeyWord)
  89. {
  90. auto strRes = filter(strKeyWord);
  91. std::stringstream ss;
  92. ss << ES_HOST << ES_INDEX_NAME << "/_search?pretty=true";
  93. Json::Value root, match;
  94. match["question"] = strRes;
  95. root["query"]["match"] = match;
  96. LOG_DEBUG("请求参数:\n%s", root.toStyledString().c_str());
  97. std::string rep;
  98. m_pHttpClient->GetJson(ss.str(), root.toStyledString(), rep);
  99. // LOG_INFO("获取结果:\n%s", rep.c_str());
  100. return parseSearchResult(rep);
  101. return std::string();
  102. }
  103. std::string EsSearch::parseSearchResult(const std::string & strRet)
  104. {
  105. Json::Value root;
  106. std::string strError;
  107. Json::CharReaderBuilder builder;
  108. std::unique_ptr<Json::CharReader> pReader(builder.newCharReader());
  109. if (!pReader->parse(strRet.c_str(), strRet.c_str() + strRet.length(), &root, &strError) || strError != "") {
  110. LOG_ERROR("json解析失败:\n%s", strRet.c_str());
  111. return "";
  112. }
  113. auto nTotal = root["hits"]["total"]["value"].asInt();
  114. auto dMaxScore = root["hits"]["max_score"].asDouble();
  115. auto arrHits = root["hits"]["hits"];
  116. for (int i = 0; i < nTotal; i++) {
  117. auto dScore = arrHits[i]["_score"].asDouble();
  118. if (dScore == dMaxScore) {
  119. std::cout << "答案是:" << arrHits[i]["_source"]["answer"].asString() << std::endl;
  120. return arrHits[i]["_source"]["answer_file"].asString();
  121. break;
  122. }
  123. }
  124. return std::string();
  125. }