简体   繁体   English

如何在ElasticSearch for Rails中为特定字段定义类型

[英]How to define type for a specific field in ElasticSearch for Rails

I am struggling with elasticsearch-rails. 我正在为elasticsearch-rails奋斗。

I have the following mapping: 我有以下映射:

{
  "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"
          }
        }
      }
    }
  }
}

I would like to change the type for the geojson field from string to geo_point so I can use the geo_shape query on it. 我想将geojson字段的类型从string更改为geo_point以便可以在其上使用geo_shape查询。

I tried this in my model: 我在模型中尝试过:

  settings index: { number_of_shards: 1 } do
    mappings dynamic: 'false' do
      indexes :geojson, type: 'geo_shape'
    end
  end

with peculiar results. 结果奇特。 When I queried the mapping with $ curl 'localhost:9200/_all/_mapping?pretty' , the geojson field still shows as type: string . 当我使用$ curl 'localhost:9200/_all/_mapping?pretty'查询映射时,geojson字段仍显示为type: string

Within a Rails console, if I do Listing.mappings.to_hash , it seems to show that the geojson field is of type geo_shape . 在Rails控制台中,如果执行Listing.mappings.to_hash ,则似乎表明geojson字段的类型为geo_shape

And yet when running this query: 但是,当运行此查询时:

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 complains that the geojson field is not of type geo_shape . ES抱怨geojson字段的类型不是geo_shape

What am I missing? 我想念什么? How do I tell ES that I want the geojson field to be of type geo_shape and not string ? 如何告诉ES我希望geojson字段的类型为geo_shape而不是string

The issue was that I didn't delete and recreate the mapping. 问题是我没有删除并重新创建映射。

In the rails console, I ran Model.__elasticsearch__.delete_index! 在rails控制台中,我运行了Model.__elasticsearch__.delete_index! and then Model.__elasticsearch__.create_index! 然后是Model.__elasticsearch__.create_index! followed by Model.import 其次是Model.import

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM