hce-node application  1.4.3
HCE Hierarchical Cluster Engine node application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
SphinxSearcher.hpp
Go to the documentation of this file.
1 
14 #ifndef SPHINX_SEARCHER_HPP
15 #define SPHINX_SEARCHER_HPP
16 
17 #include <set>
18 
19 #include "sphinxclient.h"
20 
21 #include "SphinxFilters.hpp"
25 #include "CustomMessage.hpp"
26 
27 typedef struct st_sphinx_client sphinx_client;
28 
29 namespace HCE
30 {
31 namespace sphinx
32 {
33 class SphinxFunctionalObject;
34 //-------------------------------------------------------------------
36 {
37  sphinx_client * client;
38  char** field_names;
39  int* field_weights;
40  size_t fieldsCount;
41 
42  char** index_names;
43  int* index_weights;
44  size_t indexesCount;
45 public:
46  explicit SphinxSearcher(SphinxFunctionalObject& fObj, bool startSearchd=false, bool stopSearchd_=false);
47  virtual ~SphinxSearcher(void);
48 
49  bool easyStart(int matchMode=SPH_MATCH_EXTENDED2, int sortMode=SPH_SORT_RELEVANCE, int rankingMode=SPH_RANK_DEFAULT, const std::string& rankexpr="");
50  bool startSearchd(void);
51  bool stopSearchd(void);
52  bool isActiveSearchd(void);
53  bool isNeedStopSearchd(void) const {return _StopSearchd;}
54  bool isConnected(void) const {return _isConnected;}
55  bool isStoppedSearchd(void) const {return _isStoppedSearchd;}
56 
57  std::string Process(const std::string& json); // make search (black box)
58  SphinxOutputJsonMessage Process(SphinxInputJsonMessage& inputJsonMessage); // make search (black box)
59 
60  bool open(void); // persistence connection
61  bool close(void); // close connection
62 
63  void setServerHost(const std::string& serverHost_) {serverHost=serverHost_;}
64  std::string getServerHost(void) const {return serverHost;}
65 
66  void setServerPort(int serverPort_) {serverPort=serverPort_;}
67  int getServerPort(void) const {return serverPort;}
68 
69 
70  bool setFieldWeights(std::vector <std::pair<std::string, int> >& Vec);
71  bool setIndexWeights(std::vector <std::pair<std::string, int> >& Vec);
72 
73  void getFieldWeights(std::vector <std::pair<std::string, int> >& Vec);
74  void getIndexWeights(std::vector <std::pair<std::string, int> >& Vec);
75 
76  bool addField(const std::string& name, int weight=0);
77  bool setField(const std::string& name, int weight);
78  bool delField(const std::string& name);
79 
80  bool addIndexFile(const std::string& name, int weight=0);
81  bool setIndexFile(const std::string& name, int weight);
82  bool delIndexFile(const std::string& name);
83 
84  bool isExistField(const std::string& name);
85  bool isExistIndexFile(const std::string& name);
86 
87  int getFieldWeight(const std::string& name);
88  int getIndexWeight(const std::string& name);
89 
90  bool setMatchMode(int mode); /* Match mode can be:
91  SPH_MATCH_ALL = 0,
92  SPH_MATCH_ANY = 1,
93  SPH_MATCH_PHRASE = 2,
94  SPH_MATCH_BOOLEAN = 3,
95  SPH_MATCH_EXTENDED = 4,
96  SPH_MATCH_FULLSCAN = 5,
97  SPH_MATCH_EXTENDED2 = 6 */
98 
99  bool setSortMode(int mode, const std::string& sortby=""); /* Sort mode can be:
100  SPH_SORT_RELEVANCE = 0,
101  SPH_SORT_ATTR_DESC = 1,
102  SPH_SORT_ATTR_ASC = 2,
103  SPH_SORT_TIME_SEGMENTS = 3,
104  SPH_SORT_EXTENDED = 4,
105  SPH_SORT_EXPR = 5 */
106 
107 
108  bool setRankingMode(int ranker, const std::string& rankexpr=""); /* Ranking mode can be:
109  SPH_RANK_PROXIMITY_BM25 = 0,
110  SPH_RANK_BM25 = 1,
111  SPH_RANK_NONE = 2,
112  SPH_RANK_WORDCOUNT = 3,
113  SPH_RANK_PROXIMITY = 4,
114  SPH_RANK_MATCHANY = 5,
115  SPH_RANK_FIELDMASK = 6,
116  SPH_RANK_SPH04 = 7,
117  SPH_RANK_EXPR = 8,
118  SPH_RANK_TOTAL = 9,
119  SPH_RANK_DEFAULT = SPH_RANK_PROXIMITY_BM25 */
120 
121  size_t getFieldsCount(void) const {return fieldsCount;}
122  size_t getIndexesCount(void) const {return indexesCount;}
123 
124  bool setIndexFileName(const std::string& indexFileName_);
125  std::string getIndexFileName(void) const {return indexFileName;}
126 
127  void setTimeoutedRequests(unsigned long long timeoutedRequests_) {timeoutedRequests=timeoutedRequests_;}
128  unsigned long long getTimeoutedRequests(void) const {return timeoutedRequests;}
130 
131  void setDefaultRequestTimeout(unsigned long defaultRequestTimeout_) {defaultRequestTimeout=defaultRequestTimeout_;}
132  unsigned long getDefaultRequestTimeout(void) const {return defaultRequestTimeout;}
133 
134  void resizeResultData(size_t matchesCount, size_t attributesCount);
135 protected:
136  bool addFilter(const std::string& attrName, long long value, bool exclude=false);
137  bool addFilter(const std::string& attrName, std::vector<long long>& values, bool exclude=false);
138  bool addFilterRange(const std::string& attrName, long long umin, long long umax, bool exclude=false);
139  bool addFilterFloatRange(const std::string& attrName, float fmin, float fmax, bool exclude=false);
140  void resetFilters(void);
141 
142  void setMaxResultsNumber(unsigned int maxResultsNumber) throw (Poco::Exception); // set limits max result number (with offset==0 and limit==maxResultNumber)
143  void setLimits(unsigned int offset, unsigned int limit, unsigned int maxMatches, unsigned int cutoff=0) throw (Poco::Exception); // detail set limits before search
144  void applyFilter(Poco::SharedPtr<SphinxFilter> pFilter) throw (Poco::Exception); // add attributes filter before search
145  void setMaxQueryTime(unsigned long maxQueryTime) throw (Poco::Exception); // set allowed query time before search
146 public:
148 
149 protected:
150  std::string makeSearchCommand(const std::string& json) throw (Poco::Exception, std::exception);
151 
154 
155  std::string serverHost;
157 
158  std::string indexFileName;
159 
163  unsigned long long timeoutedRequests;
164  unsigned long defaultRequestTimeout;
165 
166  void cleanup(char**& names, int*& weights, size_t& count);
167  bool setWeights(char**& names, int*& weights, size_t& count, std::vector <std::pair<std::string, int> >& Vec);
168  void getWeights(char**& names, int*& weights, size_t& count, std::vector <std::pair<std::string, int> >& Vec);
169  static std::vector <std::pair<std::string, int> >::iterator find(std::vector <std::pair<std::string, int> >& Vec, const std::string& name);
170 
171  void setAllowedFields(std::vector<std::string>& queryExtendedFields);
172  std::set<std::string>& getAlloedFields(void) {return allowedFields;}
173  bool isAllowedFields(const std::string& extendedFieldName);
174 
175  void setFullSchemaNames(std::vector<std::string>& schemaNames);
176  std::set<std::string>& getFullSchemaNames(void) {return fullSchemaNames;}
177  bool isExistSchemaNames(const std::string& schemaName);
178 
179  std::set<std::string> allowedFields;
180  std::set<std::string> fullSchemaNames;
181 
186 
188 };
189 //-------------------------------------------------------------------
190 //-------------------------------------------------------------------
191 } // end namespace sphinx
192 } // end namespace HCE
193 
194 #endif // SPHINX_SEARCHER_HPP
195