具有UTF-8字符串属性的Sphinx

时间:2012-02-09 18:54:14

标签: ruby ruby-on-rails-3 sphinx thinking-sphinx

我有一个带有UTF-8文档的Sphinx索引,特别是艺术家的名字。由于各种原因,我们将名称作为字段(indexed_name)和属性(名称)。当我搜索文档时,我发现它正确,但该属性被返回损坏:

mysql> select name from artist where match('@indexed_name Sánchez') limit 3;
+---------+--------+-----------------------+
| id      | weight | name                  |
+---------+--------+-----------------------+
| 7843884 |   2642 | Sarita Sánchez     |
| 8519538 |   2642 | Cristhian  Sánchez |
| 3853986 |   2627 | Alfonso  Sánchez   |
+---------+--------+-----------------------+
3 rows in set (0.02 sec)

看起来属性最初是UTF-8,但被视为ISO-8859-1,然后转换回UTF-8。当我在Ruby中执行此操作时,它看起来像是第二次通过它:

[1] pry(main)> rs = Thebes::Sphinxql::Query.run("select name from artist where match('@indexed_name Sánchez')")
=> #<Mysql2::Result:0x000000029bebf8 (omitted...)
[2] pry(main)> name = rs.first['name']
=> "Sarita SÃ\u0083¡nchez"

这是Sphinx中的错误,还是我做错了什么?

我可以通过ISO-8859-1和UTF-8循环来反转它:

[4] pry(main)> name.encode!("ISO-8859-1")
=> "Sarita S\xC3\x83\xC2\xA1nchez"
[5] pry(main)> name.force_encoding("UTF-8")
=> "Sarita Sánchez"
[6] pry(main)> name.encode!("ISO-8859-1")
=> "Sarita S\xC3\xA1nchez"
[7] pry(main)> name.force_encoding("UTF-8")
=> "Sarita Sánchez"

对于其他ISO-8859- *字符集中的字符以及合法需要Unicode的字符,这是否会起作用?

更新1:

第二个问题的答案是否定的。搜索土耳其语名称:

mysql> select name from artist where match('@indexed_name ÖZDEMİR') limit 3;

+---------+--------+-------------------------------+
| id      | weight | name                          |
+---------+--------+-------------------------------+
| 1753230 |   2664 | Nurullah Alper ÖZDEMİR |
| 6973956 |   2664 | YİĞİT ÖZDEMİR |
| 9133770 |   2664 | TAHA ÖZDEMİR           |
+---------+--------+-------------------------------+
3 rows in set (0.01 sec)

第二个应该是“YİĞİTÖZDEMİR。”

[2] pry(main)> rs = Thebes::Sphinxql::Query.run("select name from artist where match('@indexed_name ÖZDEMİR') limit 3")
=> #<Mysql2::Result:0x000000047779b0...
[5] pry(main)> name = rs.to_a[1]['name'].dup
=> "YÃ\u0084°Ã\u0084žÃ\u0084°T Ã\u0083â\u0080\u0093ZDEMÃ\u0084°R"
[6] pry(main)> name.encode!("ISO-8859-1")
=> "Y\xC3\x84\xC2\xB0\xC3\x84\xC5\xBE\xC3\x84\xC2\xB0T \xC3\x83\xE2\x80\x93ZDEM\xC3\x84\xC2\xB0R"
[7] pry(main)> name.force_encoding("UTF-8")
=> "YİĞİT ÖZDEMİR"
[8] pry(main)> name.encode!("ISO-8859-1")
Encoding::UndefinedConversionError: U+017E from UTF-8 to ISO-8859-1
from (pry):8:in `encode!'

我不确定如何转入Ã-,这似乎是五个字节宽......

更新2:

我不想发布我的整个sphinx.conf,但这里是这里使用的索引的配置。它由Thinking Sphinx生成。

source artist_core_0
{
  type = mysql
  sql_host = (omitted)
  sql_user = (omitted)
  sql_pass = (omitted)
  sql_db = (omitted)
  sql_query_pre = SET NAMES utf8
  sql_query_pre = SET TIME_ZONE = '+0:00'
  sql_query = (omitted)
  sql_query_range = SELECT IFNULL(MIN(`id`), 1), IFNULL(MAX(`id`), 1) FROM `artists` 
  sql_attr_uint = sphinx_internal_id
  sql_attr_uint = sphinx_deleted
  sql_attr_uint = class_crc
  sql_attr_float = latitude
  sql_attr_float = longitude
  sql_attr_string = sphinx_internal_class
  sql_attr_string = name
  sql_attr_string = homepage
  sql_attr_string = image
  sql_attr_string = city
  sql_attr_string = state
  sql_attr_string = postal_code
  sql_attr_string = country
  sql_query_info = SELECT * FROM `artists` WHERE `id` = (($id - 0) / 6)
}

index artist_core
{
  source = artist_core_0
  path = (omitted)
  morphology = libstemmer_en, libstemmer_fr, libstemmer_tr, libstemmer_es, libstemmer_de, libstemmer_it
  charset_type = utf-8
  min_prefix_len = 3
  enable_star = 1
}

index artist
{
  type = distributed
  local = artist_core
}

1 个答案:

答案 0 :(得分:0)

没关系。我们数据库中的数据是双重编码的。