This message was deleted.
# community-help
s
This message was deleted.
k
Please provide one sample document where I can try this on?
c
Copy code
上海第二医院
华西第二医院招聘岗位。
Copy code
const 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, '')
}
like 二学校 二医院 二学区
it looks like 一二三四五六七八九十 lost somehow
k
Ok will look and get back to you
c
Copy code
上海第二医院
华西第二医院招聘岗位。


上海第二学校
上海第十分钟敲响钟声
try search “十分钟” no result
but 分钟 isok
十分钟
but add random word ok like “跪分钟”
only 一分钟 works for such document
Copy code
上海第二医院
华西第二医院招聘岗位。


上海第二学校
上海第跪分钟敲响钟声


一二三

一分钟
k
When I tried using
locale: zh
for the field, it works for me. So when creating the fields, set the
zh
locale. Example:
Copy code
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"}
        ]
      }'
c
weird I have this field
Copy code
二医院
can you search this?
k
Yes, see here.
c
why 二 is missing?
matched_tokens: [“二”,“医院“]
十分钟 and you can try “十分钟”
The result is just as expected.
but 二医院 something like 二医院,is not
k
I will check
c
一 is ok,but 二 is not,and 二三四五六七八九十 doesnt work
test data
Copy code
上海第二医院
华西第二医院招聘岗位。


上海第二学校
上海第跪分钟敲响钟声


一二三

一分钟
广西第一医院
广西第二医院
广西第三医院
广西第四医院
广西第五医院
广西第六医院
广西第七医院
广西第八医院
广西第九医院
广西第十医院
Copy code
const 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))
} ())
k
If I check for
二医院
example, that's fine right
c
Copy code
Yes, but it seems to me that some specific words have been filtered out for some reason. It doesn't look as expected.
k
For the
二医院
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:
Copy code
"第二", "医院"
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.
c
Copy code
Is 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.
is it possible to search from middle?
like python ytho
k
Correct. However, in English words are usually separated into spaces already and there is less confusion on how to split words.
It's not possible to search in the middle.
c
ok got it~
Is it possible to build own ICU to manage own dictionary?
k
We don't have a way to manage an external dictionary at the moment.
I suspect that dictionary based approaches are always going to have problems. Maybe if you can find a better tokenizer for Chinese, we can consider using that.