From d0f422521a5ee5492555d6df8f831e91952397b6 Mon Sep 17 00:00:00 2001 From: shzhulin3 Date: Mon, 26 Apr 2021 17:43:11 +0800 Subject: [PATCH] complete bool query --- src/search_local/index_read/component.cc | 13 ++++++++++++ .../index_read/query/bool_query_parser.cc | 20 +++++++++++++------ .../index_read/query/bool_query_parser.h | 8 ++------ .../index_read/query/geo_distance_parser.cc | 2 +- .../index_read/query/query_parser.h | 2 +- .../index_read/query/range_query_parser.cc | 7 ++++--- 6 files changed, 35 insertions(+), 17 deletions(-) diff --git a/src/search_local/index_read/component.cc b/src/search_local/index_read/component.cc index 721524d..0f5d357 100644 --- a/src/search_local/index_read/component.cc +++ b/src/search_local/index_read/component.cc @@ -21,6 +21,10 @@ #include "db_manager.h" #include "utf8_str.h" #include "query/bool_query_parser.h" +#include "query/geo_distance_parser.h" +#include "query/range_query_parser.h" +#include "query/match_query_parser.h" +#include "query/term_query_parser.h" #include using namespace std; @@ -218,6 +222,14 @@ void Component::GetQueryWord(uint32_t &m_has_gis){ if(m_query.isObject()){ if(m_query.isMember("bool")){ query_parser = new BoolQueryParser(m_appid, m_query["bool"]); + } else if(m_query.isMember("geo_distance")){ + query_parser = new GeoDistanceParser(m_appid, m_query["geo_distance"]); + } else if(m_query.isMember("range")){ + query_parser = new RangeQueryParser(m_appid, m_query["range"]); + } else if(m_query.isMember("match")){ + query_parser = new MatchQueryParser(m_appid, m_query["match"]); + } else if(m_query.isMember("term")){ + query_parser = new TermQueryParser(m_appid, m_query["term"]); } query_parser_res = new QueryParserRes(); query_parser->ParseContent(query_parser_res); @@ -234,6 +246,7 @@ void Component::GetQueryWord(uint32_t &m_has_gis){ latitude = query_parser_res->Latitude(); longitude = query_parser_res->Longitude(); distance = query_parser_res->Distance(); + log_debug("lat: %s, lon: %s, distance: %f", latitude.c_str(), longitude.c_str(), distance); } extra_filter_keys.assign(query_parser_res->ExtraFilterKeys().begin(), query_parser_res->ExtraFilterKeys().end()); extra_filter_and_keys.assign(query_parser_res->ExtraFilterAndKeys().begin(), query_parser_res->ExtraFilterAndKeys().end()); diff --git a/src/search_local/index_read/query/bool_query_parser.cc b/src/search_local/index_read/query/bool_query_parser.cc index 5170d06..ec8b508 100644 --- a/src/search_local/index_read/query/bool_query_parser.cc +++ b/src/search_local/index_read/query/bool_query_parser.cc @@ -4,13 +4,15 @@ #include "range_query_parser.h" #include "term_query_parser.h" #include "match_query_parser.h" +#include "geo_distance_parser.h" -const char* const BoolQueryParser::NAME ="bool"; -const char* const BoolQueryParser::MUST ="must"; -const char* const BoolQueryParser::SHOULD ="should"; -const char* const BoolQueryParser::TERM ="term"; -const char* const BoolQueryParser::MATCH ="match"; -const char* const BoolQueryParser::RANGE ="range"; +const char* const NAME ="bool"; +const char* const MUST ="must"; +const char* const SHOULD ="should"; +const char* const TERM ="term"; +const char* const MATCH ="match"; +const char* const RANGE ="range"; +const char* const GEODISTANCE ="geo_distance"; BoolQueryParser::BoolQueryParser(uint32_t a, Json::Value& v) :appid(a),value(v) @@ -28,6 +30,9 @@ BoolQueryParser::~BoolQueryParser(){ if(NULL != match_query_parser){ delete match_query_parser; } + if(NULL != geo_query_parser){ + delete geo_query_parser; + } } void BoolQueryParser::DoJobByType(Json::Value& value, uint32_t type, QueryParserRes* query_parser_res){ @@ -40,6 +45,9 @@ void BoolQueryParser::DoJobByType(Json::Value& value, uint32_t type, QueryParser } else if(value.isMember(RANGE)){ range_query_parser = new RangeQueryParser(appid, value[RANGE]); range_query_parser->ParseContent(query_parser_res, type); + } else if(value.isMember(GEODISTANCE)){ + geo_query_parser = new GeoDistanceParser(appid, value[GEODISTANCE]); + geo_query_parser->ParseContent(query_parser_res); } } diff --git a/src/search_local/index_read/query/bool_query_parser.h b/src/search_local/index_read/query/bool_query_parser.h index d3c5cfb..7595847 100644 --- a/src/search_local/index_read/query/bool_query_parser.h +++ b/src/search_local/index_read/query/bool_query_parser.h @@ -24,6 +24,7 @@ class RangeQueryParser; class TermQueryParser; class MatchQueryParser; +class GeoDistanceParser; class BoolQueryParser : public QueryParser { public: @@ -38,15 +39,10 @@ private: private: uint32_t appid; Json::Value value; - static const char* const NAME; - static const char* const MUST; - static const char* const SHOULD; - static const char* const TERM; - static const char* const MATCH; - static const char* const RANGE; RangeQueryParser* range_query_parser; TermQueryParser* term_query_parser; MatchQueryParser* match_query_parser; + GeoDistanceParser* geo_query_parser; }; #endif \ No newline at end of file diff --git a/src/search_local/index_read/query/geo_distance_parser.cc b/src/search_local/index_read/query/geo_distance_parser.cc index c6745be..1e218a4 100644 --- a/src/search_local/index_read/query/geo_distance_parser.cc +++ b/src/search_local/index_read/query/geo_distance_parser.cc @@ -102,8 +102,8 @@ void GeoDistanceParser::ParseContent(QueryParserRes* query_parser_res){ stringstream sslat; stringstream sslon; sslat << geo.lat; - sslon << geo.lon; query_parser_res->Latitude() = sslat.str(); + sslon << geo.lon; query_parser_res->Longitude() = sslon.str(); query_parser_res->Distance() = distance; } diff --git a/src/search_local/index_read/query/query_parser.h b/src/search_local/index_read/query/query_parser.h index 197a3e1..0d777d9 100644 --- a/src/search_local/index_read/query/query_parser.h +++ b/src/search_local/index_read/query/query_parser.h @@ -48,7 +48,7 @@ public: return latitude; } string& Longitude(){ - return latitude; + return longitude; } double& Distance(){ return distance; diff --git a/src/search_local/index_read/query/range_query_parser.cc b/src/search_local/index_read/query/range_query_parser.cc index a040417..8d74b26 100644 --- a/src/search_local/index_read/query/range_query_parser.cc +++ b/src/search_local/index_read/query/range_query_parser.cc @@ -53,9 +53,10 @@ void RangeQueryParser::ParseContent(QueryParserRes* query_parser_res, uint32_t t info.range_type = RANGE_GTLT; } } - info.start = start.asInt(); - info.end = end.asInt(); - fieldInfos.push_back(info); + fieldInfo.start = start.asInt(); + fieldInfo.end = end.asInt(); + fieldInfo.range_type = info.range_type; + fieldInfos.push_back(fieldInfo); } if(fieldInfos.size() != 0){ if(type == ORKEY){