简体   繁体   中英

ElasticSearch Rails - Setting a Custom Analyzer

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM