ruby-on-rails - 如何在 ElasticSearch for Rails 中为特定字段定义类型

标签 ruby-on-rails elasticsearch

我在 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/

相关文章:

ruby-on-rails - Carrierwave、Fog、Amazon S3、Rails 4

ruby-on-rails - 从单个搜索字段中搜索整数和字符串值

ruby-on-rails - rails 和重构,针对 vim 用户的建议工具和技术?

mongodb - 如何知道整个集合是否在mongodb river中被索引

elasticsearch - Elasticsearch 2.4中数组字段匹配过滤器的不同值

ruby-on-rails - GitLab API 访问 : Failed to connect to internal API

ruby-on-rails - Ruby on Rails。波形音频 gem

search - 如何在 GitHub 代码搜索中转义字符?

elasticsearch - ELK数据插入由于类型映射失败而失败,因为实际数据类型发生了更改。

elasticsearch - 使用 Logstash 将数据从 Elasticsearch 导出到 CSV