views:

41

answers:

1

Hello everyone:

How can I use sphinx to search french words which the entries in the db is actually english?

The situation is:

I have a ROR project with a table in the db called "categories", and the category names are in english, category has many "question" entries.

In localization file config/locals/fr.yml, these categories were translated to french.

Consider about expandability, we can't change the category names in the db to french.

User can search by type part of the key word.

Here is a example: Category Name: Health and Medical In french: Santé et médecine

so how can I do this: type "Santé médecine abc" in the search field and sphinx will return the "questions" under "Health and Medical" category and have keyword "abc"?

A: 

First, I think that you shouldn't use yml file to translate db data. You can use yml to translate db column names, or model names and so on, but not data that is stored in db. It is bad design. The best would be to store different translations in db, but you wanted to know how to do such search without storing it in db, so:

You can do it in many various ways. If you generate links to tags (like here on SO) then it is very easy. You should generate those links like this:

# controller
@tags = Tag.all

# view 
<% @tags.each do |tag| %>
  <%= link_to I18n.t tag.name, search_tag_path(tag) %>
<% end %>

Or something similar according to your models and routes.

If you want to have some checkboxes or select fields with which you select tags, then you should do it similary to above example:

# controller
@tags = Tag.all

# view
<% form_tag search_path do |f| %>
  <% @tags.each do |tag| %>
    <%= I18n.t tag.name %> <%= check_box_tag "tags[#{tag.id}]" %>
  <% end %>
<% end %>

Here on submit you would get array of ids of selected tags: params[tags].

klew
Thanks for answer, there are a lot of categories, so make a list of categories for user to select to get the category id is not reasonable. The customer want to type part of the category name in the search box to find out all the "questions" under that category, the search engine is Sphinx.
Daniel
Do you mean that input can be a part of a category in other language, then it should get all questions from matched categories?
klew
If yes, then what you should do is, first, match all category names from yml file and get category ids from it. Then fetch everything from db. Sphinx isn't needed here. And as far as I know (I know only a little bit) about Sphinx is that it speeks directly to db. So probably it can only find data that is in db. So if you have something in yml, it won't be used by sphinx. So once again: you should put your translations in db.
klew
ok, I will try to put the translations to db, thank you very much
Daniel