无法在ElasticSearch中对非模型字段进行索引



我将Rails 6与elasticsearch-railsgem一起使用。我试图对函数的结果进行索引,但它没有出现在索引中,也不可搜索。这是我的课。

class Asset < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def my_field
return "this is my field"
end
def as_indexed_json(options={})
as_json(only: [:my_field])
end

settings index: {
number_of_shards: 10,
max_ngram_diff: 50
},
analysis: {
filter: {
ngram_filter: {
type: "ngram",
min_gram: "3",
max_gram: "10",
}
},
analyzer: {
ngram_filter: {
type: "custom",
tokenizer: "standard",
filter: ["lowercase", "ngram_filter"]
}
}

} do
mapping do
indexes :my_field, type: "text", analyzer: "standard"
end
end
end

当我用curl查询索引时(我省略了其他命中,因为除了id之外,它们都是一样的(:

curl -X GET 'localhost:9200/assets/_search?_source=true&pretty'
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 10,
"successful" : 10,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1911,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "assets",
"_type" : "_doc",
"_id" : "13",
"_score" : 1.0,
"_source" : { }
}, 

当我对模型中定义的数据库字段进行索引时,一切都按预期进行,但我需要索引函数的返回值。

你试过这个吗,我希望它能

class Asset < ApplicationRecord
include Elasticsearch::Model
include Elasticsearch::Model::Callbacks
def my_field
{ my_field: "this is my field" }
end
def as_indexed_json(options={})
as_json(only: [:id]).merge(my_field)
end
end

最新更新