2015-10-14 37 views
2

似乎geo_point字段被設置在結果忽略單一的活動缺失geo_point字段

{ 
     "mygeo": { 
     "lat": 51.247607909, 
     "lon": 22.565701278 
     }, 
     "location" : "New York" 
} 

查詢

GET /test01/_search 
{ 
    "size" : 1, 
    "fields": ["location", "mygeo"] 
} 

在mygeo字段丟失時生成以下內容。 (我也嘗試過「字段」:[「location」,「mygeo.lat」,「mygeo.lon」,「mygeo」])。

"hits": [ 
    { 
     "_index": "test01", 
     "_type": "activity", 
     "_id": "1", 
     "_score": 1, 
     "fields": { 
      "location": [ 
       "New York" 
      ] 
     } 
    } 
    ] 

我可以得到mygeo對象的唯一方法是通過_source加入 「_source」:{ 「包括」:[ 「mygeo」]}。

有沒有辦法使用「field」參數獲取geo_point字段?

我已經嘗試了Rest API和Java API。兩者使用Elasticsearch v。1.7.1產生相同的結果。

感謝

回答

2

所以邏輯下面,我已經複製並通過添加"store": true到您的索引映射解決這個問題,現在讓我來檢索經度/緯度採用fields,而不是_source

請參閱複製上Sense做在我的本地:

DELETE test 

PUT /test 
{ 
    "mappings": { 
    "test1": { 
     "properties": { 
     "location": { 
      "type": "string" 
     }, 
     "mygeo": { 
      "type": "geo_point", 
      "doc_values": true, 
      "store": true, 
      "fielddata": { 
      "format": "compressed", 
      "precision": "1km" 
      } 
     } 
     } 
    } 
    } 
} 

POST /test/test1/ 
{ 
    "mygeo": { 
    "lat": 51.247607909, 
    "lon": 22.565701278 
    }, 
    "location": "New York" 
} 


GET /test/_search 
{ 
    "size" : 1, 
    "fields": ["location", "mygeo"] 
} 

所以,這個查詢請求也帶回結果如你所願。唯一的問題是你的lan/lon被格式化爲一個數組。查看結果來自查詢:

{ 
    "hits" : [{ 
     "_index" : "test", 
     "_type" : "test1", 
     "_id" : "AVCGqhsq9y2W0mh1rPgV", 
     "_score" : 1, 
     "fields" : { 
     "mygeo" : [ 
      "51.247607909,22.565701278" 
     ], 
     "location" : [ 
      "New York" 
     ] 
     } 
    } 
    ] 
} 

但是,這是Elasticsearch正式支持的格式之一。從documentation摘自:

隨着定義爲geo_point位置字段,我們可以進行包含緯度/經度對 索引文件,其可以被 格式化爲字符串,數組或對象:

PUT /attractions/restaurant/1 
{ 
    "name" : "Chipotle Mexican Grill", 
    "location" : "40.715, -74.011" 
} 

PUT /attractions/restaurant/2 
{ 
    "name" : "Pala Pizza", 
    "location" : { 
    "lat" : 40.722, 
    "lon" : -73.989 
    } 
} 

PUT /attractions/restaurant/3 
{ 
    "name" : "Mini Munchies Pizza", 
    "location" : [-73.983, 40.719] 
} 

另外,請從文檔注意這一點:

每個人都被捕獲至少一次:字符串地理位置點是 「緯度,經度」,而陣列地理位置點是 [經度,緯度] - 相反的順序!

+0

奇怪,因爲我沒有得到這個錯誤。至少不是如果mygeo實際上在映射模板中聲明爲geo_point的話。然後,mygeo根本不會返回而沒有任何錯誤。我知道非葉節點不能通過字段返回,但mygeo.lat和mygeo.lon不會返回,也不會引發錯誤。 – Matthias

+0

顯然我在那裏的代碼中犯了一個錯字。我將geo_type映射爲不正確,數據作爲字符串插入,但我想我已經找到了如何使其工作。我已經更新了我的答案 –

+0

這種方式應該是這樣。非常感謝。任何想法爲什麼「store」:true必須爲geo_point類型設置? – Matthias

1

如果你的領域被保存在_Source,您必須使用「_source」查詢參數指定的字段(從源代碼),你想要的(即使它是一個GeoPoint類型字段):

GET /_search 
{ 
    "_source": "obj.*", 
    "query" : { 
     "term" : { "user" : "kimchy" } 
    } 
} 

看到更多細節在這裏:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

長話短說:

  • 如果你的領域是源,那麼你需要指定「 _source「模式只能得到你想要的部分(字段)
  • 在你的查詢中指定」fields「對於在」store:yes「映射中聲明的字段將(大部分)是有意義的。這並沒有說明哪些字段應該從源代碼返回。
  • 是的,這裏有一個不一致的地方:如果你的字段被映射到源代碼中,但沒有用「Store:yes」聲明,那麼在你的查詢的「fields」參數中指定它可能會或不會返回它(我注意到在這種情況下會返回「簡單」字段,例如文本,數字等,但更復雜的字段(如時間戳或GeoPoint不在))。