ElasticSearch Rails - 设置自定义分析器

时间:2014-08-12 07:03:54

标签: ruby-on-rails ruby ruby-on-rails-4 elasticsearch elasticsearch-plugin

我在Rails 4中使用ElasticSearch通过elasticsearch-rails(https://github.com/elasticsearch/elasticsearch-rails

我有一个用户模型,带有电子邮件属性。

我正在尝试使用文档中描述的'uax_url_email'标记生成器:

class User < ActiveRecord::Base
  include Elasticsearch::Model
  include Elasticsearch::Model::Callbacks

  settings analysis: { analyzer: { whole_email: { tokenizer: 'uax_url_email' } } } do
    mappings dynamic: 'false' do
      indexes :email, analyzer: 'whole_email'
    end
  end

end

我按照wiki(https://github.com/elasticsearch/elasticsearch-rails/wiki)和elasticsearch-model文档(https://github.com/elasticsearch/elasticsearch-rails/wiki)中的示例来实现此目的。

它不起作用。如果我直接查询elasticsearch:

curl -XGET 'localhost:9200/users/_mapping

它返回:

{
  "users": {
    "mappings": {
      "user": {
        "properties": {
          "birthdate": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "created_at": {
            "type": "date",
            "format": "dateOptionalTime"
          },
          "email": {
            "type": "string"
          },
          "first_name": {
            "type": "string"
          },
          "gender": {
            "type": "string"
          },
          "id": {
            "type": "long"
          },
          "last_name": {
            "type": "string"
          },
          "name": {
            "type": "string"
          },
          "role": {
            "type": "string"
          },
          "updated_at": {
            "type": "date",
            "format": "dateOptionalTime"
          }
        }
      }
    }
  }
}

1 个答案:

答案 0 :(得分:15)

这最终成为我创建索引的问题。我在努力:

User.__elasticsearch__.client.indices.delete index: User.index_name
User.import

我希望这会删除索引,然后重新导入值。但是我需要这样做:

User.__elasticsearch__.create_index! force: true
User.import