Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

multi_field ActiveModel mappings not working #613

Closed
amejiarosario opened this issue Feb 4, 2013 · 8 comments
Closed

multi_field ActiveModel mappings not working #613

amejiarosario opened this issue Feb 4, 2013 · 8 comments

Comments

@amejiarosario
Copy link

I'm using elastic search to enhance search capabilities in my app. Search is working perfectly, however sorting is not for fields with multiple words.

When I try to sort the search by log 'message', I was getting the error:

"Can't sort on string types with more than one value per doc, or more than one token per field"

I googled the error and find out that I can use multi-fields mapping on the :message field (one analyzed and the other one not) to sort them. So I did this:

class Log < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks

  tire.mapping do
    indexes :id, index: :not_analyzed
    indexes :source, type: 'string'
    indexes :level, type: 'string'
    indexes :created_at, :type => 'date', :include_in_all => false
    indexes :updated_at, :type => 'date', :include_in_all => false
    indexes :message, type: 'multi_field', fields: { 
      analyzed: {type: 'string', index: 'analyzed'},
      message: {type: 'string', index: :not_analyzed} 
    }
    indexes :domain, type: 'keyword'
  end
end

But, for some reason is not passing this mapping to ES.
rails console:

Log.index.delete #=> true
Log.index.create #=> 200 : {"ok":true,"acknowledged":true}
Log.index.import Log.all #=> 200 : {"took":243,"items":[{"index":{"_index":"logs","_type":"log","_id":"5 ... ...

# Index mapping for :message is not the multi-field 
# as I created in the Log model... why?

Log.index.mapping
=> {"log"=>
  {"properties"=>
    {"created_at"=>{"type"=>"date", "format"=>"dateOptionalTime"},
     "id"=>{"type"=>"long"},
     "level"=>{"type"=>"string"},
     "message"=>{"type"=>"string"},
     "source"=>{"type"=>"string"},
     "updated_at"=>{"type"=>"date", "format"=>"dateOptionalTime"}}}}

# However if I do a Log.mapping I can see the multi-field
# how I can fix that and pass the mapping correctly to ES? 

Log.mapping
=> {:id=>{:index=>:not_analyzed, :type=>"string"},
 :source=>{:type=>"string"},
 :level=>{:type=>"string"},
 :created_at=>{:type=>"date", :include_in_all=>false},
 :updated_at=>{:type=>"date", :include_in_all=>false},
 :message=>
  {:type=>"multi_field",
   :fields=>
    {:message=>{:type=>"string", :index=>"analyzed"},
     :untouched=>{:type=>"string", :index=>:not_analyzed}}},
 :domain=>{:type=>"keyword"}}

So, Log.index.mapping is the current mapping in ES which doesn't contain the multi-field that I created. Am I missing something? and why the multi-field is shown in Log.mapping but not in Log.index.mapping?

@karmi
Copy link
Owner

karmi commented Feb 4, 2013

You have to use the Log.create_elasticsearch_index, which picks up the correct mapping, or pass the mapping to the create method. See the source code for the import task.


Admittedly, the Log.index.create method should be smarter, pick up the correct mapping and not violate the principle of least surprise like this :)

@amejiarosario
Copy link
Author

So, I changed the workflow from:

irb> Log.index.delete; Log.index.create; Log.import

to

irb> Log.index.delete; Log.create_elasticsearch_index; Log.import

and now ES is picking up the mappings. Thanks!

@karmi
Copy link
Owner

karmi commented Feb 4, 2013

Great. Also, please put the information as an answer to your question here: http://stackoverflow.com/questions/14690975/why-multi-field-mapping-is-not-working-with-tire-gem-for-elasticsearch, and accept it, so it's not unanswered.

By the way, I greatly appreciate when people create either Github issue or Stackoverflow question, not both .)

@amejiarosario
Copy link
Author

Ok. Will do.

@davidguthu
Copy link

Wow this wasted quite a bit of my time. Could you put a deprecation warning on the other method or fix it?

@karmi
Copy link
Owner

karmi commented Jun 6, 2013

@davidguthu What has? That Log.index.create doesn't take mapping/settings into account?

@vburca
Copy link

vburca commented Jun 11, 2013

I just bumped into this problem as well. Good that I remembered I've seen it before, here (while looking for other things, obviously).

Maybe it would be a good idea of putting it in the documentation / examples, somewhere.

Thanks for the provided support, overall!

@karmi
Copy link
Owner

karmi commented Jun 11, 2013

@vburca I've added the info to the Readme (d97f7fc), agree that it's frustrating and surprising...

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants