gpt4 book ai didi

spring-boot - Elasticsearch高级休息客户端无法映射geo_point

转载 作者:行者123 更新时间:2023-12-03 02:28:29 26 4
gpt4 key购买 nike

我有一个使用elasticsearch geospatial的应用程序。我正在使用Elasticsearch Rest Client索引和搜索文档。当我索引geo_point字段以 flex 时,我无法搜索它们。我有一个异常(exception)。

我的对象类;

import lombok.Data;
import org.elasticsearch.common.geo.GeoPoint;
import javax.persistence.Id;

@Data
public class CustomerLocation {
@Id
private String id;

private Integer cifNo;

private String userId;

private String status;

private String sessionId;

private Integer locationCount;

private String lastSeenDate;

private String firstSeenDate;

private GeoPoint geoPoint;
}

创建索引方法;
public void save(CustomerLocation customerLocation) throws IOException {
IndexRequest indexRequest = new IndexRequest("customerlocation").id(customerLocation.getId()).source(objectMapper.writeValueAsString(customerLocation),XContentType.JSON);
client.index(indexRequest, RequestOptions.DEFAULT);
}

索引映射;
{
"customerlocation": {
"mappings": {
"properties": {
"cifNo": {
"type": "long"
},
"firstSeenDate": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"geoPoint": {
"properties": {
"fragment": {
"type": "boolean"
},
"geohash": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lat": {
"type": "float"
},
"lon": {
"type": "float"
}
}
},
"id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"lastSeenDate": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"locationCount": {
"type": "long"
},
"status": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}

而且geopoint字段不是geo_point类型。当我搜索半径沿线以下的位置时;
public List<CustomerLocation> findByCifNoAndUserIdWithinLocation(GenericRequest genericRequest, Integer radius) {

List<CustomerLocation> customerLocationList = new ArrayList<>();
try {
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
SearchRequest searchRequest = new SearchRequest("customerlocation");

QueryBuilder query;
if(genericRequest.getUserId()!=null) {
query = QueryBuilders.boolQuery().
filter(QueryBuilders.termQuery("cifNo",genericRequest.getClientNo())).
filter(QueryBuilders.termQuery("userId",genericRequest.getUserId()));
} else {
query = QueryBuilders.boolQuery().
filter(QueryBuilders.termQuery("cifNo",genericRequest.getClientNo()));
}
QueryBuilder geoDistanceQueryBuilder = QueryBuilders
.geoDistanceQuery("geoPoint")
.point(genericRequest.getLatitude(), genericRequest.getLongitude())
.distance(radius, DistanceUnit.KILOMETERS);

QueryBuilder finalQuery = QueryBuilders.boolQuery().must(query).filter(geoDistanceQueryBuilder);
sourceBuilder.query(finalQuery);
searchRequest.source(sourceBuilder);

SearchResponse searchResponse = client.search(searchRequest,RequestOptions.DEFAULT);
SearchHit[] hits = searchResponse.getHits().getHits();
for (SearchHit searchHit:hits) {
CustomerLocation customerLocation = objectMapper.readValue(searchHit.getSourceAsString(),CustomerLocation.class);
if(customerLocation.getStatus()!=null && !customerLocation.getStatus().equalsIgnoreCase("alert")) {
customerLocationList.add(customerLocation);
} else if (customerLocation.getStatus()==null) {
customerLocationList.add(customerLocation);
}
}
} catch (Exception e) {
log.error("[{}]Exception occured while getting customer location from elastic",genericRequest.getClientNo(),e);
}
return customerLocationList;
}

在上面的代码中运行时,我在异常下面得到提示。我怎么了
org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177) ~[elasticsearch-7.5.2.jar:7.5.2]
at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:1793) ~[elasticsearch-rest-high-level-client-7.5.2.jar:7.5.2]
at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:1770) ~[elasticsearch-rest-high-level-client-7.5.2.jar:7.5.2]
at org.elasticsearch.client.RestHighLevelClient.internalPerformRequest(RestHighLevelClient.java:1527) ~[elasticsearch-rest-high-level-client-7.5.2.jar:7.5.2]
at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:1484) ~[elasticsearch-rest-high-level-client-7.5.2.jar:7.5.2]
at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:1454) ~[elasticsearch-rest-high-level-client-7.5.2.jar:7.5.2]
at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:970) ~[elasticsearch-rest-high-level-client-7.5.2.jar:7.5.2]
at com.ykb.frd.fraudlocation.elastic.repository.CustomerLocationRepositoryImpl.findByCifNoAndUserIdWithinLocation(CustomerLocationRepositoryImpl.java:61) ~[classes/:na]
at com.ykb.frd.fraudlocation.elastic.repository.CustomerLocationRepositoryImpl$$FastClassBySpringCGLIB$$8bf452.invoke(<generated>) [classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) [spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:769) [spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) [spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) [spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) [spring-tx-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) [spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:747) [spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:689) [spring-aop-5.2.1.RELEASE.jar:5.2.1.RELEASE]
at com.ykb.frd.fraudlocation.elastic.repository.CustomerLocationRepositoryImpl$$EnhancerBySpringCGLIB$$ad3f68fd.findByCifNoAndUserIdWithinLocation(<generated>) [classes/:na]
at com.ykb.frd.fraudlocation.elastic.ElasticServiceImpl.getCustomersLocationsIndexWithinLocation(ElasticServiceImpl.java:30) [classes/:na]
at com.ykb.frd.fraudlocation.ElasticTest.elasticDataLocationFoundTest(ElasticTest.java:44) [test-classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_65]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_65]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_65]
at java.lang.reflect.Method.invoke(Method.java:497) ~[na:1.8.0_65]
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) [junit-4.12.jar:4.12]
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) [junit-4.12.jar:4.12]
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) [junit-4.12.jar:4.12]
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) [junit-4.12.jar:4.12]

最佳答案

您的映射未使用 GeoPoint mapping指定geo_point字段。

按以下方式调整

...
{
"geoPoint":{
"properties":{

...

"lat":{
"type":"float"
},
"lon":{
"type":"float"
},

"as_geopoint": { <-----
"type": "geo_point"
}
}
}
}
...

然后准备您的文档,使其看起来像:
{
...
"geoPoint": {
...
"as_geopoint": {
"lat": 41.12,
"lon": -71.34
}
}
}

然后查询 geoPoint.as_geopoint字段。

您必须先索引地理数据,然后才能对其进行地理查询!

关于spring-boot - Elasticsearch高级休息客户端无法映射geo_point,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60509768/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com