| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #include "EsSearch.h"
- #include <json/json.h>
- #include "Log.h"
- #include "Util.h"
- #define ES_HOST "http://192.168.1.8:9200/"
- #define ES_INDEX_NAME "qs_index" // 索引
- EsSearch::EsSearch():m_pHttpClient(nullptr)
- {
- m_pHttpClient = std::make_unique<HttpClient>();
- }
- std::pair<std::string, bool> EsSearch::CreatIndex()
- {
- std::stringstream ss;
- ss << ES_HOST << ES_INDEX_NAME<<"?pretty=true";
- Json::Value root, mappings, properties, question, answer;
- question["type"] = "text";
- question["analyzer"] = "jieba_index"; // 指定分词器
- question["search_analyzer"] = "jieba_search";
- answer["type"] = "text";
- properties["question"] = question;
- properties["answer"] = answer;
- mappings["properties"] = properties;
- root["mappings"] = mappings;
- auto jsonData = root.toStyledString();
- std::cout << "请求参数:\n" << jsonData << std::endl;
- std::string rep;
-
- m_pHttpClient->PutJson(ss.str(), jsonData, rep);
- std::cout << "创建索引响应:\n" << rep << std::endl << std::endl;
- return std::pair<std::string, bool>(rep,true);
- }
- std::pair<std::string, bool> EsSearch::AddFiled()
- {
- std::stringstream ss;
- ss << ES_HOST << ES_INDEX_NAME << "/_mapping?pretty=true";
- Json::Value root, properties, answer;
-
- answer["type"] = "text";
- properties["answer_file"] = answer;
- root["properties"] = properties;
- auto jsonData = root.toStyledString();
- std::cout << "请求参数:\n" << jsonData << std::endl;
- std::string rep;
- m_pHttpClient->PostJson(ss.str(), jsonData, rep);
- std::cout << "添加字段响应:\n" << rep << std::endl << std::endl;
- return std::pair<std::string, bool>(rep, true);
- }
- std::pair<std::string, bool> EsSearch::AddData()
- {
- std::stringstream ss;
- ss << ES_HOST << ES_INDEX_NAME << "/_bulk?pretty=true";
- std::string jsonData = "";
- Json::FastWriter writer;
- std::string jsonString = "";// writer.write(jsonObject);
- Json::Value index1, doc1, index2, doc2;
- index1["index"]["_id"] = 1;
- jsonString = writer.write(index1);
- jsonData.append(jsonString);
- doc1["question"] = "批量1吗";
- doc1["answer"] = "是的,批量1";
- jsonString = writer.write(doc1);
- jsonData.append(jsonString);
- index2["index"]["_id"] = 2;
- jsonString = writer.write(index2);
- jsonData.append(jsonString);
- doc2["question"] = "批量2吗";
- doc2["answer"] = "是的,批量2";
- jsonString = writer.write(doc2);
- jsonData.append(jsonString).append("");
- std::cout << "请求参数:\n" << jsonData << std::endl;
- std::string rep;
-
- m_pHttpClient->PutJson(ss.str(), jsonData, rep);
- std::cout << "批量添加数据响应:\n" << rep << std::endl << std::endl;
- return std::pair<std::string, bool>();
- }
- std::pair<std::string, bool> EsSearch::EditData()
- {
- std::stringstream ss;
- ss << ES_HOST << ES_INDEX_NAME << "/_update/2?pretty=true";
- Json::Value root, doc, answer;
- doc["answer"] = "是的,批量2,不然呢";
- root["doc"] = doc;
- auto jsonData = root.toStyledString();
- std::cout << "请求参数:\n" << jsonData << std::endl;
- std::string rep;
- m_pHttpClient->PostJson(ss.str(), jsonData, rep);
- std::cout << "修改该字段值响应:\n" << rep << std::endl << std::endl;
- return std::pair<std::string, bool>(rep, true);
- }
- std::string EsSearch::Search(const std::string & strKeyWord)
- {
- auto strRes = filter(strKeyWord);
- std::stringstream ss;
- ss << ES_HOST << ES_INDEX_NAME << "/_search?pretty=true";
- Json::Value root, match;
- match["question"] = strRes;
- root["query"]["match"] = match;
- LOG_DEBUG("请求参数:\n%s", root.toStyledString().c_str());
-
- std::string rep;
- m_pHttpClient->GetJson(ss.str(), root.toStyledString(), rep);
- // LOG_INFO("获取结果:\n%s", rep.c_str());
- return parseSearchResult(rep);
- return std::string();
- }
- std::string EsSearch::parseSearchResult(const std::string & strRet)
- {
- Json::Value root;
- std::string strError;
- Json::CharReaderBuilder builder;
- std::unique_ptr<Json::CharReader> pReader(builder.newCharReader());
- if (!pReader->parse(strRet.c_str(), strRet.c_str() + strRet.length(), &root, &strError) || strError != "") {
- LOG_ERROR("json解析失败:\n%s", strRet.c_str());
- return "";
- }
- auto nTotal = root["hits"]["total"]["value"].asInt();
- auto dMaxScore = root["hits"]["max_score"].asDouble();
- auto arrHits = root["hits"]["hits"];
- for (int i = 0; i < nTotal; i++) {
- auto dScore = arrHits[i]["_score"].asDouble();
- if (dScore == dMaxScore) {
- std::cout << "答案是:" << arrHits[i]["_source"]["answer"].asString() << std::endl;
- return arrHits[i]["_source"]["answer_file"].asString();
- break;
- }
- }
- return std::string();
- }
|