Skip to content

Commit a03845a

Browse files
committed
Fulltext for C and Python
1 parent 61e52cd commit a03845a

File tree

5 files changed

+77
-1
lines changed

5 files changed

+77
-1
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,3 +268,17 @@ C |
268268
Go |
269269
node.js
270270

271+
## Full-Text (CBFT) Queries
272+
273+
### Basic full-text example
274+
275+
This should show basic functionality of the FTS feature. Search for 'hoppy'
276+
in `beer-sample`
277+
278+
[C](c/fts-basic.cc) |
279+
[Python](python/fts-basic.py) |
280+
.NET |
281+
Go |
282+
node.js |
283+
Java |
284+
PHP

c/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ connecting-ssl
1515
*.dSYM
1616
subdoc-updating
1717
subdoc-retrieving
18+
fts-basic

c/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PROGS=connecting updating retrieving query-create-index query-criteria \
22
query-placeholders counter expiration \
33
query-consistency cas durability bulk-get bulk-store \
4-
connecting-ssl subdoc-retrieving subdoc-updating
4+
connecting-ssl subdoc-retrieving subdoc-updating fts-basic
55

66
all: $(PROGS)
77

c/fts-basic.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <libcouchbase/couchbase.h>
2+
#include <libcouchbase/cbft.h>
3+
#include <cassert>
4+
#include <cstdio>
5+
#include <cstdlib>
6+
#include <string>
7+
8+
static void rowCallback(lcb_t, int, const lcb_RESPFTS *resp)
9+
{
10+
if (resp->rflags & LCB_RESP_F_FINAL) {
11+
printf("Status: %d\n", resp->rc);
12+
printf("Meta: %.*s\n", (int)resp->nrow, resp->row);
13+
if (resp->htresp) {
14+
printf("HTTP Response: %.*s\n", (int)resp->htresp->nbody, resp->htresp->body);
15+
}
16+
} else {
17+
printf("Row: %.*s\n", (int)resp->nrow, resp->row);
18+
}
19+
}
20+
21+
int main(int, char **)
22+
{
23+
lcb_t instance;
24+
lcb_error_t rc = lcb_create(&instance, NULL);
25+
assert(rc == LCB_SUCCESS);
26+
lcb_cntl_string(instance, "detailed_errcodes", "true");
27+
lcb_connect(instance);
28+
lcb_wait(instance);
29+
assert(lcb_get_bootstrap_status(instance) == LCB_SUCCESS);
30+
31+
// Be sure to include the indexName within the request payload
32+
std::string encodedQuery(
33+
"{\"query\":{\"match\":\"hoppy\"},\"indexName\":\"beer-search\",\"size\":10}");
34+
lcb_CMDFTS cmd = { 0 };
35+
cmd.callback = rowCallback;
36+
cmd.query = encodedQuery.c_str();
37+
cmd.nquery = encodedQuery.size();
38+
rc = lcb_fts_query(instance, NULL, &cmd);
39+
assert(rc == LCB_SUCCESS);
40+
41+
lcb_wait(instance);
42+
lcb_destroy(instance);
43+
}

python/fts-basic.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
from __future__ import print_function
3+
from pprint import pprint
4+
5+
from couchbase.bucket import Bucket
6+
import couchbase.fulltext as FT
7+
8+
cb = Bucket()
9+
results = cb.search(
10+
'travel-search',
11+
FT.MatchQuery('part', fuzziness=0, field='content'),
12+
limit=3,
13+
facets={'countries': FT.TermFacet('country', limit=3)})
14+
15+
for row in results:
16+
pprint(row)
17+
18+
print('Facet results:')

0 commit comments

Comments
 (0)