def aofTestCommon(env, reloadfn): # TODO: Change this attribute in rmtest conn = getConnectionByEnv(env) env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'field1', 'text', 'field2', 'numeric') for x in range(1, 10): conn.execute_command('hset', 'doc{}'.format(x), 'field1', 'myText{}'.format(x), 'field2', 20 * x) reloadfn() waitForIndex(env, 'idx') exp = [ 9L, 'doc1', ['field1', 'myText1', 'field2', '20'], 'doc2', ['field1', 'myText2', 'field2', '40'], 'doc3', ['field1', 'myText3', 'field2', '60'], 'doc4', ['field1', 'myText4', 'field2', '80'], 'doc5', ['field1', 'myText5', 'field2', '100'], 'doc6', ['field1', 'myText6', 'field2', '120'], 'doc7', ['field1', 'myText7', 'field2', '140'], 'doc8', ['field1', 'myText8', 'field2', '160'], 'doc9', ['field1', 'myText9', 'field2', '180'] ] reloadfn() waitForIndex(env, 'idx') ret = env.cmd('ft.search', 'idx', 'myt*') env.assertEqual(toSortedFlatList(ret), toSortedFlatList(exp))
def testIssue364(env): # FT.CREATE testset "SCHEMA" "permit_timestamp" "NUMERIC" "SORTABLE" "job_category" "TEXT" "NOSTEM" "address" "TEXT" "NOSTEM" "neighbourhood" "TAG" "SORTABLE" "description" "TEXT" "building_type" "TEXT" "WEIGHT" "20" "NOSTEM" "SORTABLE" "work_type" "TEXT" "NOSTEM" "SORTABLE" "floor_area" "NUMERIC" "SORTABLE" "construction_value" "NUMERIC" "SORTABLE" "zoning" "TAG" "units_added" "NUMERIC" "SORTABLE" "location" "GEO" # ft.add testset 109056573-002 1 fields building_type "Retail and Shops" description "To change the use from a Restaurant to a Personal Service Shop (Great Clips)" # FT.SEARCH testset retail RETURN 1 description SUMMARIZE LIMIT 0 1 env.cmd('ft.create', 'idx', 'ON', 'HASH', 'SCHEMA', 'building_type', 'TEXT', 'description', 'TEXT') waitForIndex(env, 'idx') env.cmd( 'ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'building_type', 'Retail and Shops', 'description', 'To change the use from a Restaurant to a Personal Service Shop (Great Clips)' ) env.cmd( 'ft.add', 'idx', 'doc2', '1.0', 'FIELDS', 'building_type', 'Retail and Shops', 'description', 'To change the use from a Restaurant to a Personal Service Shop (Great Clips) at the end' ) ret = env.cmd('FT.SEARCH', 'idx', 'retail', 'RETURN', 1, 'description', 'SUMMARIZE') expected = [ 2L, 'doc2', [ 'description', 'To change the use from a Restaurant to a Personal Service Shop (Great Clips) at the' ], 'doc1', [ 'description', 'To change the use from a Restaurant to a Personal Service Shop (Great Clips)' ] ] env.assertEqual(toSortedFlatList(expected), toSortedFlatList(ret))
def testExpiredDuringSearch(env): N = 100 createExpire(env, N) res = env.cmd('FT.SEARCH', 'idx', 'hello*', 'nocontent', 'limit', '0', '200') env.assertGreater(103, len(res)) env.assertLess(1, len(res)) createExpire(env, N) res = env.cmd('FT.SEARCH', 'idx', 'hello*', 'limit', '0', '200') env.assertEqual(toSortedFlatList(res[1:]), toSortedFlatList(['bar', ['txt1', 'hello', 'n', '20'], 'foo', ['txt1', 'hello', 'n', '0']]))
def testStartsWith(env): conn = getConnectionByEnv(env) env.execute_command('ft.create', 'idx', 'SCHEMA', 't', 'TEXT', 'SORTABLE') conn.execute_command('hset', 'doc1', 't', 'aa') conn.execute_command('hset', 'doc2', 't', 'aaa') conn.execute_command('hset', 'doc3', 't', 'ab') res = env.cmd('ft.aggregate', 'idx', '*', 'load', 1, 't', 'apply', 'startswith(@t, "aa")', 'as', 'prefix') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1, ['t', 'aa', 'prefix', '1'], \ ['t', 'aaa', 'prefix', '1'], \ ['t', 'ab', 'prefix', '0']]))
def testSynonymsLowerCase(env): env.expect('FT.CREATE lowcase ON HASH SCHEMA foo text').ok() env.expect('FT.SYNUPDATE lowcase id1 HELLO SHALOM AHALAN').ok() dump = env.cmd('FT.SYNDUMP lowcase') env.assertEqual( toSortedFlatList(dump), toSortedFlatList( (['ahalan', ['id1'], 'shalom', ['id1'], 'hello', ['id1']]))) env.expect('FT.ADD lowcase doc1 1 FIELDS foo hello').ok() env.expect('FT.ADD lowcase doc2 1 FIELDS foo HELLO').ok() res = [2L, 'doc2', ['foo', 'HELLO'], 'doc1', ['foo', 'hello']] env.expect('FT.SEARCH lowcase SHALOM').equal(res) env.expect('FT.SEARCH lowcase shalom').equal(res)
def testMultiFilters2(env): conn = getConnectionByEnv(env) env.expect('FT.CREATE', 'test', 'ON', 'HASH', 'PREFIX', '2', 'student:', 'pupil:', 'FILTER', '@age > 16', 'SCHEMA', 'first', 'TEXT', 'last', 'TEXT', 'age', 'NUMERIC').ok() conn.execute_command('HSET', 'student:yes1', 'first', 'yes1', 'last', 'yes1', 'age', '17') conn.execute_command('HSET', 'student:no1', 'first', 'no1', 'last', 'no1', 'age', '15') conn.execute_command('HSET', 'pupil:yes2', 'first', 'yes2', 'last', 'yes2', 'age', '17') conn.execute_command('HSET', 'pupil:no2', 'first', 'no2', 'last', 'no2', 'age', '15') res1 = [2L, 'pupil:yes2', ['first', 'yes2', 'last', 'yes2', 'age', '17'], 'student:yes1', ['first', 'yes1', 'last', 'yes1', 'age', '17']] res = env.cmd('ft.search test *') env.assertEqual(toSortedFlatList(res), toSortedFlatList(res1))
def testPayload(env): conn = getConnectionByEnv(env) env.expect('ft.create', 'things', 'ON', 'HASH', 'PREFIX', '1', 'thing:', 'PAYLOAD_FIELD', 'payload', 'SCHEMA', 'name', 'text').ok() conn.execute_command('hset', 'thing:foo', 'name', 'foo', 'payload', 'stuff') for _ in env.retry_with_rdb_reload(): waitForIndex(env, 'things') res = env.cmd('ft.search', 'things', 'foo') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1L, 'thing:foo', ['name', 'foo', 'payload', 'stuff']])) res = env.cmd('ft.search', 'things', 'foo', 'withpayloads') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1L, 'thing:foo', 'stuff', ['name', 'foo', 'payload', 'stuff']]))
def testPoneticOnNonePhoneticField(env): env.assertOk(env.cmd('ft.create', 'idx', 'ON', 'HASH', 'schema', 'text', 'TEXT', 'PHONETIC', 'dm:en', 'SORTABLE', 'text1', 'TEXT', 'SORTABLE')) env.assertOk(env.cmd('ft.add', 'idx', 'doc1', 1.0, 'fields', 'text', 'morfix', 'text1', 'phonetic')) env.assertEquals(toSortedFlatList(env.cmd('ft.search', 'idx', 'morphix')), toSortedFlatList([1L, 'doc1', ['text', 'morfix', 'text1', 'phonetic']])) env.assertEquals(toSortedFlatList(env.cmd('ft.search', 'idx', '@text:morphix')), toSortedFlatList([1L, 'doc1', ['text', 'morfix', 'text1', 'phonetic']])) env.assertEquals(toSortedFlatList(env.cmd('ft.search', 'idx', 'phonetic')), toSortedFlatList([1L, 'doc1', ['text', 'morfix', 'text1', 'phonetic']])) env.assertEquals(env.cmd('ft.search', 'idx', 'fonetic'), [0L]) env.assertEquals(env.cmd('ft.search', 'idx', '@text1:morphix'), [0L]) with env.assertResponseError(): env.cmd('ft.search', 'idx', '@text1:morphix=>{$phonetic:true}') with env.assertResponseError(): env.cmd('ft.search', 'idx', '@text1:morphix=>{$phonetic:false}')
def testPoneticWithSmallTerm(env): env.assertOk(env.cmd('ft.create', 'complainants', 'ON', 'HASH', 'SCHEMA', 'name', 'text', 'PHONETIC', 'dm:en', 'almamater', 'text', 'PHONETIC', 'dm:en')) env.assertOk(env.cmd('ft.add', 'complainants', 'foo64', 1.0, 'FIELDS', 'name', 'jon smith', 'almamater', 'Trent')) env.assertOk(env.cmd('ft.add', 'complainants', 'foo65', 1.0, 'FIELDS', 'name', 'john jones', 'almamater', 'Toronto')) res = env.cmd('ft.search', 'complainants', '@name:(john=>{$phonetic:true})') env.assertEqual(toSortedFlatList(res), toSortedFlatList([2L, 'foo64', ['name', 'jon smith', 'almamater', 'Trent'], 'foo65', ['name', 'john jones', 'almamater', 'Toronto']]))
def testContains(env): conn = getConnectionByEnv(env) env.execute_command('ft.create', 'idx', 'SCHEMA', 't', 'TEXT', 'SORTABLE') conn.execute_command('hset', 'doc1', 't', 'aa') conn.execute_command('hset', 'doc2', 't', 'bba') conn.execute_command('hset', 'doc3', 't', 'aba') conn.execute_command('hset', 'doc4', 't', 'abb') conn.execute_command('hset', 'doc5', 't', 'abba') conn.execute_command('hset', 'doc6', 't', 'abbabb') # check count of contains res = env.cmd('ft.aggregate', 'idx', '*', 'load', 1, 't', 'apply', 'contains(@t, "bb")', 'as', 'substring') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1, ['t', 'aa', 'substring', '0'], \ ['t', 'bba', 'substring', '1'], \ ['t', 'aba', 'substring', '0'], \ ['t', 'abb', 'substring', '1'], \ ['t', 'abba', 'substring', '1'], \ ['t', 'abbabb', 'substring', '2']])) # check filter by contains res = env.cmd('ft.aggregate', 'idx', '*', 'load', 1, 't', 'filter', 'contains(@t, "bb")') env.assertEqual(toSortedFlatList(res)[1:], toSortedFlatList([['t', 'bba'], \ ['t', 'abb'], \ ['t', 'abba'], \ ['t', 'abbabb']])) # check count of contains with empty string. (returns length of string + 1) res = env.cmd('ft.aggregate', 'idx', '*', 'load', 1, 't', 'apply', 'contains(@t, "")', 'as', 'substring') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1, ['t', 'aa', 'substring', '3'], \ ['t', 'bba', 'substring', '4'], \ ['t', 'aba', 'substring', '4'], \ ['t', 'abb', 'substring', '4'], \ ['t', 'abba', 'substring', '5'], \ ['t', 'abbabb', 'substring', '7']])) # check filter by contains with empty string res = env.cmd('ft.aggregate', 'idx', '*', 'load', 1, 't', 'filter', 'contains(@t, "")') env.assertEqual(toSortedFlatList(res)[1:], toSortedFlatList([['t', 'aa'], \ ['t', 'bba'], \ ['t', 'aba'], \ ['t', 'abb'], \ ['t', 'abba'], \ ['t', 'abbabb']]))
def testContains(env): conn = getConnectionByEnv(env) env.execute_command('ft.create', 'idx', 'SCHEMA', 't', 'TEXT', 'SORTABLE') conn.execute_command('hset', 'doc1', 't', 'aa') conn.execute_command('hset', 'doc2', 't', 'bba') conn.execute_command('hset', 'doc3', 't', 'aba') conn.execute_command('hset', 'doc4', 't', 'abb') conn.execute_command('hset', 'doc5', 't', 'abba') conn.execute_command('hset', 'doc6', 't', 'abbabb') res = env.cmd('ft.aggregate', 'idx', '*', 'load', 1, 't', 'apply', 'contains(@t, "bb")', 'as', 'substring') env.assertEqual(toSortedFlatList(res), toSortedFlatList([1L, ['t', 'aa', 'substring', '0'], \ ['t', 'bba', 'substring', '1'], \ ['t', 'aba', 'substring', '0'], \ ['t', 'abb', 'substring', '1'], \ ['t', 'abba', 'substring', '1'], \ ['t', 'abbabb', 'substring', '2']]))
def testFailedHighlight(env): #test NOINDEX env.cmd('ft.create', 'idx', 'ON', 'HASH', 'PREFIX', 1, 'doc1', 'SCHEMA', 'f1', 'TEXT', 'f2', 'TEXT', 'f3', 'TEXT', 'NOINDEX') waitForIndex(env, 'idx') env.cmd('ft.add', 'idx', 'doc1', '1.0', 'FIELDS', 'f1', 'foo foo foo', 'f2', 'bar bar bar', 'f3', 'baz baz baz') env.assertEqual( toSortedFlatList([ 1L, 'doc1', ['f1', 'foo foo foo', 'f2', 'bar bar bar', 'f3', 'baz baz baz'] ]), toSortedFlatList(env.cmd('ft.search idx foo'))) env.assertEqual( toSortedFlatList([ 1L, 'doc1', [ 'f1', '<b>foo</b> <b>foo</b> <b>foo</b>', 'f2', 'bar bar bar', 'f3', 'baz baz baz' ] ]), toSortedFlatList( env.cmd('ft.search', 'idx', 'foo', 'highlight', 'fields', '1', 'f1'))) env.assertEqual( toSortedFlatList([ 1L, 'doc1', ['f2', 'bar bar bar', 'f1', 'foo foo foo', 'f3', 'baz baz baz'] ]), toSortedFlatList(env.cmd('ft.search idx foo highlight fields 1 f2'))) env.assertEqual( toSortedFlatList([ 1L, 'doc1', ['f3', 'baz baz baz', 'f1', 'foo foo foo', 'f2', 'bar bar bar'] ]), toSortedFlatList(env.cmd('ft.search idx foo highlight fields 1 f3'))) #test empty string env.cmd('ft.create', 'idx2', 'ON', 'HASH', 'PREFIX', 1, 'doc2', 'SCHEMA', 'f1', 'TEXT', 'f2', 'TEXT', 'f3', 'TEXT') waitForIndex(env, 'idx') env.cmd('ft.add', 'idx2', 'doc2', '1.0', 'FIELDS', 'f1', 'foo foo foo', 'f2', '', 'f3', 'baz baz baz') env.assertEqual( toSortedFlatList([ 1L, 'doc2', [ 'f1', '<b>foo</b> <b>foo</b> <b>foo</b>', 'f2', '', 'f3', 'baz baz baz' ] ]), toSortedFlatList(env.cmd('ft.search idx2 foo highlight fields 1 f1'))) env.assertEqual( toSortedFlatList( [1L, 'doc2', ['f2', '', 'f1', 'foo foo foo', 'f3', 'baz baz baz']]), toSortedFlatList(env.cmd('ft.search idx2 foo highlight fields 1 f2'))) env.assertEqual( toSortedFlatList( [1L, 'doc2', ['f3', 'baz baz baz', 'f1', 'foo foo foo', 'f2', '']]), toSortedFlatList(env.cmd('ft.search idx2 foo highlight fields 1 f3'))) #test stop word list env.cmd('ft.create', 'idx3', 'ON', 'HASH', 'PREFIX', 1, 'doc3', 'SCHEMA', 'f1', 'TEXT', 'f2', 'TEXT', 'f3', 'TEXT') waitForIndex(env, 'idx') env.cmd('ft.add', 'idx3', 'doc3', '1.0', 'FIELDS', 'f1', 'foo foo foo', 'f2', 'not a', 'f3', 'baz baz baz') env.assertEqual( toSortedFlatList([ 1L, 'doc3', [ 'f1', '<b>foo</b> <b>foo</b> <b>foo</b>', 'f2', 'not a', 'f3', 'baz baz baz' ] ]), toSortedFlatList(env.cmd('ft.search idx3 foo highlight fields 1 f1'))) env.assertEqual( toSortedFlatList([ 1L, 'doc3', ['f2', 'not a', 'f1', 'foo foo foo', 'f3', 'baz baz baz'] ]), toSortedFlatList(env.cmd('ft.search idx3 foo highlight fields 1 f2'))) env.assertEqual( toSortedFlatList([ 1L, 'doc3', ['f3', 'baz baz baz', 'f1', 'foo foo foo', 'f2', 'not a'] ]), toSortedFlatList(env.cmd('ft.search idx3 foo highlight fields 1 f3')))
def testSortBy(self): res = self.env.cmd('ft.aggregate', 'games', '*', 'GROUPBY', '1', '@brand', 'REDUCE', 'sum', 1, '@price', 'as', 'price', 'SORTBY', 2, '@price', 'desc', 'LIMIT', '0', '2') self.env.assertListEqual([292, ['brand', '', 'price', '44780.69'], [ 'brand', 'mad catz', 'price', '3973.48']], res) res = self.env.cmd('ft.aggregate', 'games', '*', 'GROUPBY', '1', '@brand', 'REDUCE', 'sum', 1, '@price', 'as', 'price', 'SORTBY', 2, '@price', 'asc', 'LIMIT', '0', '2') self.env.assertListEqual([292, ['brand', 'myiico', 'price', '0.23'], [ 'brand', 'crystal dynamics', 'price', '0.25']], res) # Test MAX with limit higher than it res = self.env.cmd('ft.aggregate', 'games', '*', 'GROUPBY', '1', '@brand', 'REDUCE', 'sum', 1, '@price', 'as', 'price', 'SORTBY', 2, '@price', 'asc', 'MAX', 2) self.env.assertListEqual([292, ['brand', 'myiico', 'price', '0.23'], [ 'brand', 'crystal dynamics', 'price', '0.25']], res) # Test Sorting by multiple properties res = self.env.cmd('ft.aggregate', 'games', '*', 'GROUPBY', '1', '@brand', 'REDUCE', 'sum', 1, '@price', 'as', 'price', 'APPLY', '(@price % 10)', 'AS', 'price', 'SORTBY', 4, '@price', 'asc', '@brand', 'desc', 'MAX', 10, ) self.env.assertListEqual([292, ['brand', 'zps', 'price', '0'], ['brand', 'zalman', 'price', '0'], ['brand', 'yoozoo', 'price', '0'], ['brand', 'white label', 'price', '0'], ['brand', 'stinky', 'price', '0'], [ 'brand', 'polaroid', 'price', '0'], ['brand', 'plantronics', 'price', '0'], ['brand', 'ozone', 'price', '0'], ['brand', 'oooo', 'price', '0'], ['brand', 'neon', 'price', '0']], res) # test LOAD with SORTBY expected_res = [2265, ['title', 'Logitech MOMO Racing - Wheel and pedals set - 6 button(s) - PC, MAC - black', 'price', '759.12'], ['title', 'Sony PSP Slim & Lite 2000 Console', 'price', '695.8']] res = self.env.cmd('ft.aggregate', 'games', '*', 'LOAD', 1, '@title', 'SORTBY', 2, '@price', 'desc', 'LIMIT', '0', '2') self.env.assertListEqual(toSortedFlatList(res), toSortedFlatList(expected_res)) res = self.env.cmd('ft.aggregate', 'games', '*', 'SORTBY', 2, '@price', 'desc', 'LOAD', 1, '@title', 'LIMIT', '0', '2') self.env.assertListEqual(toSortedFlatList(res), toSortedFlatList(expected_res)) # test with non-sortable filed expected_res = [2265, ['description', 'world of warcraft:the burning crusade-expansion set'], ['description', 'wired playstation 3 controller, third party product with high quality.']] res = self.env.cmd('ft.aggregate', 'games', '*', 'SORTBY', 2, '@description', 'desc', 'LOAD', 1, '@description', 'LIMIT', '0', '2') self.env.assertListEqual(toSortedFlatList(res), toSortedFlatList(expected_res)) res = self.env.cmd('ft.aggregate', 'games', '*', 'LOAD', 1, '@description', 'SORTBY', 2, '@description', 'desc', 'LIMIT', '0', '2') self.env.assertListEqual(toSortedFlatList(res), toSortedFlatList(expected_res))