2016-03-04 63 views
2

我已經創建和指數一樣的例子教程,在這裏...地理查詢中使用elasticsearch

https://www.elastic.co/guide/en/elasticsearch/reference/2.0/geo-point.html

在具體編寫以下:

curl -PUT 'localhost:9200/my_index?pretty' -d ' 
{ 
    "mappings": { 
    "my_type": { 
     "properties": { 
     "location": { 
      "type": "geo_point" 
     } 
     } 
    } 
    } 
}' 

我還添加兩點作爲數據

curl -PUT 'localhost:9200/my_index/my_type/1?pretty' -d' 
{ 
    "text": "first geo-point", 
    "location": { 
    "lat": 41.12, 
    "lon": -71.34 
    } 
}' 

curl -PUT 'localhost:9200/my_index/my_type/1?pretty' -d' 
{ 
    "text": "second geo-point", 
    "location": { 
    "lat": 41.13, 
    "lon": -71.35 
    } 
}' 

示例地理邊界框查詢t他的作品頁(即):

curl -XGET 'localhost:9200/my_index/_search?pretty' -d' 
{ 
    "query": { 
    "geo_bounding_box": { 
     "location": { 
     "top_left": { 
      "lat": 42, 
      "lon": -72 
     }, 
     "bottom_right": { 
      "lat": 40, 
      "lon": -74 
     } 
     } 
    } 
    } 
}' 

但是從這個頁面(https://www.elastic.co/guide/en/elasticsearch/reference/2.0/query-dsl-geo-bounding-box-query.html)的例子不工作:

我曾嘗試如下所示:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d' 
{ 
    "bool" : { 
     "must" : { 
      "match_all" : {} 
     }, 
     "filter" : { 
      "geo_bounding_box" : { 
       "my_type.location" : { 
        "top_left" : { 
         "lat" : 42, 
         "lon" : -72 
        }, 
        "bottom_right" : { 
         "lat" : 40, 
         "lon" : -74 
        } 
       } 
      } 
     } 
    } 
}' 

錯誤我得到如下:

"error" : { 
    "root_cause" : [ { 
     "type" : "search_parse_exception", 
     "reason" : "failed to parse search source. unknown search element [bool]", 
     "line" : 3, 
     "col" : 5 
    } ], 
    "type" : "search_phase_execution_exception", 
    "reason" : "all shards failed", 
    "phase" : "query", 
    "grouped" : true, 
    "failed_shards" : [ { 
     "shard" : 0, 
     "index" : "my_index", 
     "node" : "0qfvkynhTRyjHFRurBLJeQ", 
     "reason" : { 
     "type" : "search_parse_exception", 
     "reason" : "failed to parse search source. unknown search element [bool]", 
     "line" : 3, 
     "col" : 5 
     } 
    } ] 
    }, 
    "status" : 400 
} 

我希望它只是一個簡單的錯誤,所以想要kn我在做什麼錯?

回答

5

您需要指定,整個事情是一個查詢:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d' 
{ 
    "query": { 
     "bool" : { 
      "must" : { 
       "match_all" : {} 
      }, 
      "filter" : { 
       "geo_bounding_box" : { 
        "my_type.location" : { 
         "top_left" : { 
          "lat" : 42, 
          "lon" : -72 
         }, 
         "bottom_right" : { 
          "lat" : 40, 
          "lon" : -74 
         } 
        } 
       } 
      } 
     } 
    } 
}' 

但是據我瞭解使用布爾與必和過濾器是做事的老辦法。在以前的版本中,地理查詢被認爲是「過濾器」,因此您必須首先運行match_all查詢以返回所有結果,然後使用地理邊界框進行過濾。在Elasticssearch 2.0+中,過濾器和查詢之間沒有分離 - 一切都是查詢。因此,您可以直接運行地理查詢:

curl -XGET 'localhost:9200/my_index/_search?pretty' -d' 
{ 
    "query": { 
    "geo_bounding_box": { 
     "location": { 
     "top_left": { 
      "lat": 42, 
      "lon": -72 
     }, 
     "bottom_right": { 
      "lat": 40, 
      "lon": -74 
     } 
     } 
    } 
    } 
}' 
+0

謝謝!這工作! –