0
votes

I have a Sphinx index with UTF-8 documents, in particular the names of artists. For various reasons, we have the name both as a field (indexed_name) and as an attribute (name). When I search for a document, I find it correctly, but the attribute is being returned corrupted:

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)

It looks like the attributes were originally UTF-8 but were treated as ISO-8859-1 and then converted back to UTF-8. When I do this in Ruby, it looks like it goes through it a second time:

[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"

Is this a bug in Sphinx, or am I doing something wrong?

I can reverse it by cycling it through ISO-8859-1 and 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"

Is that going to work, though, for characters in other ISO-8859-* character sets and for things that legitimately need Unicode?

Update 1:

The answer to the second question is no. Searching for Turkish names:

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)

The second one there is supposed to be "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!'

I'm not sure how Ö got turned in to Ö, which appears to be five bytes wide...

Update 2:

I don't want to post my whole sphinx.conf, but here's the config for the index that's being used here. It's generated by 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
Could you attach sphinx.conf ? - Sergei Lomakov

1 Answers

0
votes

Never mind. The data in our database was double-encoded.