Spring Data Elasticsearch无法找到geo_point字段

问题描述 投票:2回答:1

我有一个用于使用Elasticsearch Geopoint的Spring Boot应用程序。当我保存弹性索引并创建geoDistanceQuery时,我得到了QueryShardException [未能找到geo_point字段[客户]]异常。

我的文档;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.GeoPointField;
import org.springframework.data.elasticsearch.core.geo.GeoPoint;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Getter
@Setter
@Document(indexName = "customer", replicas = 0, refreshInterval = "-1")
public class Customer {

    @GeneratedValue(strategy= GenerationType.AUTO)
    @Id
    private String id;

    private Integer cifNo;

    private String userId;

    private String name;

    @GeoPointField
    private GeoPoint geoPoint;

}

存储库;

@Repository
public interface CustomerRepository extends ElasticsearchRepository<Customer, String> { }

保存和获取方法;

import com.system.management.domain.entity.Customer;
import com.system.management.repository.CustomerRepository;
import org.elasticsearch.common.unit.DistanceUnit;
import org.elasticsearch.index.query.GeoDistanceQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.data.elasticsearch.core.query.SearchQuery;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class LocationFinder {

    @Autowired
    public ElasticsearchOperations elasticsearchTemplate;

    @Autowired
    public CustomerRepository customerRepository;

    public void saveNewLocation(Customer customer) {
        customerRepository.save(customer);
    }

    public List<Customer> getLocationMembers(){
        GeoDistanceQueryBuilder geoDistanceQueryBuilder = QueryBuilders
                .geoDistanceQuery("customer")
                .point(29.976, 31.131)
                .distance(10, DistanceUnit.KILOMETERS);

        SearchQuery searchQuery = new NativeSearchQueryBuilder()
                .withFilter(geoDistanceQueryBuilder)
                .build();
        return elasticsearchTemplate.queryForList(searchQuery,Customer.class);
    }
}

测试方法;

@Test
public void testLocation() {
    Customer customer = new Customer();
    customer.setName("base");
    customer.setCifNo(1242343);
    customer.setGeoPoint(new GeoPoint(29.876, 31.231));

    locationFinder.saveNewLocation(customer);
    List<Customer> customerList = locationFinder.getLocationMembers();
    Assert.assertNotEquals(0,customerList.size());
}

异常跟踪;

Failed to execute phase [dfs], all shards failed
; shardFailures {[v4sPjgozTueAfeXoU4Ua5w][customer][0]: RemoteTransportException[[mertaksu-MBP][127.0.0.1:9300][indices:data/read/search[phase/dfs]]]; nested: QueryShardException[failed to find geo_point field [customer]]; }{[v4sPjgozTueAfeXoU4Ua5w][customer][1]: RemoteTransportException[[mertaksu-MBP][127.0.0.1:9300][indices:data/read/search[phase/dfs]]]; nested: QueryShardException[failed to find geo_point field [customer]]; }{[v4sPjgozTueAfeXoU4Ua5w][customer][2]: RemoteTransportException[[mertaksu-MBP][127.0.0.1:9300][indices:data/read/search[phase/dfs]]]; nested: QueryShardException[failed to find geo_point field [customer]]; }{[v4sPjgozTueAfeXoU4Ua5w][customer][3]: RemoteTransportException[[mertaksu-MBP][127.0.0.1:9300][indices:data/read/search[phase/dfs]]]; nested: QueryShardException[failed to find geo_point field [customer]]; }{[v4sPjgozTueAfeXoU4Ua5w][customer][4]: RemoteTransportException[[mertaksu-MBP][127.0.0.1:9300][indices:data/read/search[phase/dfs]]]; nested: QueryShardException[failed to find geo_point field [customer]]; }
    at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:534)
    at org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:305)
    at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseDone(AbstractSearchAsyncAction.java:563)
    at org.elasticsearch.action.search.AbstractSearchAsyncAction.onShardFailure(AbstractSearchAsyncAction.java:384)
    at org.elasticsearch.action.search.AbstractSearchAsyncAction.access$200(AbstractSearchAsyncAction.java:65)
    at org.elasticsearch.action.search.AbstractSearchAsyncAction$1.onFailure(AbstractSearchAsyncAction.java:241)
    at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:59)
    at org.elasticsearch.action.search.SearchTransportService$ConnectionCountingHandler.handleException(SearchTransportService.java:423)
    at org.elasticsearch.transport.TransportService$ContextRestoreResponseHandler.handleException(TransportService.java:1120)
    at org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:1229)
    at org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1203)
    at org.elasticsearch.transport.TaskTransportChannel.sendResponse(TaskTransportChannel.java:60)
    at org.elasticsearch.action.search.SearchTransportService$2.onFailure(SearchTransportService.java:323)
    at org.elasticsearch.action.ActionListener$1.onFailure(ActionListener.java:71)
    at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:65)
    at org.elasticsearch.action.ActionRunnable.lambda$supply$0(ActionRunnable.java:58)
    at org.elasticsearch.action.ActionRunnable$2.doRun(ActionRunnable.java:73)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
    at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:44)
    at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:773)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.lang.Thread.run(Thread.java:830)
Caused by: [customer/fSg6OP_fT0OIG4JAiUFhgA] QueryShardException[failed to find geo_point field [customer]]
    at org.elasticsearch.index.query.GeoDistanceQueryBuilder.doToQuery(GeoDistanceQueryBuilder.java:235)
    at org.elasticsearch.index.query.AbstractQueryBuilder.toQuery(AbstractQueryBuilder.java:99)
    at org.elasticsearch.index.query.QueryShardContext.lambda$toQuery$1(QueryShardContext.java:331)
    at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:343)
    at org.elasticsearch.index.query.QueryShardContext.toQuery(QueryShardContext.java:330)
    at org.elasticsearch.search.SearchService.parseSource(SearchService.java:749)
    at org.elasticsearch.search.SearchService.createContext(SearchService.java:586)
    at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:545)
    at org.elasticsearch.search.SearchService.executeDfsPhase(SearchService.java:309)
    at org.elasticsearch.search.SearchService.lambda$executeDfsPhase$0(SearchService.java:305)
    at org.elasticsearch.action.ActionListener.lambda$map$2(ActionListener.java:146)
    at org.elasticsearch.action.ActionListener$1.onResponse(ActionListener.java:63)

我的弹性版本; 7.5.1Spring数据弹性版本;

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-elasticsearch</artifactId>
    <version>3.2.3.RELEASE</version>
</dependency>

当我获得http://localhost:9200/customer/_mapping网址时;

{"customer":{"mappings":{"properties":{"cifNo":{"type":"long"},"geoPoint":{"type":"geo_point"},"name":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}}}

我该如何解决此问题?

java spring elasticsearch spring-data-elasticsearch
1个回答
0
投票

您的点坐标,纬度和经度不在km中,而您在其中以km测试距离。给定点之间的距离不是您所期望的〜0.142,而是14.71 km。您指定的距离为10公里。这就是为什么搜索正确地找不到该点的原因。

详细信息:

[(29.976,31.131)与(29.876,31.131)之间的东西距离是11.12 km(不是0.1 km)。

[29.876,31.131)和(29.876,31.231)之间的南北距离是9.64 km(不是0.1 km)。

((29.976,31.131)与(29.876,31.231)之间的距离是14.71公里。

© www.soinside.com 2019 - 2024. All rights reserved.