java - Spring Data Elasticsearch 找不到 geo_point 字段

标签 java spring elasticsearch spring-data-elasticsearch

我有一个用于使用elasticsearch geopoint 的Spring Boot 应用程序。当我保存弹性索引并创建 geoDistanceQuery 时,我收到 QueryShardException[failed to find geo_point field [customer]] 异常。

我的文档;

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)

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

<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}}}}}}}

我该如何解决这个问题?

最佳答案

必须使用字段名称构建地理点查询:

GeoDistanceQueryBuilder geoDistanceQueryBuilder = QueryBuilders
            .geoDistanceQuery("geoPoint")
            .point(29.976, 31.131)
            .distance(10, DistanceUnit.KILOMETERS);

顺便说一句,如果您的属性是 GeoPoint 类型,则不需要 @GeoPointField 注释,它会映射到 geo_point(如果有)该类是 GeoPoint 或存在注释。

关于java - Spring Data Elasticsearch 找不到 geo_point 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59619410/

相关文章:

java - 创建具有一组线程来处理请求的服务器的指南

java - 如何在 Java 中绘制交互式图形/线条?

java - 组织.hibernate.HibernateException : 'hibernate.dialect' must be set when no Connection available

java - 压缩大量pdf文件并下载 : OutOfMemoryError: Java heap space

java - 复制后隔离JAVA中的对象

c# - 如何更改剑道错误信息?

java - 如何从 ECPublicKey 中找到匹配的曲线名称

json - 在Elasticsearch中按脚本输出过滤

elasticsearch - ELK堆栈拉入旧日志

java - ElasticSearch 按 _source 字段搜索