3
votes

I have an spring boot application for use elasticsearch geopoint. When i save an elastic index and create a geoDistanceQuery i getted QueryShardException[failed to find geo_point field [customer]] exception.

My document;

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;

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

Save and get methods;

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 Method;

@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());
}

Exception Trace;

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)

My Elastic version; 7.5.1 Spring data elastic version;

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

When i get http://localhost:9200/customer/_mapping url;

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

How can i fix this issue?

2
Try to use geoPoint attribute here instead of customer : QueryBuilders.geoDistanceQuery("geoPoint")baitmbarek

2 Answers

1
votes

The geopoint query must be built with the field name:

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

BTW, in case your property is of type GeoPoint you do not need the @GeoPointField annotation, it is mapped to geo_point if either the class is GeoPoint or the annotation is present.

-1
votes

Your point coordinates, latitude and longitude, are not in km, where as you testing the distance in km. The distance between the given points is not ~0.142 as you might expect, but 14.71 km. You specified distance of 10 km. That's why the search correctly fails to find that point.

Details:

The North-South distance between (29.976, 31.131) and (29.876, 31.131) is 11.12 km (not 0.1 km).

The West-East distance between (29.876, 31.131) and (29.876, 31.231) is 9.64 km (not 0.1 km).

The distance between (29.976, 31.131) and (29.876, 31.231) is 14.71 km.