Slackbot
11/27/2023, 11:14 AMKishore Nallan
11/27/2023, 11:17 AMcrapthings
11/27/2023, 11:18 AM上海第二医院
华西第二医院招聘岗位。crapthings
11/27/2023, 11:18 AMconst fs = require('fs');
const _ = require('lodash')
const Typesense = require('typesense')
const { load, cut } = require('@node-rs/jieba');
const { log } = require('console');
load()
// const txt5 = fs.readFileSync('./5.txt', 'utf8')
// const txt5 = _.chain(fs.readFileSync('./5.txt', 'utf8')).chunk(150000).map((chunk) => ({ title: chunk.join('') })).value()
// const txt6 = _.chain(fs.readFileSync('./5.txt', 'utf8')).chunk(100000).map((chunk) => chunk.join('')).value()
// const txt3 = fs.readFileSync('./3.txt', 'utf8')
// const txt4 = fs.readFileSync('./4.txt', 'utf8')
const txt4alt = fs.readFileSync('./4alt.txt', 'utf8')
const databaseName123 = 'okaytest123'
const client = new Typesense.Client({
'nodes': [{
'host': 'localhost',
'port': '8108',
'protocol': 'http'
}],
'apiKey': 'xyz',
'connectionTimeoutSeconds': 300
})
const schema = {
'name': databaseName123,
'fields': [
// { name: 'title', type: 'string', locale: 'zh' },
{ name: 'title', type: 'string', locale: 'zh' },
{
name: 'embedding',
type: 'float[]',
embed: {
from: [
'title',
],
model_config: {
model_name: 'ts/all-MiniLM-L12-v2'
}
}
}
]
}
;(async function () {
try {
await client.collections(databaseName123).delete()
} catch {
}
try {
await client.collections().create(schema)
} catch (ex) {
console.log(ex)
}
try {
await client.collections(databaseName123).documents().import([
// { id: '1', title: txt4 },
{ id: '2', title: txt4alt },
// { id: '3', title: txt3 },
// { id: '4', title: txt3 },
])
} catch (ex) {
console.log(ex)
}
const resp = await client.collections(databaseName123).documents().search({
q: `二学校`,
query_by: 'title',
flat_search_cutoff: 20
// prefix: true,
// query_by: 'text',
// exhaustive_search: true
// query_by: 'title',
// pre_segmented_query: true,
// drop_tokens_threshold: 0, typo_tokens_threshold: 0, prefix: false
// sort_by: '_vector_distance:desc'
}).catch(console.log)
// const result = _.map(resp.hits, (item) => {
// return item.highlights
// })
console.log(JSON.stringify(resp, null, 2))
// console.log(txt6.length)
// console.log(JSON.stringify(resp, null, 2))
// console.log(await client.collections().retrieve())
} ())
function filterNonChineseAndControlChars (str) {
return str
const chineseAndControlCharsRegex = /[^\u4E00-\u9FA5]/g
return str.replace(chineseAndControlCharsRegex, '')
}crapthings
11/27/2023, 11:19 AMcrapthings
11/27/2023, 11:20 AMKishore Nallan
11/27/2023, 11:20 AMcrapthings
11/27/2023, 11:25 AM上海第二医院
华西第二医院招聘岗位。
上海第二学校
上海第十分钟敲响钟声
try search “十分钟”
no resultcrapthings
11/27/2023, 11:26 AMcrapthings
11/27/2023, 11:26 AMcrapthings
11/27/2023, 11:29 AMcrapthings
11/27/2023, 11:31 AM上海第二医院
华西第二医院招聘岗位。
上海第二学校
上海第跪分钟敲响钟声
一二三
一分钟Kishore Nallan
11/28/2023, 6:35 AMlocale: zh for the field, it works for me. So when creating the fields, set the zh locale. Example:
curl -k "http://localhost:8108/collections" -X POST -H "Content-Type: application/json" \
-H "X-TYPESENSE-API-KEY: ${TYPESENSE_API_KEY}" -d '{
"name": "docs", "enable_nested_fields": true,
"fields": [
{"name":"title","type":"string", "locale": "zh"}
]
}'crapthings
11/28/2023, 6:46 AMcrapthings
11/28/2023, 6:47 AM二医院
can you search this?Kishore Nallan
11/28/2023, 6:48 AMcrapthings
11/28/2023, 6:50 AMcrapthings
11/28/2023, 6:50 AMcrapthings
11/28/2023, 6:56 AMcrapthings
11/28/2023, 6:58 AMcrapthings
11/28/2023, 6:58 AMKishore Nallan
11/28/2023, 6:59 AMcrapthings
11/28/2023, 6:59 AMcrapthings
11/28/2023, 7:02 AM上海第二医院
华西第二医院招聘岗位。
上海第二学校
上海第跪分钟敲响钟声
一二三
一分钟
广西第一医院
广西第二医院
广西第三医院
广西第四医院
广西第五医院
广西第六医院
广西第七医院
广西第八医院
广西第九医院
广西第十医院crapthings
11/28/2023, 7:03 AMconst fs = require('fs');
const _ = require('lodash')
const Typesense = require('typesense')
const { load, cut } = require('@node-rs/jieba');
const { log } = require('console');
load()
const txt4alt = fs.readFileSync('./4alt.txt', 'utf8')
const databaseName123 = 'okaytest123123123'
const client = new Typesense.Client({
'nodes': [{
'host': 'localhost',
'port': '8108',
'protocol': 'http'
}],
'apiKey': 'xyz',
'connectionTimeoutSeconds': 300
})
const schema = {
'name': databaseName123,
'fields': [
// { name: 'title', type: 'string', locale: 'zh' },
{ name: 'title', type: 'string', locale: 'zh' },
{ name: 'text', type: 'string', locale: 'zh' },
{
name: 'embedding',
type: 'float[]',
embed: {
from: [
'title',
],
model_config: {
model_name: 'ts/all-MiniLM-L12-v2'
}
}
}
]
}
;(async function () {
try {
await client.collections(databaseName123).delete()
} catch {
}
try {
await client.collections().create(schema)
} catch (ex) {
console.log(ex)
}
try {
await client.collections(databaseName123).documents().import([
// { id: '1', title: txt4 },
{ id: '2', title: txt4alt, text: txt4alt },
// { id: '3', title: txt3 },
// { id: '4', title: txt3 },
])
} catch (ex) {
console.log(ex)
}
const resp = await client.collections(databaseName123).documents().search({
q: `十医院`,
query_by: 'title, text',
}).catch(console.log)
console.log(JSON.stringify(resp, null, 2))
} ())Kishore Nallan
11/28/2023, 7:03 AM二医院 example, that's fine rightcrapthings
11/28/2023, 7:04 AMYes, but it seems to me that some specific words have been filtered out for some reason. It doesn't look as expected.Kishore Nallan
11/28/2023, 8:23 AM二医院 query, the reason why 二 is not matched in the results is because the text 上海第二医院华西第二医院招聘岗位 is actually tokenized in a way such that 二医院 is split among 2 different words like this:
"第二", "医院"
So when you search for 二医院 the query gets tokenized as 二, 医院 but we are not able to find either token in a single word so we have to drop 二 and only search for 医院
I suspect a similar issue for the other example. The tokenizer is context dependent.crapthings
11/28/2023, 8:30 AMIs this the same issue as with English? Is it an application issue? For example, if I want to search for "abcdpython", I can search for "abcd" from the beginning and it will match, but if I search for "cdpyt" from the middle, it won't match.crapthings
11/28/2023, 8:30 AMcrapthings
11/28/2023, 8:30 AMKishore Nallan
11/28/2023, 8:31 AMKishore Nallan
11/28/2023, 8:31 AMcrapthings
11/28/2023, 8:33 AMcrapthings
11/28/2023, 8:41 AMcrapthings
11/28/2023, 8:42 AMKishore Nallan
11/28/2023, 8:43 AMKishore Nallan
11/28/2023, 8:44 AM