Geo_shape query not returning any results in java api

Hi guys,

i'm trying to find if a point lies within a certain geographic boundary with the java api.

i have a geo_shape mapping for a field called citybounds (text below copied from kibana):

{
  "mapping": {
    "properties": {
      "citybounds": {
        "type": "geo_shape"
      },
      "properties": {
        "properties": {
          "city": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "citybounds": {
            "properties": {
              "coordinates": {
                "type": "**float**"
              },
              "type": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
        }
      }
    }
  }
}

However i see another float type(marked in bold) for citybounds in the second properties. (i'm a bit confused and wonder whether it has anything to do with the problem).

i then indexed a document(as a multipolygon) and the type when i run a query comes out correct as a multipolygon. everything seems fine.

{"took":1,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"citybounds","_type":"_doc","_id":"Nigeria_FCT_Abuja","_score":1.0,"_source":{"properties":{"country":"Nigeria","state":"FCT","city":"Abuja","citybounds":{"type":"multipolygon","coordinates":[[[[7.4776235,8.6313334],[7.4430308,8.5932817],[7.435716,8.5864147],[7.4181271,8.5699024],[7.3959702,8.5384445],[7.3802833,8.5215208],[7.3653095,8.4876713],[7.3132577,8.4735664],[7.3008519,8.4709078],[7.2704753,8.464398],[7.2490148,8.4623215],[7.2048757,8.4580505],[7.1493607,8.4514032],[7.133419,8.4494944],[7.1396693,8.4431886],[7.1409542,8.4353364],[7.1241942,8.4227889],[7.0835435,8.4193362],[7.0863488,8.4326647],[7.0798582,8.4430811],[7.0693983,8.4418286],[7.0358854,8.4397127],[7.0058274,8.4419302],[6.985625,8.4434207],[6.9808149,8.4437277],[6.9774162,8.4439446],[6.9475626,8.4561211],[6.940822,8.4588704],[6.9211164,8.45864],[6.8872841,8.4582445],[6.8343902,8.457626],[6.8335376,8.4576161],[6.8278899,8.45755],[6.8275001,8.45755],[6.7982177,8.45755],[6.7845637,8.45755],[6.7825343,8.5715025],[6.7799621,8.5802781],[6.7800331,8.5864003],[6.7811685,8.6832125],[6.781214,8.6870932],[6.7812914,8.7439273],[6.7900634,8.7900562],[6.7922865,8.8402611],[6.7831129,8.8489431],[6.783784,8.9070302],[6.7837954,8.9071075],[6.7847829,8.9912615],[6.7850229,9.0117219],[6.7867074,9.1553354],[6.7870794,9.1870497],[6.7880545,9.2701664],[6.7982683,9.2701126],[6.8140392,9.2700296],[6.9537888,9.2692941],[6.9849551,9.26913],[7.0081494,9.269008],[7.0152669,9.2689705],[7.0300751,9.2577133],[7.0690054,9.2281172],[7.0921014,9.2105589],[7.1044265,9.2012497],[7.1263098,9.1844804],[7.1428308,9.1720875],[7.1538231,9.1636889],[7.1676583,9.1531181],[7.1716627,9.1501117],[7.1793362,9.1442459],[7.219758,9.1135101],[7.2205723,9.114656],[7.2210046,9.1152643],[7.2221416,9.116864],[7.2339809,9.1335232],[7.2497568,9.1557217],[7.2653787,9.1776339],[7.3157839,9.2483241],[7.3308203,9.2694118],[7.3317654,9.2710373],[7.3526252,9.3069137],[7.356728,9.3101169],[7.3590856,9.3090389],[7.3606035,9.3111675],[7.3786453,9.3364684],[7.3896907,9.335894],[7.4337634,9.34347],[7.4737342,9.3503409],[7.4969318,9.3524047],[7.509101,9.3550313],[7.520007,9.3665497],[7.5210707,9.3675515],[7.5420941,9.3873511],[7.6140604,9.4060588],[7.6469552,9.408685],[7.6674908,9.4013691],[7.6707232,9.3839228],[7.6787092,9.3683517],[7.69297,9.3494028],[7.7209212,9.3306405],[7.7240805,9.3292549],[7.6676059,9.3036509],[7.6400901,9.2427897],[7.6033192,9.1614408],[7.5886793,9.129056],[7.5891726,9.1009444],[7.5897953,9.0654622],[7.589917,9.0585222],[7.5901872,9.0304391],[7.5907078,9.013443],[7.5915916,8.9630501],[7.5917799,8.9523379],[7.5921838,8.9293081],[7.592267,8.9245644],[7.5946589,8.8564005],[7.5936126,8.8488678],[7.5928738,8.8435486],[7.5916691,8.8348751],[7.5653014,8.7852793],[7.5530788,8.7622894],[7.5414429,8.7404032],[7.5019798,8.6686516],[7.4960008,8.6543016],[7.4932284,8.6507368],[7.4849373,8.6400773],[7.4792585,8.6327763],[7.4776235,8.6313334]]]]}}}}.............

However, the main challenge is when i try to find if a point is disjoint or lies within or intersects the multipolygon. i get no results no matter which shape relation is used.

GeoShapeQueryBuilder qb;
        try {
            qb = QueryBuilders.geoShapeQuery(fieldname, new Point(latitude, longitude));
            qb.relation(ShapeRelation.WITHIN);
            SearchRequest searchRequest = new SearchRequest(index);
            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().from(0).size(resultcount);
            searchSourceBuilder.query(qb);
            searchRequest.source(searchSourceBuilder);
            restHighLevelClient.searchAsync(searchRequest, RequestOptions.DEFAULT, actionListener);

............................................

Would appreciate any guidance you can offer.

PS:
i also have the following dependencies as required.

 <dependency>
            <groupId>org.locationtech.spatial4j</groupId>
            <artifactId>spatial4j</artifactId>
            <version>0.7</version>                        
        </dependency>

        <dependency>
            <groupId>org.locationtech.jts</groupId>
            <artifactId>jts-core</artifactId>
            <version>1.15.0</version>                         
            <exclusions>
                <exclusion>
                    <groupId>xerces</groupId>
                    <artifactId>xercesImpl</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

Thanks

Your mapping and your document are out of sync, the "properties" element should not be in the document. My recommendation is to create again the index and set the dynamic mapping to false so you will know if you are doing something wrong.

hope it helps.

1 Like

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