rails elasticsearch searching a nested json field -
i'm using elasticsearch-model gem model has has_many
relationship. match documentation, let's model article
, relationship has_many categories
. wrote client serializer follows (directly documentation):
def as_indexed_json(options={}) self.as_json( include: { categories: { only: :title}, }) end
the serialization seems work. result of illustration article's as_indexed_json contains "categories" => {"title"=> "one", "title"=> "two", "title"=> "three"}
block.
what i'm struggling with, , haven't been able find in documentation, how search nested field.
here's i've tried:
from elasticsearch documentation on nested query figured ought this:
r = article.search query: { nested: { path: "categories", query: {match: {title: "one"}} } }
but when r.results.first
error: nested object under path [categories] not of nested type]
...
i've tried adding changing 1 line in serializer be: include: { categories: { type: "nested", only: :title}
doesn't alter anything, still says categories not of nested type.
of course, tried querying field without nesting this:
r = article.search query: {match: {categories: 'one'}}
but doesn't homecoming results.
full text search this:
r = article.search query: {match: {_all: 'one'}}
returns results, of course of study want search 'one' in categories field.
any help much appreciated!
rails don't create nesting mapping in elasticsearch. elasticsearch making categories object rather nested kid using dynamic mapping ("when elasticsearch encounters unknown field in document, uses dynamic mapping determine datatype field , automatically adds new field type mapping object without nesting them"). making them nested object need create mapping 1 time again in elasticsearch categories nested type, notice type nested of category.
set /my_index { "mappings": { "article": { "properties": { "categories": { "type": "nested", "properties": { "name": { "type": "string" }, "comment": { "type": "string" }, "age": { "type": "short" }, "stars": { "type": "short" }, "date": { "type": "date" } } } } } } }
after can either reindex info client or if want 0 downtime read here.
p.s: have delete old mapping particular index before creating new one.
ruby-on-rails elasticsearch elasticsearch-model
No comments:
Post a Comment