2016-03-04 78 views
0

我試圖讓我的頭纏住嵌套查詢,但我無法得到這個工作。 我在ES 2項看起來像這樣如何訪問彈性搜索嵌套對象

{ 
"_index": "catalog", 
"_type": "products", 
"_source": { 
    "product": { 
     "ean": "abc", 
     "features": { 
     "Product Type": "DVD player", 
     }, 
     "color": "Black", 
     "manufacturer": "Sony", 
     "sitedetails": [ 
     { 
      "name": "amazon.com", 
      "sku": "zzz", 
      "url": "http://www.amazon.com/dp/zzz" 
     } 
     ], 
     "category": "Portable DVD Players" 
    } 
} 
}, 
{ 
"_index": "catalog", 
"_type": "products", 
"_source": { 
    "product": { 
     "ean": "def", 
     "features": { 
     "Product Type": "MP3 player", 
     }, 
     "color": "Black", 
     "manufacturer": "LG", 
     "sitedetails": [ 
     { 
      "name": "amazon.com", 
      "sku": "aaa", 
      "url": "http://www.amazon.com/dp/aaa" 
     } 
     ], 
     "category": "MP3 Players" 
    } 
} 

}

2個問題:

  • 什麼是捲曲得到SKU = ZZZ?
  • 在搜索「玩家」時得到兩個項目的捲曲是什麼?
  • tnx!

    +0

    分享映射請'GET /索引/類型/ _mapping' – Richa

    回答

    1

    Heyy bro,讓我們做魔術。 首先,你需要包括你的嵌套對象的映射,這樣

    curl -XPUT "http://192.168.99.100:9200/catalog" -d' 
    { 
         "mappings": { 
         "products": { 
          "properties": { 
           "product": { 
            "type": "nested", 
            "properties": { 
             "features": { 
             "type":"nested" 
             }, 
            "sitedetails": { 
             "type": "nested" 
            } 
            } 
           } 
          } 
         } 
        } 
    }' 
    

    之後,讓我們將您的數據(更改您的產品類型到產品類型)

    curl -XPOST "http://192.168.99.100:9200/catalog/products" -d' 
    { 
        "product": { 
         "ean": "abc", 
         "features": { 
         "product_type": "DVD player" 
         }, 
         "color": "Black", 
         "manufacturer": "Sony", 
         "sitedetails": [ 
         { 
          "name": "amazon.com", 
          "sku": "zzz", 
          "url": "http://www.amazon.com/dp/zzz" 
         } 
         ], 
         "category": "Portable DVD Players" 
        } 
    }' 
    

    現在,讓我們做查詢

    curl -XPOST "http://192.168.99.100:9200/catalog/products/_search" -d' 
    { 
        "query": { 
         "bool": { 
         "must": [ 
          { 
           "nested": { 
            "path": "product.features", 
            "query": { 
            "match": { 
             "product.features.product_type": "player" 
            } 
            } 
           } 
          }, 
          { 
           "nested": { 
            "path": "product.sitedetails", 
            "query": { 
            "match": { 
             "product.sitedetails.sku": "zzz" 
            } 
            } 
           } 
          } 
         ] 
         } 
        } 
    }' 
    

    和響應將是:

    "hits": { 
         "total": 1, 
         "max_score": 1.4054651, 
         "hits": [ 
         { 
          "_index": "catalog", 
          "_type": "products", 
          "_id": "AVM_fcYgvVoSi3OfqPTX", 
          "_score": 1.4054651, 
          "_source": { 
           "product": { 
            "ean": "abc", 
            "features": { 
            "Product Type": "DVD player" 
            }, 
            "color": "Black", 
            "manufacturer": "Sony", 
            "sitedetails": [ 
            { 
             "name": "amazon.com", 
             "sku": "zzz", 
             "url": "http://www.amazon.com/dp/zzz" 
            } 
            ], 
            "category": "Portable DVD Players" 
           } 
          } 
         } 
         ] 
        } 
    

    希望它能幫助:d