我有一个使用弹性搜索地理空间的应用程序。我正在使用elasticsearchrest客户端来索引和搜索文档。当我把一个geo_point字段索引为elastic时,我无法搜索它们。我遇到了一个例外。
我的对象类;
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
}
}
}
}
}
}
}
地理点字段不是地理点类型。当我搜索半径为直线的位置时;
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]
映射没有使用geo_point
映射指定GeoPoint
字段。
按以下方式调整
...
{
"geoPoint":{
"properties":{
...
"lat":{
"type":"float"
},
"lon":{
"type":"float"
},
"as_geopoint": { <-----
"type": "geo_point"
}
}
}
}
...
然后准备您的文档,使其看起来像:
{
...
"geoPoint": {
...
"as_geopoint": {
"lat": 41.12,
"lon": -71.34
}
}
}
然后查询CCD_ 3字段。
你必须先索引你的地理数据,然后才能对其进行地理查询!