1

我正在尝试使用 Elasticsearch 确定多边形是否包含地理点。当我使用弹性搜索gem 时,它的客户说没有匹配。但是当我发出一个简单的 HTTP GET 请求时,它会返回一个匹配项。

这是我的红宝石代码

require 'elasticsearch'

# clear elasticsearch index
system "curl -i -XDELETE 'http://localhost:9200/items'"

# initialize elasticsearch mapping
system 'sh shell/init_mappings.sh'

# !!! be aware of elasticsearch's latlng inverted format

client = Elasticsearch::Client.new

client.index(index: :items, type: :item, body: {
  title: 'Svobody Square',
  location: {
    type: 'point',
    coordinates: [ 36.229150, 50.005802 ]
  }
})

client.index(index: :items, type: :item, body: {
  title: 'Nauky Ave',
  location: {
    type: 'point',
    coordinates: [ 36.227069, 50.011449 ]
  }
})

search_response = client.search(index: :items, type: :item, body: {
  'query': {
    'geo_shape': {
      'location': {
        'relation': 'within',
        'shape': {
          'type': 'polygon',
          'coordinates': [[
            [ 36.225013732910156, 50.00709085651248 ],
            [ 36.224777698516846, 50.00386372640019 ],
            [ 36.23394012451172,  50.00378097662527 ],
            [ 36.233811378479004, 50.0070632751218  ],
            [ 36.225013732910156, 50.00709085651248 ]
          ]]
        }
      }
    }
  }
})

p search_response

输出:

{ 
  "took"=>2,
  "timed_out"=>false,
  "_shards"=>{"total"=>5, "successful"=>5, "failed"=>0},
  "hits"=>{"total"=>0, "max_score"=>nil, "hits"=>[]} 
}

HTTP GET 请求:

curl -XGET 'http://localhost:9200/items/item/_search' -d '{
  "query": {
    "geo_shape": {
      "location": {
        "relation": "within",
        "shape": {
          "type": "polygon",
          "coordinates": [[
            [ 36.225013732910156, 50.00709085651248 ],
            [ 36.224777698516846, 50.00386372640019 ],
            [ 36.23394012451172,  50.00378097662527 ],
            [ 36.233811378479004, 50.0070632751218  ],
            [ 36.225013732910156, 50.00709085651248 ]
          ]]
        }
      }
    }
  }
}'

它确实返回了正确的数据:

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 1,
        "max_score": 1.0,
        "hits": [{
            "_index": "items",
            "_type": "item",
            "_id": "AVpX6vE90b9UPRnO4ECB",
            "_score": 1.0,
            "_source": {
                "title": "Svobody Square",
                "location": {
                    "type": "point",
                    "coordinates": [36.22915, 50.005802]
                }
            }
        }]
    }
}
4

0 回答 0