11

I'm using ElasticSearch in Rails 4 through elasticsearch-rails (https://github.com/elasticsearch/elasticsearch-rails)

I have a User model, with an email attribute.

I'm trying to use the 'uax_url_email' tokenizer described in the docs:

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

I followed examples in the wiki (https://github.com/elasticsearch/elasticsearch-rails/wiki) and the elasticsearch-model docs (https://github.com/elasticsearch/elasticsearch-rails/wiki) to arrive at this.

It doesn't work. If I query elasticsearch directly:

curl -XGET 'localhost:9200/users/_mapping

It returns:

{
  "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"
          }
        }
      }
    }
  }
}
Cam Price-Austin
  • 1,738
  • 2
  • 18
  • 31

1 Answers1

16

This ended up being an issue with how I was creating the index. I was trying:

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

I expected this to delete the index, then re-import the values. However I needed to do:

User.__elasticsearch__.create_index! force: true
User.import
Cam Price-Austin
  • 1,738
  • 2
  • 18
  • 31
  • 2
    christ this randomly saved my life. had absolutely no clue why some records weren't getting imported. frankly, i'm still not 100% sure why THIS works, but just doing User.import without the index delete doesnt. – volk Jan 01 '15 at 21:58