0

Suppose I have the following mapping:

  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "location": {
          "type": "nested",
          "properties": {
            "point": {
              "type": "geo_shape"
            }
          }
        }
      }
    }
  }
}

There is one document in the index:

POST /example/doc?refresh
{
    "name": "Wind & Wetter, Berlin, Germany",
    "location": {            
        "type": "point",
        "coordinates": [13.400544, 52.530286]
    }
}

How can I make a nested geo-shape query? Example of usual geo-shape query from the documentation (the "bool" block can be skipped):

 {
        "query":{
            "bool": {
                "must": {
                    "match_all": {}
                },
                "filter": {
                    "geo_shape": {
                        "location": {
                            "shape": {
                                "type": "envelope",
                                "coordinates" : [[13.0, 53.0], [14.0, 52.0]]
                            },
                            "relation": "within"
                        }
                    }
                }
            }
        }
    }

Example of a nested query is:

{
    "query": {
        "nested" : {
            "path" : "obj1",
            "score_mode" : "avg",
            "query" : {
                "bool" : {
                    "must" : [
                    { "match" : {"obj1.name" : "blue"} },
                    { "range" : {"obj1.count" : {"gt" : 5}} }
                    ]
                }
            }
        }
    }
}

Now how to combine them? In the documentation it is mentioned that nested filter has been replaced by nested query. And that it behaves as a query in “query context” and as a filter in “filter context”.

If I try query for intersect with the point:

    {
  "query": {
    "nested": {
      "path": "location",
      "query": {
        "geo_shape": {
          "location.point": {
            "shape": {
              "type": "point",
              "coordinates": [
                13.400544,
                52.530286
              ]
            },
            "relation": "disjoint"
          }
        }
      }
    }
  }
}

I still get back the document even if relation is "disjoint", so it's not correct. I tried different combinations, with "bool" and "filter", etc. but query is ignored, returning the whole index. Maybe it's impossible with this type of mapping?

Clearly I am missing something here. Can somebody help me out with that, please? Any help is greatly appreciated.

jinlin
  • 1
  • 1
  • The mapping of your location field is wrong. You don't need `nested` in order to have a `geo_shape`, `location` should simply be a `geo_shape`. – Val Aug 28 '18 at 02:49
  • I know that, but in my case I am trying to figure out how to use it as a nested field because that is how it is going to be mapped. I try to index a Django model which has a foreign key relation to another model contaning GeoJson location field. – jinlin Aug 28 '18 at 07:07
  • Ok, fair enough, but then your document is not properly formed, it should be `{"location": { "point": {"type": "point", "coordinates": [13.400544, 52.530286] }}` – Val Aug 28 '18 at 07:12
  • I have changed the mapping, but unfortunately the disjoint query still returns the document. Do you think there is something off with the query itself? – jinlin Aug 28 '18 at 08:24
  • Please provide a gist with the latest commands so we can recreate your issue. – Val Aug 28 '18 at 08:28
  • Turned out that Kibana does not display the correct result, because it is actually working with curl. Stupid of me not to try it before, and thank you so much for you help. – jinlin Aug 28 '18 at 09:12

0 Answers0