我用这个手动创建了这个映射:

$ curl -X PUT 'http://localhost:9200/location_index' -d '{
        "mappings" : {
            "nodes" : {
                "dynamic": true, 
                "properties" : {
                    "id" : {
                        "type" : "long"
                    },
                    "idx" : {
                        "type" : "string"
                    },
                    "idx2" : {
                        "type" : "string"
                    },
                    "location" : {
                        "type" : "geo_point"
                    },
                    "name" : {
                        "type" : "string"
                    },
                    "address_full" : {
                        "type" : "string"
                    },
                    "tags" : {
                        "properties" : {
                            "name" : {
                                "type" : "string"
                            },
                            "slug" : {
                                "type" : "string"
                            }
                        }
                    }
                }
            }
        }
    }'

我有这个查询:

{  
    "query":{  
        "filtered":{  
            "filter":{  
                "geo_distance":{  
                    "distance":"20km",
                    "location":{  
                        "lat":40.728199,
                        "lon":-74.077599
                    }
                }
            }
        }
    },
    "script_fields":{  
        "distance":{  
            "lang":"groovy",
            "params":{  
                "lat":40.728199,
                "lon":-74.077599
            },
            "script":"doc['location'].distanceInKm(lat,lon)"
        }
    },
    "sort":[  
        {  
            "_geo_distance":{  
                "location":{  
                    "lat":40.728199,
                    "lon":-74.077599
                },
                "order":"asc",
                "unit":"km",
                "mode":"min",
                "distance_type":"plane"
            }
        }
    ],
    "fields":[  
        "_source",
        "distance"
    ]
}

我的问题是我没有将结果中的距离作为一个字段 . 所以我也不知道结果是否按距离排序 .

有任何想法吗?它可能是一个阻止我做“动态”字段的设置吗?

如果是这样,我的 elasticsearch.yaml 是这样的:

node.name: "My First Node"
   cluster.name: mycluster1
   node.data: true
   index.number_of_shards: 1
   index.number_of_replicas: 0
   network.bind_host: localhost
   script.inline: on
   script.indexed: on