0
votes

I am trying to query elastic search which has the following record:

{
  "_index": "my-index",
  "_type": "v1",
  "_id": "8758",
  "_source": {
    "id": "5855",
    "version": 3,
    "createdAt": "2020-10-30T04:45:32Z",
    "location": {
      "country": "CA",
      "provinceCode": "NA",
      "zipCode": "6069",
      "address1": "Some Address",
      "latLong": {
        "lon": 150.890033,
        "lat": -24.794214
      }
    }
  }
}

with the following curl request:

curl --location --request GET 'http://localhost:9200/my-index/_search' \
    --header 'Content-Type: application/json' \
    --data-raw '{
        "_source" : {
            "include": "*"
        },
        "sort": {
            "_script": {
                "type": "number",
                "script" : {
                    "lang": "painless",
                    "source": "Math.round((doc[params.field].planeDistanceWithDefault(params.lat, params.lon, 0) * 0.001) * 100.0) / 100.0",
                    "params" : {
                        "lat"  : -25.8152,
                        "lon" : 150.0912,
                        "field": "location"
                    }
                }
            }
        }
    }'

getting following error:

"caused_by": {
   "type": "illegal_argument_exception",
   "reason": "No field found for [location] in mapping with types []"
}

My questions are:

  1. Why is ES query not finding location field?
  2. Is there I need to change in my curl request to make it work?
1

1 Answers

0
votes

Fields of type geo_point accept latitude-longitude pairs. You need to use the following index mapping definition, to index the data.

{
  "mappings": {
    "properties": {
      "location": {
        "properties": {
          "latLong": {
            "type": "geo_point"
          }
        }
      }
    }
  }
}

And modify the search query as

{
  "_source": {
    "include": "*"
  },
  "sort": {
    "_script": {
      "type": "number",
      "script": {
        "lang": "painless",
        "source": "Math.round((doc[params.field].planeDistanceWithDefault(params.lat, params.lon, 0) * 0.001) * 100.0) / 100.0",
        "params": {
          "lat": -25.8152,
          "lon": 150.0912,
          "field": "location.latLong"         // note this
        }
      }
    }
  }
}