Skip to content

Commit d9c640a

Browse files
ohwgilesvvaintroub
authored andcommitted
SphinxSE: add support for json filtering
The filter command did not accept characters . [ ] which are valid in an attribute name for a sphinx query with json filtering. + test case added
1 parent 3a24f1c commit d9c640a

File tree

4 files changed

+54
-7
lines changed

4 files changed

+54
-7
lines changed

storage/sphinx/ha_sphinx.cc

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1411,6 +1411,14 @@ static bool myismagic ( char c )
14111411
return c=='@';
14121412
}
14131413

1414+
static bool myisjson ( char c )
1415+
{
1416+
return
1417+
c=='.' ||
1418+
c=='[' ||
1419+
c==']';
1420+
}
1421+
14141422

14151423
bool CSphSEQuery::ParseField ( char * sField )
14161424
{
@@ -1622,7 +1630,7 @@ bool CSphSEQuery::ParseField ( char * sField )
16221630
break;
16231631

16241632
tFilter.m_sAttrName = sValue;
1625-
while ( (*sValue) && ( myisattr(*sValue) || myismagic(*sValue) ) )
1633+
while ( (*sValue) && ( myisattr(*sValue) || myismagic(*sValue) || myisjson(*sValue) ) )
16261634
sValue++;
16271635
if ( !*sValue )
16281636
break;

storage/sphinx/mysql-test/sphinx/sphinx.result

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,20 @@ id w q
44
1 2 test
55
2 2 test
66
4 1 test
7+
5 1 test
78
drop table ts;
89
create table ts ( id bigint unsigned not null, w int not null, q varchar(255) not null, index(q) ) engine=sphinx connection="sphinx://127.0.0.1:SPHINXSEARCH_PORT/*";
910
select * from ts where q='test;filter=gid,1;mode=extended';
1011
id w q
11-
1 2421 test;filter=gid,1;mode=extended
12-
2 2421 test;filter=gid,1;mode=extended
12+
1 2379 test;filter=gid,1;mode=extended
13+
2 2379 test;filter=gid,1;mode=extended
14+
5 1412 test;filter=gid,1;mode=extended
1315
select * from ts where q='test|one;mode=extended';
1416
id w q
15-
1 3595 test|one;mode=extended
16-
2 2460 test|one;mode=extended
17-
4 1471 test|one;mode=extended
17+
1 3579 test|one;mode=extended
18+
2 2439 test|one;mode=extended
19+
4 1456 test|one;mode=extended
20+
5 1456 test|one;mode=extended
1821
select * from ts where q='test;offset=1;limit=1';
1922
id w q
2023
2 2 test;offset=1;limit=1
@@ -32,10 +35,11 @@ id w q gid _sph_count
3235
2 1 1 0
3336
3 1 2 0
3437
4 1 2 0
38+
5 1 1 0
3539
select * from ts where q=';groupby=attr:gid';
3640
id w q gid _sph_count
3741
3 1 ;groupby=attr:gid 2 2
38-
1 1 ;groupby=attr:gid 12
42+
1 1 ;groupby=attr:gid 13
3943
explain select * from ts where q=';groupby=attr:gid';
4044
id select_type table type possible_keys key key_len ref rows Extra
4145
1 SIMPLE ts ref q q 257 const 3 Using where with pushed condition
@@ -54,3 +58,20 @@ show status like "sphinx_word%";
5458
Variable_name Value
5559
Sphinx_word_count 0
5660
Sphinx_words
61+
create table ts ( id bigint unsigned not null, w int not null, q varchar(255) not null, index(q) ) engine=sphinx connection="sphinx://127.0.0.1:SPHINXSEARCH_PORT/*";
62+
select * from ts where q=';filter=meta.foo_count,100';
63+
id w q
64+
1 1 ;filter=meta.foo_count,100
65+
select * from ts where q='test;filter=meta.sub.int,7';
66+
id w q
67+
5 1 test;filter=meta.sub.int,7
68+
select * from ts where q=';filter=meta.sub.list[0],4';
69+
id w q
70+
select * from ts where q=';filter=meta.sub.list[1],4';
71+
id w q
72+
5 1 ;filter=meta.sub.list[1],4
73+
select * from ts where q='test;range=meta.foo_count,100,500';
74+
id w q
75+
1 2 test;range=meta.foo_count,100,500
76+
5 1 test;range=meta.foo_count,100,500
77+
drop table ts;

storage/sphinx/mysql-test/sphinx/sphinx.test

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,12 @@ drop table ts;
3232

3333
show status like "sphinx_total%";
3434
show status like "sphinx_word%";
35+
36+
--replace_result $SPHINXSEARCH_PORT SPHINXSEARCH_PORT
37+
eval create table ts ( id bigint unsigned not null, w int not null, q varchar(255) not null, index(q) ) engine=sphinx connection="sphinx://127.0.0.1:$SPHINXSEARCH_PORT/*";
38+
select * from ts where q=';filter=meta.foo_count,100';
39+
select * from ts where q='test;filter=meta.sub.int,7';
40+
select * from ts where q=';filter=meta.sub.list[0],4';
41+
select * from ts where q=';filter=meta.sub.list[1],4';
42+
select * from ts where q='test;range=meta.foo_count,100,500';
43+
drop table ts;

storage/sphinx/mysql-test/sphinx/testdata.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@
55
<sphinx:field name="title"/>
66
<sphinx:field name="content"/>
77
<sphinx:attr name="gid" type="int"/>
8+
<sphinx:attr name="meta" type="json"/>
89
</sphinx:schema>
910

1011
<sphinx:document id="1">
1112
<title>test one</title>
1213
<content>this is my test document number one. also checking search within phrases.</content>
1314
<gid>1</gid>
15+
<meta>{ "foo_count": 100 }</meta>
1416
</sphinx:document>
1517

1618
<sphinx:document id="2">
@@ -31,5 +33,12 @@
3133
<gid>2</gid>
3234
</sphinx:document>
3335

36+
<sphinx:document id="5">
37+
<title>doc number five</title>
38+
<content>this is to test json filtering</content>
39+
<gid>1</gid>
40+
<meta>{ "foo_count": 200, "sub": { "list": [ 3, 4 ], "int": 7 } }</meta>
41+
</sphinx:document>
42+
3443
</sphinx:docset>
3544

0 commit comments

Comments
 (0)