Elasticsearch Ruby gem不会返回正确的数据,而普通的HTTP GET会

时间:2017-02-19 20:05:49

标签: ruby bash shell elasticsearch elasticsearch-geo-shape

我试图找出一个多边形是否包含使用Elasticsearch的地理点。 当我使用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]
                }
            }
        }]
    }
}

0 个答案:

没有答案