ankane / searchkick

Intelligent search made easy
MIT License
6.55k stars 759 forks source link

ActiveRecord JSON field disable numeric and date detection in ElasticSearch. #1024

Closed esiegel closed 6 years ago

esiegel commented 6 years ago

Is there a way to disable both numeric_detection and date_detection for a single ActiveRecord attribute (source)? Or do I have to create a custom Elasticsearch mapping, which includes all fields, and also requires custom searching?

To help explain, in my case I have an ActiveRecord model, which has a JSON attribute properties. I enforce that the key:values are one level deep and that the values are all strings in the DB. Beyond that the keys and values are opaque and user generated.

class Foo < ActiveRecord::Base
  # property: attribute that is JSON type in the DB
end

The problem comes when User A tries to store {my_date: '2017-11-14'} and then User B tries to store the same key with a non-valid date string {my_date: '11/14/2017'}. Since user A writes first to Elasticsearch the mapping for my_date will be frozen as type date. If user B had written first then the type would be string and it would have been fine for both.

Ideally I'd like Elasticsearch to not coerce these strings to dates and this seems to be possible with the above linked disable_date setting on the field.

Hope that all made sense and please feel free to ask for clarifications.

ankane commented 6 years ago

Hey @esiegel, my suggestion would be to create a custom mapping that mirrors what Searchkick generates but with numeric_detection and date_detection disabled. If you can successfully do this, you should be able to use Searchkick's search without any issues (and not have to use custom search).

esiegel commented 6 years ago

Thanks @ankane, I'll give that a go.

Before I close the issue, could you go into a little more detail of when using a custom mapping requires using custom searching? The docs make it seem like it is always required. Does searchkick validate that the custom mapping is the same with the actual mapping and if so not require custom searching?

ankane commented 6 years ago

Anytime you use custom mapping, there's a chance you'll break compatibility with searching (which is why the docs are written this way). Searchkick doesn't do any validation, but if you start to get errors or incorrect results after adding a custom mapping, it's likely the cause.

esiegel commented 6 years ago

Thanks again.