Elasticsearch GeoShape查询的意外行为

时间:2016-06-01 16:11:43

标签: elasticsearch geospatial

使用Elasticsearch 2.3.1和python客户端我通过地理搜索得到了意想不到的结果。在下面的代码中,我创建了一个简单的索引。然后我索引两个形状 - 一个大正方形和一个完全包含在该正方形中的小正方形(look at them here)。然后,我使用所有可用的策略{'intersects','disjoint','within','contains'}使用小方块查询索引。

from elasticsearch import Elasticsearch
es_client = Elasticsearch()

# CREATE INDEX
INDEX = "geo_shapes_2"
DOC_TYPE = "metros"
es_client.indices.delete(INDEX, ignore=404)
body = {
    "mappings": {
        "metro": {
            "properties": {
                "id": {
                  "type":  "string", 
                  "index": "not_analyzed"
                },
                "geometry": {
                    "type": "geo_shape",
                    "tree": "quadtree",
                    "precision": "50m",
                    "distance_error_pct": 0.025
                }
            }
        }
    }
}
es_client.indices.create(INDEX, body)

# INDEX TWO DOCS
# small_square is completely contained within big_square
small_square = 'small square'
body = {
    'id': small_square,
    'geometry': {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -106.248779296875,
              35.12889434101051
            ],
            [
              -106.34765625,
              35.88014896488361
            ],
            [
              -105.062255859375,
              35.8356283888737
            ],
            [
              -105.13916015625,
              35.05698043137265
            ],
            [
              -106.248779296875,
              35.12889434101051
            ]
          ]
        ]
      }
}
es_client.create(INDEX, DOC_TYPE, body=body, id=small_square)

big_square = 'big square'
body = {
    'id': big_square,
    'geometry': {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -108.97338867187499,
              32.93492866908233
            ],
            [
              -108.69873046875,
              38.19502155795573
            ],
            [
              -102.095947265625,
              38.12591462924157
            ],
            [
              -102.39257812499999,
              32.87036022808355
            ],
            [
              -108.97338867187499,
              32.93492866908233
            ]
          ]
        ]
      }
}
es_client.create(INDEX, DOC_TYPE, body=body, id=big_square)

# ISSUE ALL 4 STRATEGY QUERIES FOR SMALL SQUARE
body = {
    'filter': {
        'geo_shape': {
            'geometry': {
                'indexed_shape': {
                    'id': small_square,
                    'type': DOC_TYPE,
                    'index': INDEX,
                    'path': 'geometry',
                },
            }
        }
    }
}

for strategy in ['intersects', 'disjoint', 'within', 'contains']:
    body['filter']['geo_shape']['geometry']['relation'] = strategy
    response = es_client.search(INDEX, body=body)
    print strategy + ': ' + (', '.join([r['_source']['id'] for r in response['hits']['hits']]) or 'none')

我希望回复是:

intersects: big square, small square
disjoint: none
within: none (or maybe include small square depending upon the semantics of "within")
contains: big square, small square (or maybe omit small square depending upon the semantics of "contains")

相反,我看到了:

intersects: none
disjoint: big square, small square
within: none

然后出现错误

...
/Library/Python/2.7/site-packages/elasticsearch/connection/base.pyc in _raise_error(self, status_code, raw_data)
    106             pass
    107 
--> 108         raise HTTP_EXCEPTIONS.get(status_code, TransportError)(status_code, error_message, additional_info)
    109 
    110 

RequestError: TransportError(400, u'search_phase_execution_exception', u'')

我做错了吗?或者这是一个错误吗?

1 个答案:

答案 0 :(得分:0)

主要想出来。上面的代码在映射中有一个拼写错误,其中doc类型是'metro',但在其他地方我使用'metros'的DOC_TYPE。

contains仍然会返回错误。

相关问题