HCE project C++ developers source code library  1.1.1
HCE project developer library
 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 isConnected(void) const {return _isConnected;}
54 
55  std::string Process(const std::string& json); // make search (black box)
56  SphinxOutputJsonMessage Process(SphinxInputJsonMessage& inputJsonMessage); // make search (black box)
57 
58  bool open(void); // persistence connection
59  bool close(void); // close connection
60 
61  void setServerHost(const std::string& serverHost_) {serverHost=serverHost_;}
62  std::string getServerHost(void) const {return serverHost;}
63 
64  void setServerPort(int serverPort_) {serverPort=serverPort_;}
65  int getServerPort(void) const {return serverPort;}
66 
67 
68  bool setFieldWeights(std::vector <std::pair<std::string, int> >& Vec);
69  bool setIndexWeights(std::vector <std::pair<std::string, int> >& Vec);
70 
71  void getFieldWeights(std::vector <std::pair<std::string, int> >& Vec);
72  void getIndexWeights(std::vector <std::pair<std::string, int> >& Vec);
73 
74  bool addField(const std::string& name, int weight=0);
75  bool setField(const std::string& name, int weight);
76  bool delField(const std::string& name);
77 
78  bool addIndexFile(const std::string& name, int weight=0);
79  bool setIndexFile(const std::string& name, int weight);
80  bool delIndexFile(const std::string& name);
81 
82  bool isExistField(const std::string& name);
83  bool isExistIndexFile(const std::string& name);
84 
85  int getFieldWeight(const std::string& name);
86  int getIndexWeight(const std::string& name);
87 
88  bool setMatchMode(int mode); /* Match mode can be:
89  SPH_MATCH_ALL = 0,
90  SPH_MATCH_ANY = 1,
91  SPH_MATCH_PHRASE = 2,
92  SPH_MATCH_BOOLEAN = 3,
93  SPH_MATCH_EXTENDED = 4,
94  SPH_MATCH_FULLSCAN = 5,
95  SPH_MATCH_EXTENDED2 = 6 */
96 
97  bool setSortMode(int mode, const std::string& sortby=""); /* Sort mode can be:
98  SPH_SORT_RELEVANCE = 0,
99  SPH_SORT_ATTR_DESC = 1,
100  SPH_SORT_ATTR_ASC = 2,
101  SPH_SORT_TIME_SEGMENTS = 3,
102  SPH_SORT_EXTENDED = 4,
103  SPH_SORT_EXPR = 5 */
104 
105 
106  bool setRankingMode(int ranker, const std::string& rankexpr=""); /* Ranking mode can be:
107  SPH_RANK_PROXIMITY_BM25 = 0,
108  SPH_RANK_BM25 = 1,
109  SPH_RANK_NONE = 2,
110  SPH_RANK_WORDCOUNT = 3,
111  SPH_RANK_PROXIMITY = 4,
112  SPH_RANK_MATCHANY = 5,
113  SPH_RANK_FIELDMASK = 6,
114  SPH_RANK_SPH04 = 7,
115  SPH_RANK_EXPR = 8,
116  SPH_RANK_TOTAL = 9,
117  SPH_RANK_DEFAULT = SPH_RANK_PROXIMITY_BM25 */
118 
119  size_t getFieldsCount(void) const {return fieldsCount;}
120  size_t getIndexesCount(void) const {return indexesCount;}
121 
122  bool setIndexFileName(const std::string& indexFileName_);
123  std::string getIndexFileName(void) const {return indexFileName;}
124 
125  unsigned long long getTimeoutedRequests(void) const {return timeoutedRequests;}
127 
128  void setDefaultRequestTimeout(unsigned long defaultRequestTimeout_) {defaultRequestTimeout=defaultRequestTimeout_;}
129  unsigned long getDefaultRequestTimeout(void) const {return defaultRequestTimeout;}
130 
131  void resizeResultData(size_t matchesCount, size_t attributesCount);
132 protected:
133  bool addFilter(const std::string& attrName, long long value, bool exclude=false);
134  bool addFilter(const std::string& attrName, std::vector<long long>& values, bool exclude=false);
135  bool addFilterRange(const std::string& attrName, long long umin, long long umax, bool exclude=false);
136  bool addFilterFloatRange(const std::string& attrName, float fmin, float fmax, bool exclude=false);
137  void resetFilters(void);
138 
139  void setMaxResultsNumber(unsigned int maxResultsNumber) throw (Poco::Exception); // set limits max result number (with offset==0 and limit==maxResultNumber)
140  void setLimits(unsigned int offset, unsigned int limit, unsigned int maxMatches, unsigned int cutoff=0) throw (Poco::Exception); // detail set limits before search
141  void applyFilter(Poco::SharedPtr<SphinxFilter> pFilter) throw (Poco::Exception); // add attributes filter before search
142  void setMaxQueryTime(unsigned long maxQueryTime) throw (Poco::Exception); // set allowed query time before search
143 public:
145 
146 protected:
147  std::string makeSearchCommand(const std::string& json) throw (Poco::Exception, std::exception);
148 
151 
152  std::string serverHost;
154 
155  std::string indexFileName;
156 
159  unsigned long long timeoutedRequests;
160  unsigned long defaultRequestTimeout;
161 
162  void cleanup(char**& names, int*& weights, size_t& count);
163  bool setWeights(char**& names, int*& weights, size_t& count, std::vector <std::pair<std::string, int> >& Vec);
164  void getWeights(char**& names, int*& weights, size_t& count, std::vector <std::pair<std::string, int> >& Vec);
165  std::vector <std::pair<std::string, int> >::iterator find(std::vector <std::pair<std::string, int> >& Vec, const std::string& name);
166 
167  void setAllowedFields(std::vector<std::string>& queryExtendedFields);
168  std::set<std::string>& getAlloedFields(void) {return allowedFields;}
169  bool isAllowedFields(const std::string& extendedFieldName);
170 
171  void setFullSchemaNames(std::vector<std::string>& schemaNames);
172  std::set<std::string>& getFullSchemaNames(void) {return fullSchemaNames;}
173  bool isExistSchemaNames(const std::string& schemaName);
174 
175  std::set<std::string> allowedFields;
176  std::set<std::string> fullSchemaNames;
177 
182 
184 };
185 //-------------------------------------------------------------------
186 //-------------------------------------------------------------------
187 } // end namespace sphinx
188 } // end namespace HCE
189 
190 #endif // SPHINX_SEARCHER_HPP
191