ElasticSearch(Java) - searching with the help of location(GeoPoint


(Nikita Krasnov) #1

Hello to everyone.
I try to search by geoShapeQuery:

 @PostMapping("/findLogsByValues")
    @Transactional
    public ResponseEntity findLogsByValues(@RequestBody ElasticSearchLogRequest esLogRequest) {
if (esLogRequest.getLocation() != null) {
            try {
                bqb.filter(QueryBuilders.geoShapeQuery("location", ShapeBuilders.
                        newMultiPoint(new CoordinatesBuilder().
                        coordinate(esLogRequest.getLocation().getLat(), esLogRequest.getLocation().getLon()).build())).
                        relation(ShapeRelation.INTERSECTS));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
 sourceBuilder.query(bqb);
searchRequest.source(sourceBuilder);
        SearchResponse searchResponse = null;
        try {
            searchResponse = client.search(searchRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
}

For request I use JSON:
{
"location":{
"lat":50.4501,
"lon":30.5234
}
}

And get such error:
in the comments

This is how I saved GeoPoint to ES:

try {
            XContentBuilder builder = XContentFactory.jsonBuilder();
            builder.startObject();
            {
                builder.field("location", elasticSearchLogs.getLocation());   
            }
            builder.endObject();
}

where getLocation returns GeoPoint (field GeoPoint location)
When I look through Kibana on saved longitute and latitude I see:


What should I do for searching with the help of location(GeoPoint)?


(Nikita Krasnov) #2
2018-01-24 14:30:52.405 ERROR 12932 --- [nio-8082-exec-3] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is ElasticsearchStatusException[Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]]] with root cause

    org.elasticsearch.ElasticsearchStatusException: Elasticsearch exception [type=search_phase_execution_exception, reason=all shards failed]
    	at org.elasticsearch.rest.BytesRestResponse.errorFromXContent(BytesRestResponse.java:177) ~[elasticsearch-6.1.1.jar:6.1.1]
    	at org.elasticsearch.client.RestHighLevelClient.parseEntity(RestHighLevelClient.java:573) ~[elasticsearch-rest-high-level-client-6.1.1.jar:6.1.1]
    	at org.elasticsearch.client.RestHighLevelClient.parseResponseException(RestHighLevelClient.java:549) ~[elasticsearch-rest-high-level-client-6.1.1.jar:6.1.1]
    	at org.elasticsearch.client.RestHighLevelClient.performRequest(RestHighLevelClient.java:456) ~[elasticsearch-rest-high-level-client-6.1.1.jar:6.1.1]
    	at org.elasticsearch.client.RestHighLevelClient.performRequestAndParseEntity(RestHighLevelClient.java:429) ~[elasticsearch-rest-high-level-client-6.1.1.jar:6.1.1]
    	at org.elasticsearch.client.RestHighLevelClient.search(RestHighLevelClient.java:368) ~[elasticsearch-rest-high-level-client-6.1.1.jar:6.1.1]
    	at com.datastech.admin.resource.ElasticSearchResource.findLogsByValues(ElasticSearchResource.java:298) ~[classes/:na]
    	at com.datastech.admin.resource.ElasticSearchResource$$FastClassBySpringCGLIB$$25624506.invoke(<generated>) ~[classes/:na]
    	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:738) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:282) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) ~[spring-tx-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:673) ~[spring-aop-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at com.datastech.admin.resource.ElasticSearchResource$$EnhancerBySpringCGLIB$$e8644b8a.findLogsByValues(<generated>) ~[classes/:na]
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_152]
    	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_152]
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_152]
    	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_152]
    	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133) ~[spring-web-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:967) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:901) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]
    	at javax.servlet.http.HttpServlet.service(HttpServlet.java:661) ~[tomcat-embed-core-8.5.23.jar:8.5.23]
    	at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846) ~[spring-webmvc-4.3.13.RELEASE.jar:4.3.13.RELEASE]

(Nikita Krasnov) #3
Suppressed: org.elasticsearch.client.ResponseException: method [GET], host [http://192.168.3.43:9200], URI [/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"mXFRna9FRm2_1p9mzT4LeA","index":".kibana"},{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"QutuP8lFSFSBh50Xgajv9Q","index":"portal-logs-24.01.2018"},{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"x7dXXCrXTnCWBgbgit7O_Q","index":"portal-metrics-19.01.2018"},{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"_snEJtiFRf27yco3Gs1Tyw","index":"portal-metrics-22.01.2018"},{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"8x-yE_DgRuKkJVykR0ElPA","index":"portal-metrics-23.01.2018"},{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"-I_f7jUjRjiHK2h8ye-g6w","index":"portal-user_info-19.01.2018"},{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"nY7I_dOTSN-Edofxmk_P3Q","index":"portal-user_info-22.01.2018"}],"type":"search_phase_execution_exception","reason":"all shards failed","phase":"query","grouped":true,"failed_shards":[{"shard":0,"index":".kibana","node":"SAPKQLGGSJiR8bMBdkNvfQ","reason":{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"mXFRna9FRm2_1p9mzT4LeA","index":".kibana"}},{"shard":0,"index":"portal-logs-24.01.2018","node":"JEwfXRPHQSWjPn4L4U6AVQ","reason":{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"QutuP8lFSFSBh50Xgajv9Q","index":"portal-logs-24.01.2018"}},{"shard":0,"index":"portal-metrics-19.01.2018","node":"SAPKQLGGSJiR8bMBdkNvfQ","reason":{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"x7dXXCrXTnCWBgbgit7O_Q","index":"portal-metrics-19.01.2018"}},{"shard":0,"index":"portal-metrics-22.01.2018","node":"JEwfXRPHQSWjPn4L4U6AVQ","reason":{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"_snEJtiFRf27yco3Gs1Tyw","index":"portal-metrics-22.01.2018"}},{"shard":0,"index":"portal-metrics-23.01.2018","node":"JEwfXRPHQSWjPn4L4U6AVQ","reason":{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"8x-yE_DgRuKkJVykR0ElPA","index":"portal-metrics-23.01.2018"}},{"shard":0,"index":"portal-user_info-19.01.2018","node":"JEwfXRPHQSWjPn4L4U6AVQ","reason":{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"-I_f7jUjRjiHK2h8ye-g6w","index":"portal-user_info-19.01.2018"}},{"shard":0,"index":"portal-user_info-22.01.2018","node":"JEwfXRPHQSWjPn4L4U6AVQ","reason":{"type":"query_shard_exception","reason":"failed to find geo_shape field [location]","index_uuid":"nY7I_dOTSN-Edofxmk_P3Q","index":"portal-user_info-22.01.2018"}}]},"status":400}
		at org.elasticsearch.client.RestClient$1.completed(RestClient.java:357) ~[elasticsearch-rest-client-6.1.1.jar:6.1.1]
		at org.elasticsearch.client.RestClient$1.completed(RestClient.java:346) ~[elasticsearch-rest-client-6.1.1.jar:6.1.1]
		at org.apache.http.concurrent.BasicFuture.completed(BasicFuture.java:123) ~[httpcore-4.4.8.jar:4.4.8]
		at org.apache.http.impl.nio.client.DefaultClientExchangeHandlerImpl.responseCompleted(DefaultClientExchangeHandlerImpl.java:181) ~[httpasyncclient-4.1.3.jar:4.1.3]
		at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.processResponse(HttpAsyncRequestExecutor.java:436) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.nio.protocol.HttpAsyncRequestExecutor.inputReady(HttpAsyncRequestExecutor.java:326) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.impl.nio.DefaultNHttpClientConnection.consumeInput(DefaultNHttpClientConnection.java:265) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:81) ~[httpasyncclient-4.1.3.jar:4.1.3]
		at org.apache.http.impl.nio.client.InternalIODispatch.onInputReady(InternalIODispatch.java:39) ~[httpasyncclient-4.1.3.jar:4.1.3]
		at org.apache.http.impl.nio.reactor.AbstractIODispatch.inputReady(AbstractIODispatch.java:114) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.impl.nio.reactor.BaseIOReactor.readable(BaseIOReactor.java:162) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvent(AbstractIOReactor.java:337) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.impl.nio.reactor.AbstractIOReactor.processEvents(AbstractIOReactor.java:315) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.impl.nio.reactor.AbstractIOReactor.execute(AbstractIOReactor.java:276) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.impl.nio.reactor.BaseIOReactor.execute(BaseIOReactor.java:104) ~[httpcore-nio-4.4.5.jar:4.4.5]
		at org.apache.http.impl.nio.reactor.AbstractMultiworkerIOReactor$Worker.run(AbstractMultiworkerIOReactor.java:588) ~[httpcore-nio-4.4.5.jar:4.4.5]
		... 1 common frames omitted

(David Pilato) #4

geoShape Query works on shapes AFAIK https://www.elastic.co/guide/en/elasticsearch/reference/6.1/geo-shape.html


(Nikita Krasnov) #5

As I understant for GeoPoint I have to use
QueryBuilders.geoBoundingBoxQuery(), but I haven't find example of implementation.
Can you give an example?


(Nikita Krasnov) #6

What type of QueryBuilders I have to use for searching only with one location parameters(lat, lon) of type GeoPoint ?


(David Pilato) #7

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-query-builders.html#_geo_queries

geoBoundingBoxQuery
is one of them indeed: https://artifacts.elastic.co/javadoc/org/elasticsearch/elasticsearch/6.1.2/org/elasticsearch/index/query/QueryBuilders.html#geoBoundingBoxQuery-java.lang.String-

An example of this: https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-geo-queries.html#java-query-dsl-geo-bounding-box-query

geoBoundingBoxQuery("pin.location")                          
    .setCorners(40.73, -74.1,                                
                40.717, -73.99);                             

(Nikita Krasnov) #8

But, if I need to search for logs having only one GeoPoint... In geoBoundingBoxQuery, I have to put 2 points. If I use the same coordinates for 2 points, I get an error: "top cannot be the same as bottom: "
How I can search having only one GeoPoint?


(David Pilato) #9

If you scroll the documentation a bit, you'll see:

https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/java-geo-queries.html#java-query-dsl-geo-distance-query


(Nikita Krasnov) #10

Ok, I think method geoDistanceQuery will work
But right know (when I using this method) I get an error when try to find by geo location:

Suppressed: org.elasticsearch.client.ResponseException: method [GET], host [http://192.168.3.43:9200], URI [/_search?typed_keys=true&ignore_unavailable=false&expand_wildcards=open&allow_no_indices=true&search_type=query_then_fetch&batched_reduce_size=512], status line [HTTP/1.1 400 Bad Request]
{"error":{"root_cause":[{"type":"query_shard_exception","reason":"failed to find geo_point field [location]","index_uuid":"mXFRna9FRm2_1p9mzT4LeA","index":".kibana"},{"type":"query_shard_exception","reason":"failed to find geo_point field [location]","index_uuid":"X-M1F-ZbT-SvWK8Bhzw3UQ","index":"portal-logs-24.01.2018"},{"type":"query_shard_exception","reason":"failed to find geo_point field [location]","index_uuid":"8B7M5hXLQt6WArUk1YzRug","index":"portal-logs-25.01.2018"},

And I think, it's due to incorrect saving of GeoPoint. I've added the print screen of kibana behind. It's how my GeoPoint is saved. And I have added the code also.
What I am doing wrong during saving process?


(David Pilato) #11

Yeah probably location is not a geo_point in your mapping.
Check your mapping with GET indexname/_mapping


(Nikita Krasnov) #12

Result is

 "location": {
            "properties": {
              "lat": {
                "type": "float"
              },
              "lon": {
                "type": "float"
              }
            }
          }

But, how properly save GeoPoint? O maybe my json for saving is incorrect...
For saving I used two types of jsons and both save incorrect.

{
location": "13.500544, 52.530286"
}

{
"location":{
"lat":13.400544,
"lon":52.530286
}
}

(David Pilato) #13

That's what I said. Your mapping is incorrect. Fix your mapping first.

So:

DELETE the index
PUT the new index with a correct mapping
Index your documents


(system) #14

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.