2017-04-13 196 views
0

我有以下的JSON描述一個國家的城市(1:N)關係ElasticSearch - 獲取分頁的結果嵌套表(嵌套分頁)

{ 
    "country": [ 
     { 
      "id": 1, 
      "name": "Country1", 
      "city": [ 
       {"id": 1, "name": "City1"}, 
       {"id": 2,"name": "City2"} 
      ] 
     }, { 
      "id": 2, 
      "name": "Country2", 
      "city": [ 
       {"id": 3,"name": "City3"}, 
       {"id": 4,"name": "City4"} 
      ] 
     }, { 
      "id": 3, 
      "name": "Country3", 
      "city": [ 
       {"id": 5,"name": "City5"}, 
       {"id": 6,"name": "City6"} 
      ] 
     } 
    ] 
} 

我有3加載它變成一個ES地圖這三個國家的文件。
我已經在這個城市指數

... 
"city": { 
      "type": "nested", 
... 

我想查詢所有城市,並得到一個分頁結果添加嵌套屬性。

  1. 例如3個命中將返回city1,位於City2,請分享幫助
  2. 我想按國家名稱過濾

我試圖

GET /127.0.0.1:9200/country_city/_search 
{ 
    "from": 0, 
    "size": 2, 
    "fields": [ 
     "city.id", "city.name" 
    ] 
} 

GET /127.0.0.1:9200/country_city/country/_search?_source=false 
{ 
    "query": { 
    "nested": { 
     "path": "city", 
     "query": { 
     "match_all": {} 
     }, 
     "inner_hits": { 
      "sort": "city.id", 
      "from": 0, 
      "size": 3 
     } 
    } 
    }, 
    "fields": [ 
    "name", 
    "city.id", 
    "city.name" 
    ] 
} 

但是第一個回來了兩個4城市而不是2.
(2個國家各有2個城市)
第二個返回所有文檔(儘管請求中大小爲2),並且在內部元素中返回了每個國家的前3個城市。

如何獲取嵌套對象的頁面大小? 然後進展到下一頁?

回答

0

這應該工作

映射

{ 
    "mappings": { 
     "type": { 
      "properties": { 
       "country": { 
        "type": "nested", 
        "properties": { 
         "id": { 
          "type": "integer" 
         }, 
         "name": { 
          "type": "text" 
         }, 
         "city": { 
          "type": "nested", 
          "properties": { 
           "id": { 
            "type": "integer" 
           }, 
           "name": { 
            "type": "keyword" 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

查詢

{ 
    "query": { 
     "nested": { 
      "path": "country", 
      "inner_hits": {}, 
      "query": { 
       "nested": { 
        "path": "country.city", 
        "query": { 
         "match_all": {} 
        }, 
        "inner_hits": { 
         "from": 0, 
         "size": 1, 
         "_source": { 
          "includes": ["country.city.name", "country.city.id"] 
         } 
        } 
       } 
      } 
     } 
    } 
} 

github bug

source filtering

謝謝

+0

這實際上會返回空數組。即使我正在清除內部查詢。順便說一句,爲什麼外部對象標記爲嵌套也? – Jeb

+0

是的,因爲另一個對象也是嵌套的。你是否運行了我共享的映射,國家和城市都嵌套對象 – user3775217

+0

我做了,但是當數據饋送數據(使用動態映射)時,我的數據饋送器創建了另一個映射。我的映射是包含「城市」的對象「國家」。你的名字是'type',其中包含'country',其中包含'city'。 – Jeb