如何在带有 Java class 的 Elasticsearch 中将 geo_point 与 geohash 一起使用?
How to use geo_point with geohash in Elasticsearch with a Java class?
我有一个 Java class 如下所示(GeoPoint 是一种 Elasticsearch 类型):
private Long id;
private Integer genre;
private String cityName;
private GeoPoint geoPoint;
private Date lastUpdate;
private Double lat;
private Double lon;
我使用的 Elasticsearch 映射是:
{
"location": {
"properties": {
"id": {"type": "long"},
"genre": {"type": "integer"},
"cityName": {"type": "string"},
"geoPoint": {
"type": "geo_point",
"geohash": true,
"geohash_prefix": true,
"geohash_precision": 7
},
"lastUpdate": {"type": "date", format: "yyyy/MM/dd HH:mm:ss"}
}
}
}
尝试对其编制索引时,出现以下异常:
org.elasticsearch.ElasticsearchParseException: field must be either
lat/lon or geohash
从 line 381 of the GeoUtils class 抛出异常。它发生在检查映射中的双纬度和经度字段之后 class,就像 GeoPoint 属性一样。
我不明白为什么它不起作用,因为我按照 ElasticSearch documentation 的建议将 geoPoint 的字段类型设置为 geo_point。
更新 1
Java class.
public class UserLocation implements Serializable {
private Long id;
private Integer genre;
private String cityName;
private GeoPoint geoPoint;
private Date lastUpdate;
public UserLocation () {
}
public UserLocation (UserLocationLite userLocationLite) {
this.id = userLocationLite.getId();
this.genre = userLocationLite.getGenre();
this.cityName = userLocationLite.getCity();
this.geoPoint =
new GeoPoint(userLocationLite.getLatitude(), userLocationLite.getLongitude());
this.lastUpdate = userLocationLite.getActivity();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getGenre() {
return genre;
}
public void setGenre(Integer genre) {
this.genre = genre;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public GeoPoint getGeoPoint() {
return geoPoint;
}
public void setGeoPoint(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
}
索引方法。
@Override
public boolean save(String id, R r) {
try {
return this.transportClient.prepareIndex(this.index, this.type, id)
.setSource(this.objectMapper.writeValueAsString(r))
.execute().actionGet().isCreated();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return false;
}
其中 R 是为另一个 class 实现的一种泛型类型,在本例中使用 UserLocation class,序列化如下。
{"id":40,"genre":1,"cityName":"Madrid","geoPoint":{"lat":42.626595,"lon":-0.488439,"geohash":"ezrm5c0vx832"},"lastUpdate":1402144560000}
更新 2
现在 Java class 结构工作正常。
public class UserLocationSearch implements Serializable {
private Long id;
private Integer genre;
private String cityName;
@JsonIgnore
private GeoPoint geoPoint;
private Date lastUpdate;
public UserLocationSearch() {
this.geoPoint = new GeoPoint();
}
public UserLocationSearch(UserLocationLite userLocationLite) {
this.id = userLocationLite.getId();
this.genre = userLocationLite.getGenre();
this.cityName = userLocationLite.getCity();
this.geoPoint =
new GeoPoint(userLocationLite.getLatitude(), userLocationLite.getLongitude());
this.lastUpdate = userLocationLite.getActivity();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getGenre() {
return genre;
}
public void setGenre(Integer genre) {
this.genre = genre;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public GeoPoint getGeoPoint() {
return geoPoint;
}
public void setGeoPoint(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
public String getGeohash() {
return this.geoPoint.getGeohash();
}
public void setGeohash(String geohash) {
this.geoPoint.resetFromGeoHash(geohash);
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
}
但现在我有另一个问题。
如果得到文件。
获取/user/location/40
{
"_index": "user",
"_type": "location",
"_id": "40",
"_version": 7,
"found": true,
"_source": {
"id": 40,
"genre": 1,
"cityName": "Madrid",
"lastUpdate": 1402144560000,
"geohash": "ezrm5c28d9x0"
}
}
geohash 有 12 个字符,但在映射中 geohash 精度设置为 7...
获取/user/location/_mapping
{
"user": {
"mappings": {
"location": {
"properties": {
"cityName": {
"type": "string"
},
"genre": {
"type": "integer"
},
"geoPoint": {
"type": "geo_point",
"geohash": true,
"geohash_prefix": true,
"geohash_precision": 7
},
"geohash": {
"type": "string"
},
"id": {
"type": "long"
},
"lastUpdate": {
"type": "date",
"format": "yyyy/MM/dd HH:mm:ss"
}
}
}
}
}
}
这意味着它工作不正常?
更新 3
当前 class.
public class UserLocationSearch implements Serializable {
private Long id;
private Integer genre;
private String cityName;
private Location location;
private GeoPoint geoPoint;
private Date lastUpdate;
public UserLocationSearch() {
}
public UserLocationSearch(UserLocationLite userLocationLite) {
this.id = userLocationLite.getId();
this.genre = userLocationLite.getGenre();
this.cityName = userLocationLite.getCity();
this.location = new Location(userLocationLite.getLatitude(), userLocationLite.getLongitude());
this.geoPoint = new GeoPoint(this.location.getGeohash());
this.lastUpdate = userLocationLite.getActivity();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getGenre() {
return genre;
}
public void setGenre(Integer genre) {
this.genre = genre;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public GeoPoint getGeoPoint() {
return geoPoint;
}
public void setGeoPoint(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
public static class GeoPoint{
private String geohash;
public GeoPoint() {
}
public GeoPoint(String geohash) {
this.geohash = geohash;
}
public String getGeohash() {
return geohash;
}
public void setGeohash(String geohash) {
this.geohash = geohash;
}
}
public static class Location{
private Double lat;
private Double lon;
public Location() {
}
public Location(Double lat, Double lon) {
this.lat = lat;
this.lon = lon;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
@JsonIgnore
public String getGeohash(){
return new org.elasticsearch.common.geo.GeoPoint(this.lat, this.lon).getGeohash();
}
}
}
它的映射。
{
"location": {
"properties": {
"id": {"type": "long"},
"genre": {"type": "integer"},
"cityName": {"type": "string"},
"location": {
"type": "geo_point",
"geohash": false
},
"geoPoint": {
"type": "geo_point",
"geohash": true,
"geohash_prefix": true,
"geohash_precision": 7
},
"lastUpdate": {"type": "date", format: "yyyy/MM/dd HH:mm:ss"}
}
}
}
搜索。
按距离(工作正常)。
获取/user/location/_search
{
"query": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "100km",
"location": {
"lat": 42.5,
"lon": -0.49
}
}
}
}
通过 geohash(也可以正常工作,超过 7 的精度将被忽略,因为它是用 "geohash_precision: 7" 映射的)。
获取/user/location/_search
{
"query": {
"filtered": {
"filter": {
"geohash_cell": {
"geoPoint": {
"geohash": "ezrm5c0y5rh8"
},
"neighbors": true,
"precision": 7
}
}
}
}
}
结论
我不明白为什么org.elasticsearch.common.geo.GeoHashUtils.GeoPoint class 与实际Elastic 版本不兼容。
但是,按照@jkbkot 提供的轨道,我决定实施我自己的 GeoPoint 和 Location classes 以获得完全兼容性。
以下 Sense script 应该会让您知道该怎么做:
DELETE location
PUT location
PUT location/location/_mapping
{
"location": {
"properties": {
"id": {"type": "long"},
"genre": {"type": "integer"},
"cityName": {"type": "string"},
"geoPoint": {
"type": "geo_point",
"geohash": true,
"geohash_prefix": true,
"geohash_precision": 7
},
"lastUpdate": {"type": "date", format: "yyyy/MM/dd HH:mm:ss"}
}
}
}
GET location/location/_mapping
PUT location/location/1
{"id":40,"genre":1,"cityName":"Madrid","geoPoint":{"geohash":"ezrm5c0vx832"},"lastUpdate":1402144560000}
GET /location/location/_search
{
"query" : {
"match_all": {}
},
"filter" : {
"geo_distance" : {
"distance" : "40km",
"geoPoint" : {
"lat" : 42.5,
"lon" : -0.49
}
}
}
}
您必须将保存文档的 Java class 转换为 JSON,其结构如下:
{
"id": 40,
"genre": 1,
"cityName": "Madrid",
"geoPoint": {
"geohash": "ezrm5c0vx832"
},
"lastUpdate": 1402144560000
}
或
{
"id": 40,
"genre": 1,
"cityName": "Madrid",
"geoPoint": {
"lat": 42.626595,
"lon": -0.488439
},
"lastUpdate": 1402144560000
}
所以要么你必须删除,例如private GeoPoint geoPoint;
来自你的 Java class 并把 lat
和 lon
留在那里(或者反过来),或者你必须改变你如何序列化 class 到 JSON 字符串 - 省略 geoPoint 或同时省略纬度和经度。
我有一个 Java class 如下所示(GeoPoint 是一种 Elasticsearch 类型):
private Long id;
private Integer genre;
private String cityName;
private GeoPoint geoPoint;
private Date lastUpdate;
private Double lat;
private Double lon;
我使用的 Elasticsearch 映射是:
{
"location": {
"properties": {
"id": {"type": "long"},
"genre": {"type": "integer"},
"cityName": {"type": "string"},
"geoPoint": {
"type": "geo_point",
"geohash": true,
"geohash_prefix": true,
"geohash_precision": 7
},
"lastUpdate": {"type": "date", format: "yyyy/MM/dd HH:mm:ss"}
}
}
}
尝试对其编制索引时,出现以下异常:
org.elasticsearch.ElasticsearchParseException: field must be either lat/lon or geohash
从 line 381 of the GeoUtils class 抛出异常。它发生在检查映射中的双纬度和经度字段之后 class,就像 GeoPoint 属性一样。
我不明白为什么它不起作用,因为我按照 ElasticSearch documentation 的建议将 geoPoint 的字段类型设置为 geo_point。
更新 1
Java class.
public class UserLocation implements Serializable {
private Long id;
private Integer genre;
private String cityName;
private GeoPoint geoPoint;
private Date lastUpdate;
public UserLocation () {
}
public UserLocation (UserLocationLite userLocationLite) {
this.id = userLocationLite.getId();
this.genre = userLocationLite.getGenre();
this.cityName = userLocationLite.getCity();
this.geoPoint =
new GeoPoint(userLocationLite.getLatitude(), userLocationLite.getLongitude());
this.lastUpdate = userLocationLite.getActivity();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getGenre() {
return genre;
}
public void setGenre(Integer genre) {
this.genre = genre;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public GeoPoint getGeoPoint() {
return geoPoint;
}
public void setGeoPoint(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
}
索引方法。
@Override
public boolean save(String id, R r) {
try {
return this.transportClient.prepareIndex(this.index, this.type, id)
.setSource(this.objectMapper.writeValueAsString(r))
.execute().actionGet().isCreated();
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return false;
}
其中 R 是为另一个 class 实现的一种泛型类型,在本例中使用 UserLocation class,序列化如下。
{"id":40,"genre":1,"cityName":"Madrid","geoPoint":{"lat":42.626595,"lon":-0.488439,"geohash":"ezrm5c0vx832"},"lastUpdate":1402144560000}
更新 2
现在 Java class 结构工作正常。
public class UserLocationSearch implements Serializable {
private Long id;
private Integer genre;
private String cityName;
@JsonIgnore
private GeoPoint geoPoint;
private Date lastUpdate;
public UserLocationSearch() {
this.geoPoint = new GeoPoint();
}
public UserLocationSearch(UserLocationLite userLocationLite) {
this.id = userLocationLite.getId();
this.genre = userLocationLite.getGenre();
this.cityName = userLocationLite.getCity();
this.geoPoint =
new GeoPoint(userLocationLite.getLatitude(), userLocationLite.getLongitude());
this.lastUpdate = userLocationLite.getActivity();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getGenre() {
return genre;
}
public void setGenre(Integer genre) {
this.genre = genre;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public GeoPoint getGeoPoint() {
return geoPoint;
}
public void setGeoPoint(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
public String getGeohash() {
return this.geoPoint.getGeohash();
}
public void setGeohash(String geohash) {
this.geoPoint.resetFromGeoHash(geohash);
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
}
但现在我有另一个问题。
如果得到文件。
获取/user/location/40
{
"_index": "user",
"_type": "location",
"_id": "40",
"_version": 7,
"found": true,
"_source": {
"id": 40,
"genre": 1,
"cityName": "Madrid",
"lastUpdate": 1402144560000,
"geohash": "ezrm5c28d9x0"
}
}
geohash 有 12 个字符,但在映射中 geohash 精度设置为 7...
获取/user/location/_mapping
{
"user": {
"mappings": {
"location": {
"properties": {
"cityName": {
"type": "string"
},
"genre": {
"type": "integer"
},
"geoPoint": {
"type": "geo_point",
"geohash": true,
"geohash_prefix": true,
"geohash_precision": 7
},
"geohash": {
"type": "string"
},
"id": {
"type": "long"
},
"lastUpdate": {
"type": "date",
"format": "yyyy/MM/dd HH:mm:ss"
}
}
}
}
}
}
这意味着它工作不正常?
更新 3
当前 class.
public class UserLocationSearch implements Serializable {
private Long id;
private Integer genre;
private String cityName;
private Location location;
private GeoPoint geoPoint;
private Date lastUpdate;
public UserLocationSearch() {
}
public UserLocationSearch(UserLocationLite userLocationLite) {
this.id = userLocationLite.getId();
this.genre = userLocationLite.getGenre();
this.cityName = userLocationLite.getCity();
this.location = new Location(userLocationLite.getLatitude(), userLocationLite.getLongitude());
this.geoPoint = new GeoPoint(this.location.getGeohash());
this.lastUpdate = userLocationLite.getActivity();
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Integer getGenre() {
return genre;
}
public void setGenre(Integer genre) {
this.genre = genre;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public GeoPoint getGeoPoint() {
return geoPoint;
}
public void setGeoPoint(GeoPoint geoPoint) {
this.geoPoint = geoPoint;
}
public Date getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(Date lastUpdate) {
this.lastUpdate = lastUpdate;
}
public static class GeoPoint{
private String geohash;
public GeoPoint() {
}
public GeoPoint(String geohash) {
this.geohash = geohash;
}
public String getGeohash() {
return geohash;
}
public void setGeohash(String geohash) {
this.geohash = geohash;
}
}
public static class Location{
private Double lat;
private Double lon;
public Location() {
}
public Location(Double lat, Double lon) {
this.lat = lat;
this.lon = lon;
}
public Double getLat() {
return lat;
}
public void setLat(Double lat) {
this.lat = lat;
}
public Double getLon() {
return lon;
}
public void setLon(Double lon) {
this.lon = lon;
}
@JsonIgnore
public String getGeohash(){
return new org.elasticsearch.common.geo.GeoPoint(this.lat, this.lon).getGeohash();
}
}
}
它的映射。
{
"location": {
"properties": {
"id": {"type": "long"},
"genre": {"type": "integer"},
"cityName": {"type": "string"},
"location": {
"type": "geo_point",
"geohash": false
},
"geoPoint": {
"type": "geo_point",
"geohash": true,
"geohash_prefix": true,
"geohash_precision": 7
},
"lastUpdate": {"type": "date", format: "yyyy/MM/dd HH:mm:ss"}
}
}
}
搜索。
按距离(工作正常)。
获取/user/location/_search
{
"query": {
"match_all": {}
},
"filter": {
"geo_distance": {
"distance": "100km",
"location": {
"lat": 42.5,
"lon": -0.49
}
}
}
}
通过 geohash(也可以正常工作,超过 7 的精度将被忽略,因为它是用 "geohash_precision: 7" 映射的)。
获取/user/location/_search
{
"query": {
"filtered": {
"filter": {
"geohash_cell": {
"geoPoint": {
"geohash": "ezrm5c0y5rh8"
},
"neighbors": true,
"precision": 7
}
}
}
}
}
结论
我不明白为什么org.elasticsearch.common.geo.GeoHashUtils.GeoPoint class 与实际Elastic 版本不兼容。
但是,按照@jkbkot 提供的轨道,我决定实施我自己的 GeoPoint 和 Location classes 以获得完全兼容性。
以下 Sense script 应该会让您知道该怎么做:
DELETE location
PUT location
PUT location/location/_mapping
{
"location": {
"properties": {
"id": {"type": "long"},
"genre": {"type": "integer"},
"cityName": {"type": "string"},
"geoPoint": {
"type": "geo_point",
"geohash": true,
"geohash_prefix": true,
"geohash_precision": 7
},
"lastUpdate": {"type": "date", format: "yyyy/MM/dd HH:mm:ss"}
}
}
}
GET location/location/_mapping
PUT location/location/1
{"id":40,"genre":1,"cityName":"Madrid","geoPoint":{"geohash":"ezrm5c0vx832"},"lastUpdate":1402144560000}
GET /location/location/_search
{
"query" : {
"match_all": {}
},
"filter" : {
"geo_distance" : {
"distance" : "40km",
"geoPoint" : {
"lat" : 42.5,
"lon" : -0.49
}
}
}
}
您必须将保存文档的 Java class 转换为 JSON,其结构如下:
{
"id": 40,
"genre": 1,
"cityName": "Madrid",
"geoPoint": {
"geohash": "ezrm5c0vx832"
},
"lastUpdate": 1402144560000
}
或
{
"id": 40,
"genre": 1,
"cityName": "Madrid",
"geoPoint": {
"lat": 42.626595,
"lon": -0.488439
},
"lastUpdate": 1402144560000
}
所以要么你必须删除,例如private GeoPoint geoPoint;
来自你的 Java class 并把 lat
和 lon
留在那里(或者反过来),或者你必须改变你如何序列化 class 到 JSON 字符串 - 省略 geoPoint 或同时省略纬度和经度。