Forked from knazarov/full_text_search_tarantool.lua
Created
January 25, 2021 12:28
-
-
Save 221V/9f7d05245f807d88f5dc4d650ee06aff to your computer and use it in GitHub Desktop.
Full text search example for Tarantool
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env tarantool | |
local pickle = require('pickle') | |
local yaml = require('yaml') | |
function trivec(str) | |
str = string.lower(str) | |
local vec = "" | |
if #str < 3 then | |
return "" | |
end | |
local res = 0 | |
for i = 1,#str-2 do | |
local c1 = string.sub(str, i, i) | |
local c2 = string.sub(str, i+1, i+1) | |
local c3 = string.sub(str, i+2, i+2) | |
local val = string.byte(c1) * 10000 + | |
string.byte(c2) * 100 + string.byte(c3) | |
res = bit.bor(res, bit.lshift(1ULL, val%64)) | |
end | |
return pickle.pack('Q', res) | |
end | |
box.cfg{} | |
box.schema.space.create('account', {if_not_exists=true}) | |
box.space.account:format({ {name='id',type='unsigned'}, | |
{name='name',type='string'}, | |
{name='trivec',type='string'}, | |
}) | |
box.space.account:create_index( | |
'primary', | |
{ | |
unique = true, | |
parts = { {field = 'id', type = 'unsigned'}}, | |
if_not_exists=true}) | |
box.space.account:create_index( | |
'hash', | |
{unique=false, type='BITSET', parts={3,type='string'},if_not_exists=true}) | |
function put_user(id, name) | |
return box.space.account:put({id, name, trivec(name)}) | |
end | |
function find_user(name_part) | |
local vec = trivec(name_part) | |
local res = {} | |
for _, v in box.space.account.index.hash:pairs(vec, {iterator='BITS_ALL_SET'}) do | |
if string.match(string.lower(v.name), string.lower(name_part)) then | |
table.insert(res, v) | |
end | |
end | |
return res | |
end | |
put_user(1, "Konstantin Nazarov") | |
put_user(2, "Konstantin Osipov") | |
print(yaml.encode(find_user('onst'))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
filonenko-mikhail commented on Nov 3, 2020 •