-
Notifications
You must be signed in to change notification settings - Fork 2
/
sphinxtest2.cc
218 lines (189 loc) · 7.04 KB
/
sphinxtest2.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
*
* C++ sphinx search client library
* Copyright (C) 2007 Seznam.cz, a.s.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Seznam.cz, a.s.
* Radlicka 2, Praha 5, 15000, Czech Republic
* http://www.seznam.cz, mailto:[email protected]
*
* $Id$
*
* DESCRIPTION
* A sphinxclient sample and testing program.
*
* AUTHORS
* Jan Kirschner <[email protected]>
*
* HISTORY
* 2006-12-14 (kirschner)
* Created.
*
* Quick compile:
* g++ -g sphinxtest2.cc -Iinclude/ -Lsrc/.libs/ -lsphinxclient -o sphinxtest2
*/
#include <sphinxclient/sphinxclient.h>
#include <stdio.h>
#include <unistd.h>
//------------------------------------------------------------------------------
void printResult(const Sphinx::Response_t &result)
{
printf("command version: 0x%X\n", result.commandVersion);
printf("field count: %d\n", result.field.size());
printf("attribute count: %d\n", result.attribute.size());
printf("match count: %d\n", result.entry.size());
printf("word count: %d\n", result.word.size());
printf("\nFields:\n");
for (int i=0;i<result.field.size();i++)
{
printf(" %s\n", result.field[i].c_str());
}//for
printf("\nAttributes:\n");
for (Sphinx::AttributeTypes_t::const_iterator it=result.attribute.begin() ;
it!=result.attribute.end();it++)
{
if (it->second & Sphinx::SPH_ATTR_MULTI) {
printf(" name: %s, multi type: %d\n", it->first.c_str(), (it->second ^ Sphinx::SPH_ATTR_MULTI));
}//if
else
printf(" name: %s, type: %d\n", it->first.c_str(), it->second);
}//for
printf("\nWords:\n");
for (std::map<std::string, Sphinx::WordStatistics_t>::const_iterator it
= result.word.begin() ; it!=result.word.end() ; it++)
{
printf(" Word %s: %d docs / %d hits\n", it->first.c_str(), it->second.docsHit, it->second.totalHits);
}//for
printf("entries: %d\n", result.entriesGot);
printf("documents: %d\n", result.entriesFound);
printf("duration: %d\n", result.timeConsumed);
printf("64bit ID: %d\n", result.use64bitId);
// print results with attributes
printf("\nResponse:\n");
int i = 0;
for (std::vector<Sphinx::ResponseEntry_t>::const_iterator it = result.entry.begin();
it != result.entry.end(); it++) {
i++;
printf("%d) id: %ld", i, it->documentId);
for (std::map<std::string, Sphinx::Value_t>::const_iterator j =
it->attribute.begin();
j != it->attribute.end() ; j++)
{
switch(j->second.getValueType()) {
case Sphinx::VALUETYPE_FLOAT:
printf(" | %s:%.2f", j->first.c_str(), (float)(j->second));
break;
case Sphinx::VALUETYPE_VECTOR: {
char comma[2] = {'\0', '\0'};
std::vector<Sphinx::Value_t> vec = j->second;
printf(" | %s:(", j->first.c_str());
for (std::vector<Sphinx::Value_t>::const_iterator vi=vec.begin() ; vi!=vec.end() ; vi++) {
if(vi->getValueType() == Sphinx::VALUETYPE_FLOAT)
printf("%s%ff", comma, (float)(*vi));
else
printf("%s%dd", comma, (uint32_t)(*vi));
comma[0] = ',';
}//for
printf(")");
break; }
case Sphinx::VALUETYPE_UINT64:
printf("%s:%lu | ", j->first.c_str(), (uint64_t)(j->second));
break;
case Sphinx::VALUETYPE_UINT32:
default:
printf(" | %s:%d", j->first.c_str(), (uint32_t)(j->second));
break;
}//switch
}
printf("\n");
}//for response entries
}//konec fce
//------------------------------------------------------------------------------
int main()
{
Sphinx::ConnectionConfig_t config (
"localhost", // hostname
3312, // port
true, // keepalive
2000, // connect timeout
20000, // read timeout
2000, // write timeout
5, // num retries
600 // delay between retries
);
Sphinx::Client_t connection(config);
Sphinx::Response_t result;
Sphinx::SearchConfig_t settings(Sphinx::VER_COMMAND_SEARCH_0_9_9);
printf("starting.....\n");
// search setup
settings.setPaging(0, 20);
settings.setMatchMode(Sphinx::SPH_MATCH_ALL);
settings.setMaxMatches(1000);
settings.setSearchedIndexes("test1");
settings.setQueryComment("comment");
settings.setMaxQueryTime(10000);
// sort relevance
settings.setSorting(Sphinx::SPH_SORT_RELEVANCE);
//--------------- first search query ----------------------
try{
//single query
connection.query("pes ahoj", settings, result);
usleep(1000);
printf("query success.\n");
}
catch(Sphinx::Error_t e)
{
printf("query error:\n%s\n", e.errMsg.c_str());
return 2;
}
printf("------------------------ result 1 ------------------------\n");
printResult(result);
printf("----------------------------- end ------------------------\n");
//--------------- attribute update ----------------------
Sphinx::AttributeUpdates_t updateData;
updateData.addAttribute("att_uint");
updateData.addAttribute("att_group");
updateData.addDocument(2, Sphinx::VALUETYPE_UINT32, 1, 101);
updateData.addDocument(3, Sphinx::VALUETYPE_UINT32, 2, 102);
updateData.addDocument(5, Sphinx::VALUETYPE_UINT32, 3, 103);
updateData.addDocument(9, Sphinx::VALUETYPE_UINT32, 4, 104);
try {
connection.updateAttributes("test1", updateData);
usleep(1000);
printf("update query success.\n");
}
catch(Sphinx::Error_t e)
{
printf("update query error:\n%s\n", e.errMsg.c_str());
return 2;
}
//--------------- second search query ----------------------
try{
//single query
connection.query("pes ahoj", settings, result);
printf("query success.\n");
}
catch(Sphinx::Error_t e)
{
printf("query error:\n%s\n", e.errMsg.c_str());
return 2;
}
printf("------------------------ result 2 ------------------------\n");
printResult(result);
printf("----------------------------- end ------------------------\n");
return 0;
}//main