我在 Elasticsearch rails 上苦苦挣扎。
我有以下映射:
{
"listings" : {
"mappings" : {
"listing" : {
"properties" : {
"address" : {
"type" : "string"
},
"authorized" : {
"type" : "boolean"
},
"categories" : {
"properties" : {
"created_at" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"id" : {
"type" : "long"
},
"name" : {
"type" : "string"
},
"parent_id" : {
"type" : "long"
},
"updated_at" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"url_name" : {
"type" : "string"
}
}
},
"cid" : {
"type" : "string"
},
"city" : {
"type" : "string"
},
"country" : {
"type" : "string"
},
"created_at" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"featured" : {
"type" : "boolean"
},
"geojson" : {
"type" : "string"
},
"id" : {
"type" : "long"
},
"latitude" : {
"type" : "string"
},
"longitude" : {
"type" : "string"
},
"name" : {
"type" : "string"
},
"phone" : {
"type" : "string"
},
"postal" : {
"type" : "string"
},
"province" : {
"type" : "string"
},
"thumbnail_filename" : {
"type" : "string"
},
"updated_at" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"url" : {
"type" : "string"
}
}
}
}
}
}
我想将 geojson 字段的类型从
string
更改为至geo_point
所以我可以使用 geo_shape 查询。我在我的模型中试过这个:
settings index: { number_of_shards: 1 } do
mappings dynamic: 'false' do
indexes :geojson, type: 'geo_shape'
end
end
具有特殊的结果。当我使用
$ curl 'localhost:9200/_all/_mapping?pretty'
查询映射时,geojson 字段仍显示为 type: string
.在 Rails 控制台中,如果我这样做
Listing.mappings.to_hash
,似乎表明 geojson
字段的类型为 geo_shape
.然而在运行这个查询时:
Listing.search(query: { fuzzy_like_this: { fields: [:name], like_text: "gap" } }, query: { fuzzy_like_this_field: { city: { like_text: "San Francisco" } } }, query: { geo_shape: { geojson: { shape: { type: :envelope, coordinates: [[37, -122],[38,-123]] } } } }); response.results.total; response.results.map { |r| puts "#{r._score} | #{r.name}, #{r.city} (lat: #{r.latitude}, lon: #{r.longitude})" }
ES 提示
geojson
字段不是 geo_shape
类型.我错过了什么?我如何告诉 ES 我想要
geojson
字段类型为 geo_shape
而不是 string
?
最佳答案
问题是我没有删除并重新创建映射。
在 Rails 控制台中,我运行了 Model.__elasticsearch__.delete_index!
然后 Model.__elasticsearch__.create_index!
后跟 Model.import
关于ruby-on-rails - 如何在 ElasticSearch for Rails 中为特定字段定义类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25493806/