背景
用es數據庫來根據區域查詢數據,有根據圓形區域和矩形區域。
代碼
根據圓形區域範圍內的車輛信息。
@Data
@EqualsAndHashCode(callSuper = false)
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "EsRoundVo", description = "es根據圓形區域查詢車輛實時數據請求參數")
public class EsRoundVo extends EsIndexTypeVo{
@ApiModelProperty(name = "latitude", value = " 緯度", required = true)
@NotNull(message = "緯度不能爲空!")
@DoubleRange(min = -90D, max = 90D, message = "緯度值應在 -90 到 90 之間!")
private Double latitude;
@ApiModelProperty(name = "longitude", value = "經度", required = true)
@NotNull(message = " 經度不能爲空!")
@DoubleRange(min = -180D, max = 180D, message = "經度值應在 -180 到 180 之間!")
private Double longitude;
@ApiModelProperty(name = "radius", value = "半徑", required = true)
@NotNull(message = "半徑不能爲空")
@Min(value = 0, message = "半徑不能爲負!")
private Double radius;
@ApiModelProperty(name = "radiusUnit", value = "半徑單位,in,yd,ft,km,NM,mm,cm,mi,m", required = true)
@NotEmpty(message = "半徑單位不能爲空!")
@EnumValueScope(scope = AccessConstant.RadiusEnum.class, message = "半徑單位不在取值範圍內!")
private String radiusUnit;
@ApiModelProperty(name = "includes", value = "可選展示字段,不填默認全字段,所有es字段皆可選")
private String[] includes;
@ApiModelProperty(name = "offset", value = "偏移量,默認爲0")
private int offset = 0;
@ApiModelProperty(name = "pageSize", value = "每頁數量,默認爲10")
@Min(value = 1, message = "每頁數量最小爲1!")
@Max(value = 1000, message = "每頁數量最大爲1000!")
private int pageSize = 10;
}
public EsResultVo getCircle(EsRoundVo esRound){
try {
BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
queryBuilder.must(QueryBuilders.geoDistanceQuery(LOCATION.getValue())
.point(esRound.getLatitude(), esRound.getLongitude())
.distance(esRound.getRadius(), DistanceUnit.fromString(esRound.getRadiusUnit())));
SearchRequestBuilder requestBuilder = elasticsearchClient.prepareSearch(esRound.getIndex())
.setTypes(esRound.getType())
.setFetchSource(esRound.getIncludes(), null)
.setQuery(queryBuilder)
.setFrom(esRound.getOffset())
.setSize(esRound.getPageSize());
SearchHits searchHits = requestBuilder.get().getHits();
List<Object> res = Stream.of(searchHits.getHits()).map(SearchHit::getSource).collect(Collectors.toList());
return EsResultVo.format(res, esRound.getOffset(), esRound.getPageSize(), searchHits.getTotalHits());
}catch (Exception e){
log.error("es接口有誤",e);
throw IotServerException.create(THIRD_PARTY_API_REQUEST_ERROR, e.getMessage());
}
}
報錯
org.elasticsearch.action.search.SearchPhaseExecutionException: all shards failed
at org.elasticsearch.action.search.AbstractSearchAsyncAction.onFirstPhaseResult(AbstractSearchAsyncAction.java:208) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.action.search.AbstractSearchAsyncAction.access$100(AbstractSearchAsyncAction.java:52) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.action.search.AbstractSearchAsyncAction$1.onFailure(AbstractSearchAsyncAction.java:143) ~[na:na]
at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:51) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.transport.TransportService$ContextRestoreResponseHandler.handleException(TransportService.java:1024) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:1126) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1104) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.transport.TransportService$7.onFailure(TransportService.java:621) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.onFailure(ThreadContext.java:581) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:39) ~[elasticsearch-5.6.9.jar:5.6.9]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:na]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:na]
at java.lang.Thread.run(Thread.java:748) ~[na:na]
Caused by: org.elasticsearch.transport.RemoteTransportException: [edge5.hadoop.ds.com][192.168.1.179:9300][indices:data/read/search[phase/query]]
Caused by: org.elasticsearch.index.query.QueryShardException: failed to find geo_point field [point]
at org.elasticsearch.index.query.GeoDistanceQueryBuilder.doToQuery(GeoDistanceQueryBuilder.java:280) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:97) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.index.query.BoolQueryBuilder.addBooleanClauses(BoolQueryBuilder.java:442) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.index.query.BoolQueryBuilder.doToQuery(BoolQueryBuilder.java:416) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:97) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.index.query.QueryShardContext.lambda$toQuery$1(QueryShardContext.java:312) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:329) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:311) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:671) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.search.SearchService.createContext(SearchService.java:540) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:516) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:251) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.action.search.SearchTransportService$6.messageReceived(SearchTransportService.java:298) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.action.search.SearchTransportService$6.messageReceived(SearchTransportService.java:295) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.transport.RequestHandlerRegistry.processMessageReceived(RequestHandlerRegistry.java:69) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.transport.TransportService$7.doRun(TransportService.java:610) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:596) ~[elasticsearch-5.6.9.jar:5.6.9]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-5.6.9.jar:5.6.9]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:na]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:na]
at java.lang.Thread.run(Thread.java:748) ~[na:na]
關鍵信息就是point字段沒有被設定爲指定字段geo_point。
字段屬性
這個正常模版的index,我嘗試往裏面設置數據,但是界面無法顯示。
因爲一些原因,存儲到es的對象沒有用es jar包裏的GeoPoint對象,而是替換成了手寫的對象。
用這個對象存儲到數據庫的配置是
現在在讓同事把造一個對象存儲到模版index裏面。然後查看是否能查詢出來。
結論是:又一個es集羣,point字段被設置成了missing,這個還需要跟設置者溝通。