Skip to content

Commit c7483b3

Browse files
authored
DOC-4196: add TCEs to the full-text query page (#3377)
1 parent 64d4bb8 commit c7483b3

File tree

1 file changed

+84
-0
lines changed

1 file changed

+84
-0
lines changed

doctests/query_ft.py

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# EXAMPLE: query_ft
2+
# HIDE_START
3+
import json
4+
import sys
5+
import redis
6+
from redis.commands.json.path import Path
7+
from redis.commands.search.field import TextField, NumericField, TagField
8+
from redis.commands.search.indexDefinition import IndexDefinition, IndexType
9+
from redis.commands.search.query import NumericFilter, Query
10+
11+
r = redis.Redis(decode_responses=True)
12+
13+
# create index
14+
schema = (
15+
TextField("$.brand", as_name="brand"),
16+
TextField("$.model", as_name="model"),
17+
TextField("$.description", as_name="description"),
18+
)
19+
20+
index = r.ft("idx:bicycle")
21+
index.create_index(
22+
schema,
23+
definition=IndexDefinition(prefix=["bicycle:"], index_type=IndexType.JSON),
24+
)
25+
26+
# load data
27+
with open("data/query_em.json") as f:
28+
bicycles = json.load(f)
29+
30+
pipeline = r.pipeline(transaction=False)
31+
for bid, bicycle in enumerate(bicycles):
32+
pipeline.json().set(f'bicycle:{bid}', Path.root_path(), bicycle)
33+
pipeline.execute()
34+
# HIDE_END
35+
36+
# STEP_START ft1
37+
res = index.search(Query("@description: kids"))
38+
print(res.total)
39+
# >>> 2
40+
# REMOVE_START
41+
assert res.total == 2
42+
# REMOVE_END
43+
# STEP_END
44+
45+
# STEP_START ft2
46+
res = index.search(Query("@model: ka*"))
47+
print(res.total)
48+
# >>> 1
49+
# REMOVE_START
50+
assert res.total == 1
51+
# REMOVE_END
52+
# STEP_END
53+
54+
# STEP_START ft3
55+
res = index.search(Query("@brand: *bikes"))
56+
print(res.total)
57+
# >>> 2
58+
# REMOVE_START
59+
assert res.total == 2
60+
# REMOVE_END
61+
# STEP_END
62+
63+
# STEP_START ft4
64+
res = index.search(Query("%optamized%"))
65+
print(res)
66+
# >>> Result{1 total, docs: [Document {'id': 'bicycle:3', 'payload': None, 'json': '{"pickup_zone":"POLYGON((-80.2433 25.8067, -80.1333 25.8067, -80.1333 25.6967, -80.2433 25.6967, -80.2433 25.8067))","store_location":"-80.1918,25.7617","brand":"Eva","model":"Eva 291","price":3400,"description":"The sister company to Nord, Eva launched in 2005 as the first and only women-dedicated bicycle brand. Designed by women for women, allEva bikes are optimized for the feminine physique using analytics from a body metrics database. If you like 29ers, try the Eva 291. It’s a brand new bike for 2022.. This full-suspension, cross-country ride has been designed for velocity. The 291 has 100mm of front and rear travel, a superlight aluminum frame and fast-rolling 29-inch wheels. Yippee!","condition":"used"}'}]}
67+
# REMOVE_START
68+
assert res.total == 1
69+
# REMOVE_END
70+
# STEP_END
71+
72+
# STEP_START ft5
73+
res = index.search(Query("%%optamised%%"))
74+
print(res)
75+
# >>> Result{1 total, docs: [Document {'id': 'bicycle:3', 'payload': None, 'json': '{"pickup_zone":"POLYGON((-80.2433 25.8067, -80.1333 25.8067, -80.1333 25.6967, -80.2433 25.6967, -80.2433 25.8067))","store_location":"-80.1918,25.7617","brand":"Eva","model":"Eva 291","price":3400,"description":"The sister company to Nord, Eva launched in 2005 as the first and only women-dedicated bicycle brand. Designed by women for women, allEva bikes are optimized for the feminine physique using analytics from a body metrics database. If you like 29ers, try the Eva 291. It’s a brand new bike for 2022.. This full-suspension, cross-country ride has been designed for velocity. The 291 has 100mm of front and rear travel, a superlight aluminum frame and fast-rolling 29-inch wheels. Yippee!","condition":"used"}'}]}
76+
# REMOVE_START
77+
assert res.total == 1
78+
# REMOVE_END
79+
# STEP_END
80+
81+
# REMOVE_START
82+
# destroy index and data
83+
r.ft("idx:bicycle").dropindex(delete_documents=True)
84+
# REMOVE_END

0 commit comments

Comments
 (0)